我正在编写AS3中的2D侧滚动平台游戏。我有一个与播放器一起滚动的相机,但仍保持固定的高度。
import flash.events.Event;
import flash.geom.Rectangle;
stage.addEventListener(Event.ENTER_FRAME, camera);
function camera(evt:Event){
root.scrollRect = new Rectangle(player.x - stage.stageWidth/2, camera1.y - stage.stageHeight/2, stage.stageWidth, stage.stageHeight);}
我编写了一个坑,如果玩家进入,将在一个单独的框架上通过屏幕将玩家带到游戏中。此函数从帧中删除所有事件侦听器。
stage.addEventListener(Event.ENTER_FRAME, walls);
function walls(e:Event){
if (player.hitTestObject(Pit1)){
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keydown);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyup);
stage.removeEventListener(Event.ENTER_FRAME, movement);
stage.removeEventListener(Event.ENTER_FRAME, walls);
stage.removeEventListener(Event.ENTER_FRAME, camera);
gotoAndPlay("GameOver");
}
}
但是当我通过屏幕进入游戏时,我仍然会收到相机的TypeError#1009。
答案 0 :(得分:1)
您应该创建一个处理enterframe事件的唯一函数。您可以刷新视图并在该独特功能中测试碰撞:
/**
* Init function, called at the end.
*/
function init()
{
stage.addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
}
/**
* Function that updates your camera
*/
function updateCamera()
{
root.scrollRect = new Rectangle(player.x - stage.stageWidth/2, camera1.y - stage.stageHeight/2, stage.stageWidth, stage.stageHeight);
}
/**
* Function that test your collisions.
*/
function testCollision()
{
if (player.hitTestObject(Pit1))
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keydown);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyup);
stage.removeEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
gotoAndPlay("GameOver");
}
}
/**
* Unique function that manages enterframe events
*/
function enterFrameEventHandler(e:Event)
{
// update camera position
updateCamera();
// test collision
testCollision();
// etc.
}
init();