This site requires JavaScript, please enable it in your browser!
Greenfoot back
Stormrage
Stormrage wrote ...

2014/1/14

Help!! How to shoot bullet in every direction

Stormrage Stormrage

2014/1/14

#
The terminal windows said:ava.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. This is the code for object:
if(System.currentTimeMillis()/500%2==0){
            BossBullet[] eb = new BossBullet[12];
            for(int i=0;i<12;i++){
                
                getWorld().addObject( new BossBullet(i+1),getX(),getY());
            }
            
        }    
This is code for bullet:
public class BossBullet extends Boss
{
    public BossBullet(int n){
        act(n);
    }
    /**
     * Act - do whatever the BossBullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act(int direction) 
    {
        switch(direction) {
            case 1 :
            setLocation(this.getX(), this.getY() + 3);
            break;
            case 2 :
            setLocation(getX() + 1, getY()+2);
            break;
            case 3 :
            setLocation(getX()+2, getY() + 1);
            break;
            case 4 :
            setLocation(getX() +3, getY());
            break;
            case 5 :
            setLocation(getX() +2, getY()-1);
            break;
            case 6 :
            setLocation(getX() +1, getY()-2);
            break;
            case 7 :
            setLocation(getX() , getY()-3);
            break;
            case 8 :
            setLocation(getX() -1, getY()-2);
            break;
            case 9 :
            setLocation(getX() -2, getY()-1);
            break;
            case 10 :
            setLocation(getX() -3, getY());
            break;
            case 11:
            setLocation(getX() -2, getY()+1);
            break;
            case 12 :
            setLocation(getX() -1, getY()+2);
            break;
        }
    }    
}
danpost danpost

2014/1/14

#
The main problem is you put a call to your movement method in the constructor of the class. The actor cannot move if it is not in the world...and it cannot be in the world until construction has completed and the world has 'addObject'ed it in. In other words, you cannot call your 'act(int)' method from the constructor. You will have to store the 'n' as an instance field and change your 'act(int)' method into an 'act()' method.
danpost danpost

2014/1/14

#
There is no code in the BossBullet class to remove the bullet when it reaches the edge of the world (the bullets will just accumulate on the edges of the world). Also, a BossBullet is NOT a Boss. A Boss may produce a BossBullet, but that does not make it a Boss object. The boss is created with one blueprint and the bullet is created with a totally different blueprint; therefore, the BossBullet class should not extend the Boss class. In the Boss class, you are creating an array to hold 12 BossBullet objects; yet, nowhere are you using that array (basically, there is no need for the array and that line can be removed).
Stormrage Stormrage

2014/1/16

#
Thanks:) Sorry for the late. First time using this website still don`t know how to using it yet.
You need to login to post a reply.