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

2017/5/25

isTouching Error

Archimedes007 Archimedes007

2017/5/25

#
Hello all.. I have a problem when playing this game, when just a few moments play the game directly out such notification Notice java.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. at greenfoot.Actor.failIfNotInWorld(Actor.java:711) at greenfoot.Actor.isTouching(Actor.java:972) at Laser.act(Laser.java:34) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
import greenfoot.*;  


[code]import greenfoot.*;  




public class Laser extends Actor
{
    private boolean toRemove=false;
    private int vx=3;
    public void addedToWorld(World Latar)
    {
        GreenfootImage image=new GreenfootImage(50,10);
        image.setColor(Color.RED);
        image.drawLine(0,5,image.getWidth()-1,5);
        setImage(image);
    }
    
    public void act() 
    {
        if(!toRemove){
            setLocation(getX()+vx,getY());
            Actor actor=getOneIntersectingObject(Alien.class);
            if(actor!=null){
                World myWorld = getWorld();
                ((Alien)actor).Hancur();
                Latar latar = (Latar)myWorld;
                Counter counter = latar.getCounter();
                counter.addScore();
            }
            if(getX()>getWorld().getWidth()+200)toRemove=true;
        }else{
            World myWorld = getWorld();
            getWorld().removeObject(this);

        }
        
       (isTouching(Alien.class))
        {
            getWorld().removeObject(this);
            
            
        }
    }
}
danpost danpost

2017/5/25

#
Change line 40 to:
if (getWorld() != null && isTouching(Alien.class))
Archimedes007 Archimedes007

2017/5/25

#
danpost wrote...
Change line 40 to:
if (getWorld() != null && isTouching(Alien.class))
thanks buddy probblem fixed :D.. Do you have any idea to level in the game? So the game when it reaches score 100 then the game will be finished and change to next level
danpost danpost

2017/5/25

#
Archimedes007 wrote...
Do you have any idea to level in the game? So the game when it reaches score 100 then the game will be finished and change to next level
You will probably need to use something like this:
if ((Latar)getWorld()).getCounter.getScore() >= 100) Greenfoot.setWorld(new NextLevelName());
Best place to put the line is immediately after the 'addScore' line.
Archimedes007 Archimedes007

2017/5/25

#
danpost wrote...
Archimedes007 wrote...
Do you have any idea to level in the game? So the game when it reaches score 100 then the game will be finished and change to next level
You will probably need to use something like this:
if ((Latar)getWorld()).getCounter.getScore() >= 100) Greenfoot.setWorld(new NextLevelName());
Best place to put the line is immediately after the 'addScore' line.
import greenfoot.*; 

public class Counter extends Actor
{
    int score = 0;
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK));
    }
    
    public void addScore()
    {
        
        score++;
        
    }
}

Thats my actor counter, Where can i put that code buddy ?
danpost danpost

2017/5/25

#
Archimedes007 wrote...
Where can i put that code buddy ?
In your Laser class.
Archimedes007 Archimedes007

2017/5/25

#
danpost wrote...
Archimedes007 wrote...
Where can i put that code buddy ?
In your Laser class.
import greenfoot.*;  

public class Laser extends Actor
{
    private boolean toRemove=false;
    private int vx=3;
    public void addedToWorld(World Latar)
    {
        GreenfootImage image=new GreenfootImage(50,10);
        image.setColor(Color.YELLOW);
        image.drawLine(0,5,image.getWidth()-1,5);
        setImage(image);
    }
    
    public void act() 
    {
        if(!toRemove){
            setLocation(getX()+vx,getY());
            Actor actor=getOneIntersectingObject(Alien.class);
            if(actor!=null){
                World myWorld = getWorld();
                ((Alien)actor).Hancur();
                Latar latar = (Latar)myWorld;
                Counter counter = latar.getCounter();
                counter.addScore();
            }
            if(getX()>getWorld().getWidth()+200)toRemove=true;
        }else{
            getWorld().removeObject(this);

        }
        
          if (getWorld() != null && isTouching(Alien.class))
        {
            getWorld().removeObject(this);
            
            
        }
    }
}
thats my laser class buddy, where can i put that code buddy ?
danpost danpost

2017/5/25

#
Archimedes007 wrote...
where can i put that code buddy ?
Like I said before:
Best place to put the line is immediately after the 'addScore' line.
Change 'getScore()' in the line being inserted to 'score'.
You need to login to post a reply.