我正在尝试与数组的新事物,并有一定的困难。我正在尝试创建1个类的多个实例并将它们放入数组中。部分工作阵列/ hitTestobject
我创建的情况下,像这样:
public function creatingitem(e:TimerEvent)
{
amtcreated = Math.ceil(Math.random() * 4);
while (amtcreated >= 1)
{
amtcreated--;
var i:Number = Math.ceil(Math.random() * 3);
switch (i)
{
case 1 :
//Object1
objectnum = 1;
objectwei = 3;
r = new Board(objectnum,objectwei,stagw,stagh);
addChild(r);
fallingitem.push(r);
break;
case 2 :
//Object2
objectnum = 2;
objectwei = 4;
c = new Board(objectnum,objectwei,stagw,stagh);
addChild(c);
fallingitem.push(c);
break;
case 3 :
//Object3
objectnum = 3;
objectwei = 4;
l = new Board(objectnum,objectwei,stagw,stagh);
addChild(l);
fallingitem.push(l);
break;
default :
break;
}
}
}
一旦这些创建他们检查他们是否与主球碰撞:
public function hitcheck(e:Event)
{
for (var v:int = fallingitem.length - 1; v >= 0; v--)
{
if (ball.hitTestObject(fallingitem[v]))
{
trace(fallingitem[v]);
if (fallingitem[v] == r)
{
bonusscore += 100;
fallingitem[v].removeitem();
}
else if (fallingitem[v] == c)
{
bonusscore += 75;
fallingitem[v].removeitem();
}
else if (fallingitem[v] == l)
{
bonusscore += 75;
fallingitem[v].removeitem();
}
trace(bonusscore);
}
}
}
的问题是我看到的每一项击中由于跟踪功能。并非所有实例都符合if条件。作为一个例子,我可以有2个“r”个实例,当我打中两个时1会通过并加入得分,另一个将继续过去。直接跟在hitTestObject之后的跟踪显示我已经被命中和注册了,但我不确定它为什么不添加分数。
谢谢
我感谢您的协助。我用第二个想法来使用我分配给它的objectnum的值。一旦我开始检查这个值,每个对象都会被击中。 –