如何让我的影片剪辑的性格搬家? [英] How do I get my movieclip character to move?
问题描述
我一直在尝试了几个小时了,我不能让我的小角色移动与键盘。
I've been trying for a few hours now and I cant get my little character to move with the keyboard.
我已经跑了一丝做出看看是否有什么发生了什么和位置值没有改变,但我的性格没有反应到该位置变化。
I have ran a trace to make see if anything was happening and the position value does change but my character doesn't react to that position change.
我收到没有错误。无论我的性格和BrickBlock是影片剪辑,他们已经进口了动作。
I receive no errors. Both my Character and BrickBlock are movieclips and they have been imported for ActionScript.
如果需要任何其他信息,请让我知道。谢谢! :)
If any other information is needed please let me know. Thank you! :)
我的关注code:
package {
import flash.events.Event
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class CharMove extends MovieClip {
var char1 :Character;
var block :BrickBlock;
public function CharMove()
{
char1 = new Character();
block = new BrickBlock();
//this.addEventListener(Event.ENTER_FRAME, collide)
stage.addEventListener(KeyboardEvent.KEY_DOWN, kDown);
}
/*function collide(e:Event):void
{
if(char.hitTestObject(block))
{
char.visible = !char.visible;
}
}*/
function kDown(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
char1.x -= 5;
trace(char1.x);
break;
case Keyboard.RIGHT:
char1.x +=5;
trace(char1.x);
break;
}
}
}
}
推荐答案
您可能要考虑写一个静态的输入类。
You might want to consider writing a static Input class.
package input {
import flash.display.Stage;
import flash.events.KeyboardEvent;
public class Input {
private static var keys:Array = new Array(255);
public static function setup(stage:Stage):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
}
private static function keyDown(e:KeyboardEvent):void {
keys[e.keyCode] = true;
}
private static function keyUp(e:KeyboardEvent):void {
keys[e.keyCode] = false;
}
public static function isKeyDown(k:int):Boolean {
return keys[k];
}
public static const A:uint = 65;
public static const B:uint = 66;
public static const C:uint = 67;
// The rest of the keys...
}
}
要使用它第一次调用的设置()添加了侦听器的 KEY_DOWN 和 KEY_UP 的事件。 他们可以很容易地查询键,并相应做好相关行动。
To use it first call setup() which adds the listeners for KEY_DOWN and KEY_UP events. They you can easily query keys and do relevant actions accordingly.
Input.setup(stage);
/...
if(Input.isKeyDown(Input.A)) {
char1.x -= 5;
}
这篇关于如何让我的影片剪辑的性格搬家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!