我正在开发一个简单的 Flash 游戏,我的玩家将桶从船上推下以赢得比赛。我已经编写了移动代码并定义了大多数对象,但是当我添加碰撞属性时,我失去了用键盘移动玩家的能力。这是代码:
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class Main_Character extends MovieClip
{
var vx:int;
var vy:int;
public function Main_Character()
{
init();
}
function init():void{
vx = 0;
vy = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5;
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
}
}
function onKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT ||
event.keyCode == Keyboard.RIGHT)
{
vx = 0;
}
if (event.keyCode == Keyboard.DOWN ||
event.keyCode == Keyboard.UP)
{
vy = 0;
}
}
function onEnterFrame(event:Event):void
{
player1.x += vx;
player1.y += vy;
if (player1.x > 530)
{
player1.x = 530;
}
else if (player1.x < 0)
{
player1.x = 0;
}
else if (player1.y < 0)
{
player1.y = 0;
}
else if (player1.y > 515)
{
player1.y = 515;
}
else if (player1.hitTestObject(water1))
{
player1.x -= vx;
player1.y -= vy;
}
Collision.block(barrel1, player1);
}
}
}