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

2019/5/2

HELP MY ERROR: It says 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:714)

gdrgnxxi gdrgnxxi

2019/5/2

#
//My World: Real Game 

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class RealGame here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class RealGame extends World
{
    HealthBar healthBar = new HealthBar();//access healthbar info from the public healthbar method 
    Counter counter = new Counter();
    /**
     * Constructor for objects of class RealGame.
     * 
     */
    
    int trashCount = 0;
    public RealGame()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
    }
    
    public HealthBar gethealthBar(){
        return healthBar;
    }
    
    public Counter getCounter(){ 
        return counter;
    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(counter, 108,125);

        Back back = new Back();
        addObject(back,75,40);

        Cheese cheese = new Cheese();
        addObject(cheese,404,62);  
        addObject(healthBar,639,50);
        healthBar.setLocation(706,39);
        healthBar.setLocation(702,36);
        Chef chef = new Chef();
        addObject(chef,377,495);
        Trash trash = new Trash();
        addObject(trash,315,56);
           
    }
        int iCounter = 0;
    
    public void act()
    {
       trashCount++; //30 times per second
       if(trashCount>59) //happens 60 times per second
       {    addTrash();
            trashCount = 0;
        }
    }
    public void addTrash(){
            Trash myTrash = new Trash();
            addObject(new Trash(), Greenfoot.getRandomNumber(getWidth()),0);
    }   
}
			
gdrgnxxi gdrgnxxi

2019/5/2

#
//My Actor Trash code where error occured at hitTrash() method 


import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Trash here.
 * 
 * @author (your name) 
 * @version (a version number or Ia date)
 */
public class Trash extends Actor
{
    //HealthBar health = new HealthBar();
    public Trash()
    {
        GreenfootImage image = getImage();
        int myNewHeight = (int)image.getHeight() / 20;
        int myNewWidth = (int)image.getWidth() / 20;
        image.scale(myNewWidth, myNewHeight);
    }
    
    /**
     * Act - do whatever the Trash wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
    {
        fall2();
        getHit();
        hitTrash();

    }

     
     public void fall2() 
    {
        setLocation(getX(), getY() + 1);
    }
    
     public void getHit()
    {
        Actor Knife = getOneObjectAtOffset(0,0, Knife.class);
        if (Knife != null)  getWorld().removeObject(this);
        return; 
    }

       boolean touchingTrash = false;
    public void hitTrash()  
    {         
            Actor Knife = getOneObjectAtOffset(0,0, Knife.class);   
            Actor Trash = getOneObjectAtOffset(0,0, Trash.class);
            if(Trash != null)getWorld();
            {
                if(getY() >= 599){
            World realWorld = getWorld(); 
            RealGame levelone= (RealGame)realWorld; //getting my realword object to the realgame type world, get everything it do
             HealthBar healthBar = levelone.gethealthBar();
                healthBar.loseHealth(1);     
                getWorld().removeObject(this);
                return;
            }else if(isTouching(Knife.class)){
                getWorld().removeObject(Trash);
                return; 
             }else{
                    touchingTrash = true; 
                 
                }
                }
            }
    }
gdrgnxxi gdrgnxxi

2019/5/2

#
//My Knife (the shooter of the actor trash) 

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Knife here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Knife extends Actor
{
    
    public Knife()
    {
        GreenfootImage image = getImage();
        int myNewHeight = (int)image.getHeight() / 3;
        int myNewWidth = (int)image.getWidth() / 3;
        image.scale(myNewWidth, myNewHeight);
    }

    /**
     * Act - do whatever the Knife wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() , getY() - 20);
        
        if(isAtEdge())
        {
           getWorld().removeObject(this); 
        }
        
          Actor Trash = getOneObjectAtOffset(0,0, Trash.class);
            if(Trash != null){
            World realWorld = getWorld(); 
            RealGame levelone= (RealGame)realWorld;
            Counter counter = levelone.getCounter(); //get actual counter from the world
            counter.gainScore();
            
            Knife knife = new Knife();
            getWorld().removeObject(knife); 
            //add a score when hit trash
            //creates variable levelone, takes realWorld and turns it into realgame type

}
}
}
danpost danpost

2019/5/2

#
By the way you are using your return statements, I get the feeling you misunderstand what they do (or, rather, do not do). Yes, they do cause an immediate break out of the method you place them in. No, they do not prevent the act method (or the method that called the method with the return statement in it) from continuing to execute unless they are in the act method, itself (or the calling method). So, if you remove the actor in a method called from the act method, no future method can later be called in the act method requiring the actor be in the world unless you first double-check that it is still in the world. Look at the return statements that you do have. * Line 46 in the Trash class: the return statement is at the end of the method; yet, the method will automatically be returned from because there are no more lines to execute; * Lines 62 and 65 in the Trash class: this is just a variant of line 46. One, and only one, block will be executed in the 'if-else-else' structure and no more lines to execute after that; so, the method will be returned from regardless of your return statements. In your Knife class, you have a line that could potentially remove the actor from the world (line 32) and do not take appropriate action to avoid future method calls that require the actor to be in the world within the same method (line 35 and others).
gdrgnxxi gdrgnxxi

2019/5/2

#
should i remove the return statement from those in the hitTrash method?
danpost danpost

2019/5/3

#
gdrgnxxi wrote...
should i remove the return statement from those in the hitTrash method?
You can remove ALL return statements in the Trash class. But you also need something in the act method to prevent the calling of hitTrash for the case where getHit has the actor removed from the world.
You need to login to post a reply.