我试图让我的键盘移动使用信号和Hero
类来工作。到目前为止,我可以获得发送信号,但我无法接受键盘输入。我应该使用信号进行移动,还是应该从Hero
类调用该函数。
这是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.mcHero.moveSignal.add(moveHero);
}
private function moveHero():void
{
trace("Testing" + count);
count++;
}
计数是出于测试目的,让我自己看看发生了什么。