0

In this game I made some cubes fall and you have to avoid them. When you've avoided one, it keeps falling and hits the ground (scoreDetector), so everytime it hits the ground, I get 1 point. The problem is that the animatione of the cube keeps looping (that's what I want) but by doing so the score counter removes the point and keeps adding and removing it everytime the animation of the cube starts.

Code:

var time:int;
var timer:Timer = new Timer(1000,0);
var score:int = 0;

score = 0;
scoreCounter.text = "Score:  " + score;

timer.addEventListener(TimerEvent.TIMER, cubeFall);

timer.start();

function cubeFall(t:TimerEvent) {

time++;

if (time == 3) {
    cube_1.play();
} else if (time == 10) {
    cube_2.play();
}

// Add Score

else if (cube_1.hitTestObject(scoreDetector)) {
score++;
scoreCounter.text = "Score:  " + score;
}
}
4

2 回答 2

1

嗨,您可以使用一个包含命中元素的数组,如下所示:

var time:int;
var timer:Timer = new Timer(1000,0);
var score:int = 0;
var hittedObjects:Array = new Array();
score = 0;
scoreCounter.text = "Score:  " + score;

timer.addEventListener(TimerEvent.TIMER, cubeFall);

timer.start();

function cubeFall(t:TimerEvent) {

time++;

if (time == 3) {
    cube_1.play();
} else if (time == 10) {
    cube_2.play();
}

// Add Score

else if (cube_1.hitTestObject(scoreDetector) && hittedObjects.indexOf(cube_1)>0) {
score++;
scoreCounter.text = "Score:  " + score;
hittedObjects.push(cube_1);
}
}
于 2012-06-03T11:55:14.480 回答
0

这是哈立德的一个解决方案,好主意!:) 但这不是数组的问题。hitTest 不应出现在 else if() 语句中。它应该在它自己的 if() 语句中。其次,得分++;应该是 hitTest if() 语句中的唯一内容。scoreCounter.text = "分数:" + 分数;应该在 if() 语句之外。这是它应该是什么样子。

var time:int;
var timer:Timer = new Timer(1000,0);
var score:int = 0;

score = 0;
scoreCounter.text = "Score:  " + score;

timer.addEventListener(TimerEvent.TIMER, cubeFall);

timer.start();

function cubeFall(t:TimerEvent) {

time++;

if (time == 3) {
    cube_1.play();
} else if (time == 10) {
    cube_2.play();
}

// Add Score

if (cube_1.hitTestObject(scoreDetector)) {
score++;
}
scoreCounter.text = "Score:  " + score;
}
于 2013-05-13T00:03:08.057 回答