2010-08-06 85 views
0

我正嘗試用一把船在左右兩邊按鍵之間移動來創建一個簡單的遊戲。移動是可以的,但是當我嘗試檢測左端和右端時,它根本不起作用。以下是代碼的一部分。什麼可能是錯的?檢測位置?


    stage.addEventListener(Event.ENTER_FRAME,moveBoat); 

function moveBoat(event:Event):void { 
if(! boat.x >= 700){ 

if(moveLeft) { 
    boat.x -= 5; 
    boat.scaleX = 1; 
} 
if (moveRight) { 
    boat.x += 5; 
    boat.scaleX = -1; 
} 


} 
} 
+0

什麼不行,具體是什麼?到達邊界時會發生什麼? – tzaman 2010-08-06 16:02:23

+0

嗨,船走出遊戲區。但我現在通過下面的代碼解決了這個問題: if(moveLeft && boat.x> 70){ \t \t \t boat.x- = 5; \t \t \t boat.scaleX = 1; \t \t} 但現在我有另一個問題。這艘船將在潛艇上投擲炸彈,我想知道如何以簡單的方式解決這個問題。這艘船應該有五個炸彈,所以我想使用五個布爾變量,從一開始就是錯誤的,當它們掉落時,它們變得真實並且從那時的船隻x位置落到底部。嗯,任何建議如何做到這一點?謝謝! :) – 2010-08-06 17:33:16

回答

0

如果你已經解決了你的碰撞問題,這裏有一個關於你的丟彈問題的答案。這樣做有5個布爾變量將是一個相當不確定的做法;而不是簡單地用一個整數來記錄你的船了多少炸彈留下下降,每它滴一次,1。降低這個數值,以下是一些示例代碼:

//Create a variable to hold the number of bombs left. 
var bombsLeft:int = 5; 

//Create an event listener to listen for mouse clicks; upon a click, we'll drop a bomb. 
addEventListener(MouseEvent.CLICK, dropBomb); 

//The function dropBomb: 
function dropBomb(event:MouseEvent):void 
{ 
    if (bombsLeft > 0) 
    { 
     //Create a new instance of the Bomb class; this could be an object in your Library (if you're using the Flash IDE), which has a graphic inside it of a bomb. 
     var newBomb:Bomb = new Bomb(); 
     //Position the bomb. 
     newBomb.x = boat.x; 
     newBomb.y = boat.y; 
     //Add it to the stage 
     addChild(newBomb); 
     //Reduce the number of bombs you have left. 
     bombsLeft--; 
    } 
    //At this point you could check if bombsLeft is equal to zero, and maybe increase it again to some other value. 
} 

這不包括代碼,然後向下移動炸彈,但你可以簡單地使用更新循環來做到這一點。如果你正在努力做到這一點,讓我知道,我會給你另一個例子。

希望有所幫助。

debu