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

2023/6/11

Unable to add object to world

Hitsheep Hitsheep

2023/6/11

#
Can someone explain why the class Rod is unable to add the object fishingBar into the World?
public class Rod extends Actor
{
    
    private int pity = 0;
    private final int hard = 15;
    private final int soft = 8;
    
    private boolean isCast = false, pressSpace = false, caught = false;
    
    
    private String curType;
    private World curWorld = getWorld();
    
    
    private int dL = 3, dE = 2, dR = 1, dC = 1;
    
    
    public void displayBar(){
        Object sprite = curWorld.getObjects(cat.class);
        ((map)curWorld).addObject(new fishingBar(), 200, 200);
        Greenfoot.delay(5);
    }
public void act() 
    {
        // Add your action code here.
        
        if(isCast == false && pressSpace == false && Greenfoot.isKeyDown("c")){
            
            pressSpace = true;
            displayBar();
            Greenfoot.delay(5);
        } 
        
        
        pressSpace = false;
        isCast = false;
        
    }
}
danpost danpost

2023/6/11

#
Hitsheep wrote...
Can someone explain why the class Rod is unable to add the object fishingBar into the World? << Code Omitted >>
Sure. Line 12 declares a field (instance variable member) named curWorld, which is assigned an initial value by calling the getWorld method. Problem is that this assignment is done before the Rod actor is placed into a world, meaning the field is assigned a null value (that is, no World object is assigned to the field). There seems to be no logic to your codes, on a whole. Most of the fields are not used at all. The delay method call will probably not work as you intended. And, if curWorld had a referenced world, there is nothing to prevent fishingBar objects from being spawned repeatedly, one on top of another. Start again, beginning with the following:
public class Rod extends Actor
{
    boolean isCast;
    
    public void act() {
        if (isCast == false && Greenfoot.isKeyDown("c")) {
            getWorld().addObject(new fishingBar(), 200, 200);
            isCast = true;
        }
    }
}
Or, maybe better:
public class Rod extends Actor
{
    public void act() {
        if (getWorld().getObjects(fishingBar.class).isEmpty() && Greenfoot.isKeyDown("c")) {
            getWorld().addObject(new fishingBar(), 200, 200);
        }
    }
}
Hitsheep Hitsheep

2023/6/11

#
danpost wrote...
Hitsheep wrote...
Can someone explain why the class Rod is unable to add the object fishingBar into the World? << Code Omitted >>
Sure. Line 12 declares a field (instance variable member) named curWorld, which is assigned an initial value by calling the getWorld method. Problem is that this assignment is done before the Rod actor is placed into a world, meaning the field is assigned a null value (that is, no World object is assigned to the field). There seems to be no logic to your codes, on a whole. Most of the fields are not used at all. The delay method call will probably not work as you intended. And, if curWorld had a referenced world, there is nothing to prevent fishingBar objects from being spawned repeatedly, one on top of another. Start again, beginning with the following:
public class Rod extends Actor
{
    boolean isCast;
    
    public void act() {
        if (isCast == false && Greenfoot.isKeyDown("c")) {
            getWorld().addObject(new fishingBar(), 200, 200);
            isCast = true;
        }
    }
}
This didn't work for me for some reason, but it's fine I found the solution. Btw, the solution was to add a Rod class into the game which I never did for some reason.
You need to login to post a reply.