我的eventListener是否超出范围?

时间:2011-07-20 06:01:31

标签: flash actionscript-3

我正在创建一个射击游戏,目前我有一个主要的课程,我在这里添加“主要孩子”,我有一个对象类“枪手”,我在我的主要部分添加了“枪手”,我在炮手类上有一个eventListener,MOUSE_CLICK,但是当我点击一下时没有任何反应,为什么会这样呢?在我的情况下,枪手应该射击子弹。事件没有被发送到我的炮手对象?

我主班的构造函数。

    public function waterMain() 
    {
        Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
        canPlay = true;
        soundTimer = new Timer(300, 1);
        soundTimer.addEventListener(TimerEvent.TIMER, waterSoundHandler, false, 0, true);
        waterMovementSound = new pondSound();
        bg = new Background(stage);
        myRippler = new Rippler(bg , 20, 5, 5);
        //event listeners
        addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
        addEventListener(TouchEvent.TOUCH_MOVE, onTouchBegin);
        addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveTriggered);

        addChild(bg);
        touchPoints = Multitouch.maxTouchPoints;
        if (Multitouch.supportsGestureEvents && Multitouch.supportsTouchEvents)
        {
            trace("Multitouch gesture supported");
            var inputField:TextField = new TextField();
            inputField.border = true;
            inputField.wordWrap = true;
            inputField.text = "Multitouch gesture,touch supported,MTP : "+touchPoints;
            addChild(inputField);
        }else if (Multitouch.supportsTouchEvents)
        {
            trace("touch  supported");
            var inputField2:TextField = new TextField();
            inputField2.border = true;
            inputField2.wordWrap = true;
            inputField2.text = "Multitouch touch supported,MTP : "+touchPoints;
            addChild(inputField2);
        }
        //game
        //creating gunner
        var gun:gunner = new gunner(stage);
        gun.x = stage.stageWidth / 2;
        gun.y = stage.stageHeight;
        addChild(gun);
    }

枪手类

package  
{
    import flash.display.MovieClip;
    import flash.display.Stage;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    /**
     * ...
     * @author ...
     */
    public class gunner extends MovieClip
    {
        private var _stageRef:Stage;
        public function gunner(stageRef:Stage) 
        {
            _stageRef = stageRef;
            addEventListener(Event.ENTER_FRAME, rotateTurret);
            addEventListener(MouseEvent.CLICK, fireBullet);
        }


        private function rotateTurret(evt:Event):void 
        {
            //calculations, distance, angle etc
            var a:Number = _stageRef.mouseX - this.x;
            var b:Number = _stageRef.mouseY - this.y;
            var angRad:Number = Math.atan2(b, a);
            var angDeg:Number = (angRad * 180 / Math.PI);

            rotation = angDeg;
        }
        private function fireBullet(mouseEvt:MouseEvent):void
        {

            _stageRef.addChild(new bullet(_stageRef, 500,500));
        }
    }

}

子弹课

public class bullet extends MovieClip
{
    //variables
    private var _bulletSpeedVector:Point;
    private var _stageRef:Stage;
    private var _maxX:Number = 1024;
    private var _maxY:Number = 768;
    public function bullet(stageRef:Stage,x:Number,y:Number) 
    {
        _stageRef = stageRef;
        this.x = x;
        this.y = y;
        addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
    }
    private function loop(e:Event) : void
    {
        //move bullet

        /*if (this.x > _maxX || this.x < 0)
            removeSelf();
            else if (this.y > _maxY || this.y < 0)
            removeSelf();
            */
    }
    private function removeSelf() : void
    {
        removeEventListener(Event.ENTER_FRAME, loop);

        if (_stageRef.contains(this))
            _stageRef.removeChild(this);
    }


}

3 个答案:

答案 0 :(得分:1)

从它的外观来看,MouseEvent.Click只会在你点击炮手而不是主舞台时注册。您可能希望在舞台上注册EventListener,而不是在炮手上添加EventListener。尝试更换:

addEventListener(MouseEvent.CLICK, fireBullet);

在炮手的构造函数中:

_stageRef.addEventListener(MouseEvent.CLICK, fireBullet);

这样,每次点击舞台时都应该调用fireBullet。如果我是对的,现在点击你的炮手将使他射击子弹。

答案 1 :(得分:0)

stage.addEventListener(MouseEvent.CLICK, fireBullet);

并且该类的名字应该大写。

答案 2 :(得分:0)

我不确定为什么会发生这种情况,但当我将其更改为MouseEvent.MOUSE_DOWN时,它可以正常工作。但点击它不能工作。