2011-07-19 32 views
0

因为我是新来的as3,我想实现一个MachineGun,在有弹药的同时触发,并且触发器被拉动,在我的情况下为MouseEvent.MOUSE_DOWNMOUSE_DOWN触发每一帧,ActionScript 3

问题是此事件只会触发一次。

我得到的最接近的是这是MouseEvent.MOUSE_MOVE,但它失败时,鼠标的位置是持续我的目的

编辑:

我需要更新的鼠标光标每帧

回答

5

当您MouseEvent.MOUSE_DOWN事件处理程序,您可以创建一个事件侦听器侦听要调度的Event.ENTER_FRAME活动中删除。使用Event.ENTER_FRAME事件处理程序,您可以重复调用处理射击项目符号的方法。以下是一个例子,我的示例仿照在http://www.benoitfreslon.com/actionscript-throw-bullets-to-mouse-direction

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 

    [SWF(width="500", height="500", frameRate="24", backgroundColor="0xFFFFFF")] 
    public class Main extends Sprite 
    { 
     private var _gun:Gun; 
     private var _interval:int; 

     public function Main():void 
     { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 

     }// end function 

     private function init(e:Event = null):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 

      _gun = new Gun(); 
      _gun.x = stage.stageWidth/2 - _gun.width/2; 
      _gun.y = stage.stageHeight/2 - _gun.height/2; 
      addChild(_gun); 

      stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown); 

     }// end function 

     private function onStageMouseDown(e:MouseEvent):void 
     { 
      stage.addEventListener(Event.ENTER_FRAME, onStageEnterFrame); 
      stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp); 

     }// end function 

     private function onStageEnterFrame(e:Event):void 
     { 
      shootBullet(); 

     }// end function 

     private function shootBullet():void 
     { 
      if (_interval > 5) 
      { 
       var bullet:Bullet = new Bullet(); 
       bullet.addEventListener(Event.ENTER_FRAME, onBulletEnterFrame); 
       bullet.x = _gun.x; 
       bullet.y = _gun.y; 
       bullet.angleRadian = Math.atan2(stage.mouseY - _gun.y, stage.mouseX - _gun.x); 
       bullet.addEventListener(Event.ENTER_FRAME, onBulletEnterFrame); 
       addChild(bullet); 

       _interval = 0; // reset 

      }// end if 

      _interval++; 

     }// end function 

     private function onBulletEnterFrame(e:Event):void 
     { 
      var bullet:Bullet = Bullet(e.target); 
      bullet.x += Math.cos(bullet.angleRadian) * bullet.SPEED; 
      bullet.y += Math.sin(bullet.angleRadian) * bullet.SPEED; 

      if (bullet.x < 0 || bullet.x > 500 || bullet.y < 0 || bullet.y > 500) 
      { 
       removeChild(bullet); 
       bullet.removeEventListener(Event.ENTER_FRAME, onBulletEnterFrame); 

      }// end if 

     }// end function 

     private function onStageMouseUp(e:MouseEvent):void 
     { 
      stage.removeEventListener(Event.ENTER_FRAME, onStageEnterFrame); 
      stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp); 

      _interval = 0; 

     }// end function 

    }// end class 

}// end package 

import flash.display.Sprite; 

internal class Gun extends Sprite 
{ 
    public function Gun() 
    { 
     graphics.beginFill(0x000000); 
     graphics.drawCircle(0, 0, 10); 
     graphics.endFill(); 

    }// end function 

}// end class 

internal class Bullet extends Sprite 
{ 
    public var angleRadian:Number; 
    public const SPEED:Number = 10; 

    public function Bullet() 
    { 
     graphics.lineStyle(1, 0x000000); 
     graphics.drawCircle(0, 0, 10); 
     graphics.endFill(); 

    }// end function 

}// end class 

感兴趣的点是在onStageEnterFrame()方法。有一个if语句,用于检查_interval属性的值是否大于5,如果是,则创建新的项目符号并进行拍摄,否则_interval属性的值将递增。 _interval财产的目的是隔开被枪杀的子弹。

[UPDATE]

下面是示例闪存应用程序的运行的一个图像:

enter image description here

鼠标按钮被按下在右上角因此子弹正在拍摄在那个方向。

3

在接收一个MouseEvent.MOUSE_DOWN事件,运行一个while循环让机枪开火。循环在接收到MouseEvent.MOUSE_UP事件时中断。事情是这样的

private function handleMouseDown(event:Event):void 
{ 
    this.addEventListener(MouseEvent.MOUSE_UP , handleMouseUp); 
    startFiring(); 
} 


private function handleMouseUp(event:Event):void 
{ 
    this.removeEventListener(MouseEvent.MOUSE_UP , handleMouseUp); 
    this.addEventListener(MouseEvent.MOUSE_DOWN , handleMouseDown); 
    stopFiring(); 
} 

编辑:为了澄清,停止和启动点火功能是运行一个循环,以保持机枪射击的动画会然而,可能是功能。

+0

我还需要更新的光标位置,所以我可能知道镜头的位置,我该怎么做? –

+0

@Fabiano,在'MouseEvent.MOUSE_UP'上添加一个侦听器,它调用'aim(event.stageX,event.stageY)' –

0

附加动作射击按下鼠标时ENTER_FRAME事件,当鼠标移动起来

this.addEventListener(MouseEvent.MOUSE_DOWN, this.handler_down); 
this.addEventListener(MouseEvent.MOUSE_UP, this.handler_up); 

private function handler_down(event:Event):void { 
    super.addEventListener(Event.ENTER_FRAME, this.handler_frame); 
} 

private function handler_up(event:Event):void { 
    super.removeEventListener(Event.ENTER_FRAME, this.handler_frame); 
} 

private function handler_frame(event:Event):void { 
    this.fire(); 
} 
0

我必须经常这样做,而不是使用enter_frame侦听器,我选择Timer()和TimerEvent.Timer侦听器。通过这种方式,我可以更好地控制重复频率的触发频率,而无需考虑帧频和不支持的帧频。

private var fireRepeat:Timer; 
    private function triggerDown(e:MouseEvent):void { 
     if (!this.fireRepeat) {  // create the repeater 
      this.fireRepeat = new Timer(500, 0); 
      this.fireRepeat.addEventListener(TimerEvent.TIMER, this.fire); 
     } 
     fire();      // fire the first bullet 
     this.fireRepeat.reset(); // reset the repeater 
     this.fireRepeat.start(); // start the repeater 
    } 
    private function triggerUp(e:MouseEvent):void { 
     if (this.fireRepeat) { this.fireRepeat.stop(); } 
    } 
    public function fire(e:* = null):void { 
     trace('fire!'); 
    } 
相关问题