我让奴才在香蕉之后奔跑并且被箭头键控制。我也有一个生活吧,我会根据小奴是否掉落香蕉来跟踪它。我现在遇到的问题是当香蕉(从阵列中)撞到地面时,我的生命棒会完全掉落。我希望它能够逐渐逐渐每个打到地上的香蕉。我尝试使用调度事件,但它不起作用。
这就是我在主程序中所拥有的:
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
var leftKey:Boolean;
var rightKey:Boolean;
var upKey:Boolean;
var downKey:Boolean;
var jump:Boolean = false;
var xvelocity:int = 9;
var yvelocity:int = 3;
var gravity:Number = 7;
stage.addEventListener(Event.ENTER_FRAME, changeVelocity);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
function changeVelocity(evt:Event){
moveMinion();
yvelocity += gravity;
}
function moveMinion(){
if (leftKey == true){
sideMinion.x -= xvelocity;
sideMinion.left();
}
if (rightKey == true){
sideMinion.x += xvelocity;
sideMinion.right();
}
}
function checkKeyDown(e:KeyboardEvent){
if (e.keyCode == Keyboard.LEFT){
leftKey = true;
}
else if (e.keyCode == Keyboard.RIGHT){
rightKey = true;
}
}
function checkKeyUp(e:KeyboardEvent){
if (e.keyCode == Keyboard.LEFT){
leftKey = false;
}
else if (e.keyCode == Keyboard.RIGHT){
rightKey = false;
}
}
btnStart.addEventListener(MouseEvent.CLICK, makeItFall);
function makeItFall(e:MouseEvent){
var numBananas = 6;
var theBananas: Array = new Array();
theBananas = [];
for (var i = 0; i < numBananas; i++) {
var aBanana: banana = new banana();
theBananas.push(aBanana);
btnStart.visible=false;
aBanana.y=100;
theBananas[i].setSpot(Math.random()*450,Math.random()*200);
theBananas[i].setSpeed((Math.random()), 1);
stage.addChild(aBanana);
}
var health: Number= 100;
var healthPercent: Number= 1;
var lives:Event= new Event("lifeDrops")
addEventListener(Event.ENTER_FRAME, pickUpBananas);
function pickUpBananas(event:Event){
for (var i:int = theBananas.length-1; i>-1; i--){
if (sideMinion.hitTestObject(theBananas[i])){
stage.removeChild(theBananas[i]);
theBananas.splice(i,1);
}
if (info.hitTestObject(theBananas[i])){
dispatchEvent(lives);
health=health-10;
healthPercent=health/100;
trace(healthPercent);
life.scaleX= healthPercent;
text.text=String(health);
}
if (health<0){
gotoAndStop(5);
}
}
}
}
stop();
在香蕉班:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.Event;
public class banana extends MovieClip {
var velX: Number = 0;
var velY: Number = 0;
var falling: Boolean = false;
var gravity: Number = 2;
public function banana() {
var timing: Timer = new Timer(25, 0);
timing.addEventListener(TimerEvent.TIMER, moveMe);
timing.start();
}
private function moveMe(event: TimerEvent)
{
x += velX;
y += velY;
if (falling) velY += gravity;
/* trace("[BANANA] position:", x, y, "speed:", velX, velY);*/
}
public function setSpeed(dx,dy) {
velX = dx;
velY = dy;
}
public function setSpot(atX,atY){
this.x=atX;
this.y=atY;
}
public function makeItFall (){
falling=true;
}
}
}
答案 0 :(得分:1)
您的pickUpBananas
功能每帧运行并检查每个香蕉,看它是否接触地面。因此,如果香蕉击中,它将减少该帧的健康状况,并且每个后续帧,直到它不再接触地面。
你需要做些什么来防止同一个香蕉在后续帧中反复消耗健康状况。
具体的解决方案将取决于您希望游戏如何运作。您可以立即重置它的位置,使其不再接触地面,但您可能希望它保持坐在地上(或者至少在消失之前淡出)。在这种情况下,您可以在香蕉上设置一个标志,表明它已被计数,因此不再计算。如果你重新放下香蕉,请记得清除那面旗帜。
答案 1 :(得分:1)
您只是无法使用数据从阵列中删除香蕉。它需要从数组中删除,否则它将继续检查该香蕉是否正在点击info
。
theBananas.removeAt(i); //works with most recent flash players and Adobe air
或
theBananas.splice(1,i); // works with all players and Adobe air versions
两种方法都做同样的事情,但removeAt
更快更容易打字。
当然,您可以将此代码与info.hitTest中的其余块一起放入,例如,它可以直接放在text.text=String(health);
下面
我也会继续使用theBananas[i].parent.removeChild(theBananas[i];
从舞台上移除那根香蕉。当然,这需要在之前完成将其从数组中删除。
希望有所帮助。
编辑:@Cadin的答案是完全正确的。如何让香蕉停止触发hitTest取决于你想要它的外观和工作方式。我的答案只是一种方法。