2012-06-09 563 views
1

我正在为我的学校编程项目开发射击游戏,并使用演员的碰撞检测。将演员列表变成演员(Greenfoot)

后来我意识到,我需要使用另一种方法,可以返回一个区域中的所有演员,但唯一的问题是它返回一个列表。我不知道如何使用列表和需要打开列表中的每个元素为演员

下面是一段代码:

MyWorld w = (MyWorld) getWorld(); 
    List<Actor> a = getObjectsInRange(20, null) ; 
    //if it hits the soldier 
    if (a instanceof Soldier) 
    { 
     Soldier s = (Soldier) a; 
     //kill the enemy 
     s.die(); 
     //add 100 score to the enemy 
     w.addScore(100); 
     //if the weapon is not laser 
     if (weaponId != 2) 
     { 
      //getting the world to make the bullet able to fire again 
      w.setBulletLive(false); 
      //remove the bullet 
      getWorld().removeObject(this); 

     } 
    } 
    // if it hits the enemy 
    else if (a instanceof EnemyWeapon) 
    { 
     EnemyWeapon g = (EnemyWeapon) a; 
     //intercept the missile 
     g.intercepted(); 
+0

究竟是一个Actor,它与其他对象有什么关系?你可能想先从(演员actor:演员){core here} – stevedbrown

回答

2

如果你想通过演员名单迭代您可以执行以下操作:

for (Actor actor : listActors) { 
    // here you should put your logic. 
    ... 
}