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

2021/6/23

Big problems with Actor not in World

Turbo_Thorsten Turbo_Thorsten

2021/6/23

#
My code always leads to problems with actor not in World. What's wrong with it?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tumor extends Actor
{
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int a;
    int b = 1;
    int timer = 300;
    boolean badtumor = false;
    private GreenfootImage GoodTumor;
    private GreenfootImage BadTumor;
    public void act() 
    {
       GoodTumor = new GreenfootImage("GoodTumor.png");
       BadTumor = new GreenfootImage("BadTumor.png");
        if (Tutorial.tutorial == false)
        {
        if (MyWorld.freezeTimer > 0) return;
        else
        {
        if( isAtEdge() ){
            turn(17);
        }
        if( isTouching(Border.class) ){
            turn(17);
        }
        if (Greenfoot.getRandomNumber(50) <1){
            a = Greenfoot.getRandomNumber(91)-45;
            turn(a);
        }
        move(b);
        }
        if (timer > 0)
        {
         timer--; 
         if(timer == 0)
         {
          badtumor = !badtumor;
          timer = timer + 300;
         }
        }
        if (badtumor == true)
        {
           setImage(BadTumor); 
        }
        else
        {
           setImage(GoodTumor); 
        }
       }
       lookForBullet();
       lookForPlayer();
    }  
    public void lookForPlayer() {
        if (MyWorld.life == 0)
        {
         removeTouching (Player.class);  
        }
        if (isTouching(Player.class))
        {
         if(MyWorld.life > 0)
         {
             MyWorld.life--;
             getWorld().removeObject(this);
        }
        }
    }
    public void lookForBullet()
    {
     if (isTouching(Bullet.class))
        {
            if (badtumor == false)
            {
                removeTouching (Tumor.class);  
                getWorld().removeObject(this);
                Bullet.enemieskilled++;
                if (Bacteria.bacteria > 2)
                {
                    Bacteria.bacteria--;
                }
                if (Virus.virus > 3)
                {
                    Virus.virus--;
                }
            }
            else if (badtumor == true)
            {
                removeTouching (Tumor.class);  
                getWorld().removeObject(this);
                Bullet.enemieskilled++;
                if (Bacteria.bacteria < 4)
                {
                    Bacteria.bacteria++;
                }
                if (Virus.virus < 5)
                {
                    Virus.virus++;
                }
            }
     }
    }
}
Gbasire Gbasire

2021/6/23

#
at line 84 and 97, put getWorld().removeObject(this) at the end of your methods, because otherwise it will first delete your object and then try to do the rest of the method without success
Turbo_Thorsten Turbo_Thorsten

2021/6/23

#
And why is this not working?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tumor extends Actor
{
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int a;
    int b = 1;
    int timer = 300;
    boolean badtumor = false;
    private GreenfootImage GoodTumor;
    private GreenfootImage BadTumor;
    public void act() 
    {
       GoodTumor = new GreenfootImage("GoodTumor.png");
       BadTumor = new GreenfootImage("BadTumor.png");
       if (Tutorial.tutorial == false)
        {
        if (MyWorld.freezeTimer > 0) return;
        else
        {
        if( isAtEdge() ){
            turn(17);
        }
        if( isTouching(Border.class) ){
            turn(17);
        }
        if (Greenfoot.getRandomNumber(50) <1){
            a = Greenfoot.getRandomNumber(91)-45;
            turn(a);
        }
        move(b);
        }
        if (timer > 0)
        {
         timer--; 
         if(timer == 0)
         {
          badtumor = !badtumor;
          timer = timer + 300;
         }
        }
        if (badtumor == true)
        {
           setImage(BadTumor); 
        }
        else
        {
           setImage(GoodTumor); 
        }
       }
       lookForBullet();
       lookForPlayer();
    }  
    public void lookForPlayer() {
        if (MyWorld.life == 0)
        {
         removeTouching (Player.class);  
        }
        if (isTouching(Player.class))
        {
         if(MyWorld.life > 0)
         {
             MyWorld.life--;
             getWorld().removeObject(this);
        }
        }
    }
    public void lookForBullet()
    {
            if (badtumor == false)
            {
                removeTouching (Bullet.class); 
                Bullet.enemieskilled++;
                if (Bacteria.bacteria > 2)
                {
                    Bacteria.bacteria--;
                }
                if (Virus.virus > 3)
                {
                    Virus.virus--;
                }
            }
            else if (badtumor == true)
            {
                removeTouching (Bullet.class);
                Bullet.enemieskilled++;
                if (Bacteria.bacteria < 4)
                {
                    Bacteria.bacteria++;
                }
                if (Virus.virus < 5)
                {
                    Virus.virus++;
                }
     }
     getWorld().removeObject(this);
    }
}
Gbasire Gbasire

2021/6/23

#
what error comes out of the terminal when the game crashes ?
Turbo_Thorsten Turbo_Thorsten

2021/6/23

#
So I kind fixed it but I really don't know what's going on. I'll let you know if anything weird happens
danpost danpost

2021/6/23

#
Line 105 is poorly placed. It will unconditionally remove the tumor from the world once the scenario begins running (or when put in the world, if scenario is already running). Also, I do not see the following anywhere:
if (isTouching(Bullet.class))
So, how do you know if a bullet has ever been touched?
Turbo_Thorsten Turbo_Thorsten

2021/6/24

#
Ok so now I got the problem again. What's wrong with my code?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tumor extends Actor
{
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int a;
    int b = 1;
    int timer = 300;
    boolean badtumor = false;
    private GreenfootImage GoodTumor;
    private GreenfootImage BadTumor;
    public void act() 
    {
       GoodTumor = new GreenfootImage("GoodTumor.png");
       BadTumor = new GreenfootImage("BadTumor.png");
        if (MyWorld.freezeTimer > 0) 
        {
            lookForBullet();
            lookForPlayer();
            return;
        }
        else
        {
        if( isAtEdge() ){
            turn(17);
        }
        if( isTouching(Border.class) ){
            turn(17);
        }
        if (Greenfoot.getRandomNumber(50) <1){
            a = Greenfoot.getRandomNumber(91)-45;
            turn(a);
        }
        move(b);
        lookForBullet();
        lookForPlayer();
        }
        if (timer > 0)
        {
         timer--; 
         if(timer == 0)
         {
          badtumor = !badtumor;
          timer = timer + 300;
         }
        }
        if (badtumor == true)
        {
           setImage(BadTumor); 
        }
        else
        {
           setImage(GoodTumor); 
        }
       
    }  
    public void lookForPlayer() {
        if (MyWorld.life == 0)
        {
         removeTouching (Player.class);  
        }
        if (isTouching(Player.class))
        {
         if(MyWorld.life > 0)
         {
             MyWorld.life--;
             getWorld().removeObject(this);
        }
    }
    }
    public void lookForBullet()
    {
       if (isTouching(Bullet.class))
        {
           if (badtumor == false)
            {
                Bullet.enemieskilled++;
                if (Bacteria.bacteria > 2)
                {
                    Bacteria.bacteria--;
                }
                if (Virus.virus > 3)
                {
                    Virus.virus--;
                }
            }
            else if (badtumor == true)
            {
                if (Bacteria.bacteria < 3)
                {
                    Bacteria.bacteria++;
                }
                if (Virus.virus < 4)
                {
                    Virus.virus++;
                } 
            }
            Bullet.enemieskilled++;
            removeTouching (Bullet.class);
            getWorld().removeObject(this);
        } 
    }
}
danpost danpost

2021/6/24

#
Turbo_Thorsten wrote...
Ok so now I got the problem again. What's wrong with my code?
If lookForBullet removes tumor from world, lookForPlayer, which requires the tumor to be in world, fails. Add as first line in lookForPlayer:
if (getWorld() == null) return;
You need to login to post a reply.