2010-08-06 96 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