嘿伙计们,所以基本上我想创建多个级别,我设置了如下所示的级别,但由于某种原因,level_2 的 HittestObject 没有启动。我无法确定我做错了什么。级别 2 已启动,但当我试图击中你时,它就直接通过了。
我有一个Goal_1 和一个goal_2,我想和玩家一起击球。
我想有多个级别的goal_1和多个级别的goal_2等等
这是代码:
public class keepFocusEngine extends MovieClip
{
//MultiTouch added for touch gestures
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
public var btnPressedUp:Boolean;
public var btnPressedDown:Boolean;
public var btnShootPlayer:Boolean;
public var btnUp:MovieClip;
public var btnDown:MovieClip;
public var shootPlayer:MovieClip;
private var speed:Number = 6.0;
public var vx:Number = 0;
public var vy:Number = 0;
private var friction:Number = 0.93;
private var maxspeed:Number = 12;
public var player:mPlayer;
public var mGoal_1:goal_1;
public var mGoal_2:goal_2;
public var playerLives:Number;
public var livesText:TextField;
public var highScoreText:TextField;
public var nScore:Number;
public var levelText:TextField;
public var nLevel:Number;
public var menuEnd:mcEndGameScreen;
public var menuStart:mcStartGameScreen;
public function keepFocusEngine()
{
//Initialize variable to false
btnDown.visible = false;
btnUp.visible = false;
shootPlayer.visible = false;
livesText.visible = false;
highScoreText.visible = false;
levelText.visible = false;
//Create loader object
var startGameLoader:Loader = new Loader();
//Add event listener for complete event
startGameLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, startGameLoaded);
//Load loader object
startGameLoader.load(new URLRequest("StartGameScreen.swf"));
//Create End game loader object
var endGameLoader:Loader = new Loader();
//Add event listener
endGameLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, endGameLoaded);
//Load end game object
endGameLoader.load(new URLRequest("EndGameScreen.swf"));
}
private function startGameLoaded(e:Event):void
{
//Get a refrece to the loaded movieclip
menuStart = e.target.content as mcStartGameScreen;
//Listen for start game event
menuStart.addEventListener("START_GAME", playGameAgain);
//Add start screen to the stage
addChild(menuStart);
}
private function endGameLoaded(e:Event):void
{
menuEnd = e.target.content as mcEndGameScreen;
menuEnd.addEventListener("END_GAME", playGameAgain);
addChild(menuEnd);
menuEnd.hideEndScreen();
}
private function playGameAgain(e:Event):void
{
//Add Hi Res Stats
addChild(new Stats());
//mainPlayerObject added to stage
player = new mPlayer();
stage.addChild(player);
player.x = (stage.stageWidth / 2) - 280;
player.y = (stage.stageHeight / 2);
mGoal_1 = new goal_1();
stage.addChild(mGoal_1);
//Update Player lives and add 3 lives
playerLives = 3;
updatePlayerLives();
//Update Level progression
nLevel = 1;
updatePlayerLevel();
//Add score to texxt field
nScore = 0;
updateHighScore();
//Initialize stage objects to true
btnDown.visible = true;
btnUp.visible = true;
shootPlayer.visible = true;
livesText.visible = true;
highScoreText.visible = true;
levelText.visible = true;
//Hide external screens
menuEnd.hideEndScreen();
menuStart.hideStartScreen();
//setup Listeners when btn's are rolled over and out
btnUp.addEventListener(TouchEvent.TOUCH_OVER, btnUpMouseOver, false, 0, true);
btnUp.addEventListener(TouchEvent.TOUCH_OUT, btnUpMouseOver, false, 0, true);
btnDown.addEventListener(TouchEvent.TOUCH_OVER, btnDownMouseOver, false, 0, true);
btnDown.addEventListener(TouchEvent.TOUCH_OUT, btnDownMouseOver, false, 0, true);
shootPlayer.addEventListener(TouchEvent.TOUCH_TAP, shootPlayerObject, false, 0, true);
//setup game loop event listeners
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
//To completely end game when back button pushed on android
NativeApplication.nativeApplication.addEventListener( KeyboardEvent.KEY_DOWN, handleKeyDown );
}
private function gameLoop(e:Event):void
{
playerShoot();
playerControl();
playerStageBoundaries();
checkEndGameCondition();
checkPlayerOffScreen();
level_1();
}
private function level_1():void
{
if(player.hitTestObject(mGoal_1))
{
trace("Goal_1 Collision");
//Remove button for constant movement
btnShootPlayer = false;
mGoal_1.destroyGoal_1();
player.destroyPlayer();
//Update High Score text
nScore += 10;
updateHighScore();
//Update level
nLevel++;
updatePlayerLevel();
stage.removeEventListener(Event.ENTER_FRAME, gameLoop);
level_2();
}else
{
checkEndGameCondition();
}
}
public function level_2():void
{
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
trace("Level_2 Initiated");
//Keep Text Scores initiated
updateHighScore();
updatePlayerLives();
player = new mPlayer();
stage.addChild(player);
player.x = (stage.stageWidth / 2) - 280;
player.y = (stage.stageHeight / 2);
mGoal_1 = new goal_1();
stage.addChild(mGoal_1);
if (player.hitTestObject(mGoal_1))
{
trace("Level 2 Hit test works!");
nScore += 10;
updateHighScore();
}
}