[AS3]as3中键盘按键控制影片拖动源代码实例
[AS3]as3中键盘按键控制影片拖动源代码实例
[AS3]as3中键盘按键控制影片拖动源代码实例
- package
- {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.KeyboardEvent;
- import flash.events.TimerEvent;
- import flash.utils.Timer;
- import flash.utils.getTimer;
- public class Main extends Sprite
- {
- var mascot:Bo = new Bo();
- var leftArrow:Boolean = false;
- var rightArrow:Boolean = false;
- var upArrow:Boolean = false;
- var downArrow:Boolean = false;
- public function Main():void
- {
- init();
- }
- private function init():void
- {
- this.addChild(mascot);
- stage.addEventListener(KeyboardEvent.KEY_DOWN,downs);
- stage.addEventListener(KeyboardEvent.KEY_UP,ups);
- stage.addEventListener(Event.ENTER_FRAME,moves);
- }
- public function downs(e:KeyboardEvent):void
- {
- if(e.keyCode == 37)
- {
- leftArrow = true;
- }
- else if(e.keyCode == 38)
- {
- upArrow = true;
- }
- else if(e.keyCode == 39)
- {
- rightArrow = true;
- }
- else if(e.keyCode == 40)
- {
- downArrow = true;
- }
- }
- public function ups(e:KeyboardEvent):void
- {
- if(e.keyCode == 37)
- {
- leftArrow = false;
- }
- else if(e.keyCode == 38)
- {
- upArrow = false;
- }
- else if(e.keyCode == 39)
- {
- rightArrow = false;
- }
- else if(e.keyCode == 40)
- {
- downArrow = false;
- }
- }
- public function moves(e:Event):void
- {
- var speed:Number = 15;
- if(leftArrow && upArrow)
- {
- mascot.x -= speed;
- mascot.y -= speed;
- }
- if(leftArrow)
- {
- mascot.x -= speed;
- }
- else if(upArrow)
- {
- mascot.y -= speed;
- }
- if(rightArrow && downArrow)
- {
- mascot.x += speed;
- mascot.y += speed;
- }
- if(rightArrow)
- {
- mascot.x += speed;
- }
- else if(downArrow)
- {
- mascot.y += speed;
- }
- }
- }
- }
热门文章推荐
- [JS]window.location获取url各项参数详解
- [JS]jQuery,javascript获得网页的高度和宽度
- [JS]视频弹窗视频弹出层videoLightBox(含三种播放器的用法)
- [JS]JS提交中文encodeURI两次转码
- [JS]js版方面encodeURI转码和decodeURI解码的用法实例
- [JS]js取当前机子的时间戳实例
- [JS]AES加密(基于crypto-js)PHP后端解密
- [JS]data:image/png;base64写法的用途及说明
请稍候...