flash center

鼠标与键盘控制物体的移动 Tag: 教案 版权声明 :转载时请以超链接形式标明文章原始出处和作者信息及 本声明 http://flashcenter.blogbus.com/logs/1269923.html 鼠标控制物体的移动 例:利用鼠标控制小车的移动。 影片浏览器 注解 Scene 1 图层 1 Frame 1 car, <car> Actions for car onClipEvent (load) { clickSpot_x = _x; clickSpot_y = _y; speed = 7; clickMode = true; turnMode = false; } onClipEvent (mouseDown) { if (clickMode && _root._xmouse>125 && _root._xmouse<465) { clickSpot_x = _root._xmouse; clickSpot_y = _root._ymouse; } } onClipEvent (enterFrame) { _root.curve._visible = turnMode; _root.pointer._visible = !clickMode; gotoSpotX = _root._xmouse; gotoSpotY = _root._ymouse; delta_x = _x-gotoSpotX; delta_y = _y-gotoSpotY; targetRotation = -Math.atan2(delta_x, delta_y)/(Math.PI/180); _rotation = targetRotation; if (Math.sqrt((delta_x*delta_x)+(delta_y*delta_y))>speed) { _y -= speed*Math.cos(_rotation*(Math.PI/180)); _x += speed*Math.sin(_rotation*(Math.PI/180)); } if (_y<0) { _y = 232; } if (_y>232) { _y = 0; } if (_x<125) { _x = 465; } if (_x>465) { _x = 125; } } // 初始化变量 // 初始化移动速度 // 当鼠标单击时,保存鼠标位置 // 计算旋转角度 // 根据速度,设置小车坐标 // 设置小车移动范围 知识点:数字函数对象 数值比较与取样 / 三角函数 / 自然对数 / 指数和对数 / 圆周率 / 连乘积与平方根 1. Math.atan2() x 指定点的 x 坐标的数字 y 指定点的 y 坐标的数字 以弧度为单位计算并返回 xy 的反正切值。返回值表示相对直角三角形对角的角,其中 x 是临边边长,而 y 是对边边长。 2. Math.sqrt() 计算并返回指定数字的平方根 例 2 利用键盘控制小车的移动 影片浏览器 注解 Scene 1 图层 2 Frame 1 keyup Actions for keyup onClipEvent (enterFrame) { if (Key.isDown(Key.UP)) { gotoAndStop(2); } else { gotoAndStop(1); } } keydown Actions for keydown onClipEvent (enterFrame) { if (Key.isDown(Key.DOWN)) { gotoAndStop(2); } else { gotoAndStop(1); } } keyright Actions for keyright onClipEvent (enterFrame) { if (Key.isDown(Key.RIGHT)) { gotoAndStop(2); } else { gotoAndStop(1); } } keyleft Actions for keyleft onClipEvent (enterFrame) { if (Key.isDown(Key.LEFT)) { gotoAndStop(2); } else { gotoAndStop(1); } } 图层 1 Frame 1 car Actions for car onClipEvent (load) { speed = 5; } onClipEvent (enterFrame) { if (Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT)) { _x -= speed; _rotation = 270; } if (Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) { _x += speed; _rotation = 90; } if (Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)) { _y -= speed; _rotation = 0; } if (Key.isDown(Key.DOWN) && !Key.isDown(Key.UP)) { _y += speed; _rotation = 180; } if (Key.isDown(Key.LEFT) && Key.isDown(Key.UP) && !Key.isDown(Key.RIGHT) && !Key.isDown(Key.DOWN)) { _rotation = 315; } if (Key.isDown(Key.RIGHT) && Key.isDown(Key.UP) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.DOWN)) { _rotation = 45; } if (Key.isDown(Key.LEFT) && Key.isDown(Key.DOWN) && !Key.isDown(Key.RIGHT) && !Key.isDown(Key.UP)) { _rotation = 225; } if (Key.isDown(Key.RIGHT) && Key.isDown(Key.DOWN) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.UP)) { _rotation = 135; } if (_y<0) { _y = 231; } if (_y>231) { _y = 0; } if (_x<231) { _x = 465; } if (_x>465) { _x = 231; } } keyup 影片剪辑 keydown 影片剪辑 keyright 影片剪辑 keyleft 影片剪辑 // 设置移动速度变量 // 判断按键的方向,设置移动方向及移动速度 // 判断是否控制小车斜行,改变旋转属性 // 限制移动范围 知识点: Key.isDown() 一般运动 反弹 物体的基本运动: 随机文章: 反弹 2005-06-16 一般直线运动 2005-06-16 利用hitTest感知两物体是否相重叠或交叉 2005-05-27 计算器 2005-05-27 How cool flash games website. 2005-06-16 收藏到: Del.icio.us 引用地址: maggiegaohappy | 编辑 | 继续话题 | 转发 | 推荐 发表评论

原始快照:web.archive.org