Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 信号键盘移动AS3_Actionscript 3_Flash_Signals - Fatal编程技术网

Actionscript 3 信号键盘移动AS3

Actionscript 3 信号键盘移动AS3,actionscript-3,flash,signals,Actionscript 3,Flash,Signals,我正试图通过信号和Hero类让键盘运动正常。到目前为止,我可以得到信号发送,但我不能得到它采取键盘输入。我应该使用信号进行移动,还是从Hero类调用函数 这是英雄的代码: package { import flash.display.MovieClip; import flash.display.Sprite; import org.osflash.signals.Signal; import flash.events.Event; import com.natejc.input.Keyboar

我正试图通过信号和
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

计数是为了测试我自己,看看发生了什么。

是否有一个
isKeyDown
检查通过了?它有没有进过里面?问题似乎出在键盘管理器上,这里没有显示。信号系统在工作时并不重要。由您决定如何构造代码—您可以使用它,也可以不使用它。但问题是,如果按下键盘,就要捕捉。不,向下键没有通过。看起来它实际上并没有进入函数。我不确定如何捕捉键盘交互。键盘管理器是我们老师给我们的解决问题的工具,我忘了使用
keyboardmanager.init(this.stage)在主菜单中。我的头撞在墙上已经有两天了!
this.mcHero.moveSignal.add(moveHero);

    }

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