我想制作一个在按下键盘箭头时运行影片剪辑的移动角色。例如:
当没有按下箭头时,我希望当按下任何键盘箭头时角色不会移动并运行一个影片剪辑动画,当按下右箭头并且左箭头相同时,角色会运行。
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
movieClip_1.addEventListener(Event.ENTER_FRAME,fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
movieClip_1.y -= 5;
}
if (downPressed)
{
movieClip_1.y += 5;
}
if (leftPressed)
{
movieClip_1.x -= 5;
}
if (rightPressed)
{
movieClip_1.x += 5;
}
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
case Keyboard.DOWN:
{
downPressed = true;
break;
}
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
case Keyboard.DOWN:
{
downPressed = false;
break;
}
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
}
}
我的电影片段是:run_right
和run_left
。
我的时间表:
答案 0 :(得分:1)
这是您的Mistake
movieClip_1.addEventListener(Event.ENTER_FRAME,fl_MoveInDirectionOfKey);
------------
删除
movieClip_1.
应该是这样的
stage.addEventListener(Event.ENTER_FRAME,fl_MoveInDirectionOfKey);
答案 1 :(得分:1)
创建名为" movieClip_1"。
的MovieClipvar movieClip_1 = new MovieClip_1();
在此MovieClip中,添加" run_right"第一帧上的动画和" run_left"在第二帧。
我的意思是,添加包含动画的新MovieClip。
然后,转到fl_MoveInDirectionOfKey函数并写下:
if (rightPressed)
{
movieClip_1.gotoAndStop(1);
}
else if (leftPressed)
{
movieClip_1.gotoAndStop(2);
}
else
{
// no animation
// movieClip_1.gotoAndStop(3); idle animation could be on frame 3
}
答案 2 :(得分:1)
这是第二个解决方案,答案(Event.Enter_Frame
)位于 movieClip 内,因此您需要在MovieClip之前添加(root.
)
像这样改变
if (upPressed)
{
root.movieClip_1.y -= 5;
}
if (downPressed)
{
root.movieClip_1.y += 5;
}
if (leftPressed)
{
root.movieClip_1.x -= 5;
}
if (rightPressed)
{
root.movieClip_1.x += 5;
}