在我的 Main 类中,我有一个 Enemy 对象数组,在我的 Player 类中,我有一个 Bullet 对象数组。在其中的某个地方,我需要一种方法来测试任何玩家子弹和敌人之间的碰撞,当检测到碰撞时,调用 Bullet 的 destroyBullet() 方法和 Enemy 的 destroyEnemy() 方法。
如果两个数组都在同一个类中,我可能会弄清楚如何做到这一点,但否则我很难过。
在我的 Main 类中,我有一个 Enemy 对象数组,在我的 Player 类中,我有一个 Bullet 对象数组。在其中的某个地方,我需要一种方法来测试任何玩家子弹和敌人之间的碰撞,当检测到碰撞时,调用 Bullet 的 destroyBullet() 方法和 Enemy 的 destroyEnemy() 方法。
如果两个数组都在同一个类中,我可能会弄清楚如何做到这一点,但否则我很难过。
I see no reason to not also hold your bullets array in your main class, but if you want to do this as you have it you'll need public access to Player.bullets
This should work from your main class. You will want to put this inside of a checkCollisions function that runs in your game loop.
//loop through bullets first, because if no bullets are fired, no point in checking
for each(var bullet:Bullet in player.bullets) {
for each(var enemy:Enemy in enemies) {
if(bullet.hitTestObect(enemy)) {
bullet.destroyBullet();
enemy.destroyEnemy();
}
}
}
Keep in mind that destroying a bullet or enemy will involve more than removing the sprite and cleanup. You'll also need to splice the arrays that each object is stored in.