所以我使用了动作脚本3,并且在将代码添加到舞台后,我的碰撞代码停止工作。当触摸墙壁时,玩家根本无法移动。然而,如果我通过将影片剪辑拖到舞台上将其添加到舞台上,它可以正常工作,播放器在触摸墙壁时不会冻结,而是无法通过墙壁,但它可以从墙壁移回。
这是我将其添加到舞台的代码。
public class Main extends MovieClip
{
var mountains: Mountains;
var homePage: HomePage;
var oneManager: OneManager;
public function Main()
{
mountains = new Mountains;
homePage = new HomePage;
oneManager = new OneManager;
addChild(homePage);
stage.displayState = "fullScreen";
homePage.playButtons.addEventListener(MouseEvent.CLICK, onPlayButtonsClick);
}
function onPlayButtonsClick(event:MouseEvent):void
{
stage.focus = stage;
parent.addChild(oneManager);
homePage.parent.removeChild(homePage);
}
}
这是我的代码,它使用碰撞检测来阻止玩家穿越墙壁
private function onKeyDown(event: KeyboardEvent): void
{
if (event.keyCode == Keyboard.A)
{
vx = -5;
}
else if (event.keyCode == Keyboard.D)
{
vx = 5;
}
else if (event.keyCode == Keyboard.W)
{
vy = -5;
}
else if (event.keyCode == Keyboard.S)
{
vy = 5;
}
}
private function onKeyUp(event: KeyboardEvent): void
{
if (event.keyCode == Keyboard.A || event.keyCode == Keyboard.D)
{
vx = 0;
}
else if (event.keyCode == Keyboard.S || event.keyCode == Keyboard.W)
{
vy = 0;
}
}
private function onEnterFrame(event: Event): void
{
if (player.collisionArea.hitTestObject(wall))
{
player.y -= vy;
}
}