Signals Keyboard Movement AS3

2019-09-03 19:54发布

问题:

I'm trying to get my keyboard movement to work using signals and a Hero class. So far I can get the signal to dispatch but I can't get it to take the keyboard input. Should I be using signals for movement at all or should I just call the function from the Hero class.

This is the code for Hero:

package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import org.osflash.signals.Signal;
import flash.events.Event;
import com.natejc.input.KeyboardManager;
import com.natejc.input.KeyCode;
/**
 * ...
 * @author Kevin Raskell
 */
public class Hero extends MovieClip
{
    public var defeatedSignal :Signal = new Signal();
    public var _nHeroMovement :Number = 5;
    public var moveSignal :Signal;


    public function Hero() 
    {
        super();
        this.mouseChildren = false;
        this.mouseEnabled = false;

        this.addEventListener(Event.ENTER_FRAME, heroMovement);

        moveSignal = new Signal();

    }

    public function heroDefeated()
    {
        trace("You have died");
    }

    public function heroMovement($e:Event):void
    {
        moveSignal.dispatch();
        if (this)
        {               
            if (KeyboardManager.instance.isKeyDown(KeyCode.DOWN))
            {
                if (this.y + this.height > this.stage.stageHeight || this.y - this.height <= 0)
                {
                    this.y += -15;
                    this.gotoAndPlay("Idle");
                    return;
                }
                this.y += _nHeroMovement;
                this.gotoAndPlay("Down");
            }
            else if (KeyboardManager.instance.isKeyDown(KeyCode.UP))
            {
                if (this.y + this.height > this.stage.stageHeight || this.y - this.height <= 0)
                {
                    this.y += 15;
                    this.gotoAndPlay("Idle");
                    return;
                }
                this.y -= _nHeroMovement;
                this.gotoAndPlay("Up");
            }

            if (KeyboardManager.instance.isKeyDown(KeyCode.LEFT))
            {
                if (this.x + this.width > this.stage.stageWidth || this.x - this.width <= 0) 
                {
                    this.x += 15;
                    this.gotoAndPlay("Idle");
                    return;
                }                   
                this.x -= _nHeroMovement;
                this.gotoAndPlay("Left");

            }
            else if (KeyboardManager.instance.isKeyDown(KeyCode.RIGHT))
            {   
                if (this.x + this.width > this.stage.stageWidth || this.x - this.width <= 0) 
                {
                    this.x += -15;
                    this.gotoAndPlay("Idle");
                    return;
                }
                this.x += _nHeroMovement;
                this.gotoAndPlay("Right");
            }
        }//end if
    }//end heroMovement     
}//end Hero
}//end package

This is in the main function:

this.mcHero.moveSignal.add(moveHero);

    }

    private function moveHero():void
    {
        trace("Testing" + count);
        count++;
    }

The counting was for testing purposes for myself to see what was happening.