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

3 days ago

boolean not changing

1
2
bonyay bonyay

23 hours ago

#
danpost wrote...
With the following, no reference is kept:
addObject(new Player(), 100, 365);
which is almost equivalent to, or short for:
Player player = new Player();
addObject(player, 100, 365);
The only difference between these two is that you can do something to player somewhere later within the method currently being executed with the two-liner (since there is a local reference to the player in that method). In the first case, you would have to go through all actors in the world and find the instance that is a Player object to get the reference. On the other hand, if a field reference is kept:
public Player player;

public Dock() {
    super(600, 400, 1);
    player = new Player();  // retains reference
    addObject(player, 300, 200); // wherever
    // etc.
then, the reference is readily available to any actor in the world and any other object that gains a reference to this world. With the following, all worlds will be accessible provided the current world (or an actor within it) maintains a reference to the Dock world:
public Shop shop;
public Room room;

public Dock() {
    super(600, 400, 1);
    shop = new Shop();
    room = new Room();
    prepare();
}
The codes of "new Shop()" and "new Room()" should not be used anywhere else in your project. Nor should "shop =" and "room =" be found anywhere else.
This is all very helpful and digestible, thank you. I did this and compiled, then realized I had some "new Room()" and "new Shop()" code in the buttons, so right now im fighting a whole lot of lag to get through and delete it. As a genuine piece of advice, do you think that I should gut it out of everything to do with shop and upgrades, and just make the goal to raise x amount of money by selling cans? This would just be to have something technically completed and submittable because it's due in literally 5 hours and I have other classes before then. I genuinely want to make this game work, and would continue on the more complicated version once the school year ends (a couple days away!!).
bonyay bonyay

23 hours ago

#
danpost wrote...
You still have two static variables in your Dock class. I am thinking that maybe energyVar is a state of the Player object and should probably be in the Player class. The other variable, moneyVar, seems to be a Player state as well and should probably be moved, as well (although, it might be more questionable).
I mean, everything to do with that has been fine, I think. I'll move them once I can delete the codes I mentioned in my previous reply! :) Alright, I haven't gotten to moving them yet because my attempt at using your reference is now throwing some errors at my previous code. I think I know how to fix them using the knowledge you gave me, but it's rather laggy and I can hardly even start to try. Third edit: I have now moved the code, but cant test it until i fix all errors lol FOURTH edit: ok actually i lied i just compiled it and all the methods that use or affect energy and money work fine in the Player class!
danpost danpost

21 hours ago

#
bonyay wrote...
As a genuine piece of advice, do you think that I should gut it out of everything to do with shop and upgrades, and just make the goal to raise x amount of money by selling cans? This would just be to have something technically completed and submittable because it's due in literally 5 hours and I have other classes before then.
I do not think that should be an issue. Sounds like you are in a beginning programming class where they cannot expect you to do too much. Maybe throwing in a few comments to indicate your intentions to build on it wouldn't hurt.
bonyay bonyay

21 hours ago

#
danpost wrote...
On the other hand, if a field reference is kept:
public Player player;

public Dock() {
    super(600, 400, 1);
    player = new Player();  // retains reference
    addObject(player, 300, 200); // wherever
    // etc.
then, the reference is readily available to any actor in the world and any other object that gains a reference to this world.
If I used this, then do you think I should then have the code for changing worlds in the world and not the actor? Just asking before I actually try it Edit: Wait, this made sense when I made the reply but now I'm thinking about it more and I no longer make sense. Sorry, perhaps I should think it over further
bonyay bonyay

21 hours ago

#
danpost wrote...
I do not think that should be an issue. Sounds like you are in a beginning programming class where they cannot expect you to do too much. Maybe throwing in a few comments to indicate your intentions to build on it wouldn't hurt.
I just was considering it because the main requirement is that everything in it actually works, so if I had features that didn't work, well, I'd lose points. You're right though, it's a beginner course. However, it appears I am more of a beginner than the beginner course anticipated...
bonyay bonyay

21 hours ago

#
danpost wrote...
On the other hand, if a field reference is kept:
public Player player;

public Dock() {
    super(600, 400, 1);
    player = new Player();  // retains reference
    addObject(player, 300, 200); // wherever
    // etc.
then, the reference is readily available to any actor in the world and any other object that gains a reference to this world.
Ok. I've tried this, and it compiles fine, but gives an error that "world cannot be null" when tested
public class RoomButton extends Buttons
{
    public Room room; //i know you didnt say to add this, but it was the only way that it would stop saying "room" is an undeclared variable
    /**
     * Act - do whatever the RoomButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        change();
    }
    public void change()
    {
        if(Greenfoot.mouseClicked(this))
        {
            Greenfoot.setWorld(room);
        }
    }
    public RoomButton()
    {
        
    }
}
public class Dock extends World
{
    

    public int timer=0;
    
  
    public boolean baitNo=true; 
    public boolean rodNo=true;
    
    public boolean baitLow=false;
    public boolean rodLow=false;
    
    public boolean baitMed=false;
    public boolean rodMed=false;
    public boolean baitHigh=false;
    public boolean rodHigh=false;

    public Shop shop;
    public Room room;
    /**
     * Constructor for objects of class Dock.
     * 
     */
    public Dock()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600,400,1);
        setBackground(new GreenfootImage("dockbg.png"));
        prepare();
        shop = new Shop();
        room = new Room();
        RoomButton roomButton = new RoomButton();
        addObject(roomButton,34,305);
        
        Greenfoot.start();
    }
danpost danpost

21 hours ago

#
bonyay wrote...
<< RoomButton Class Codes Omitted >>
public class RoomButton extends Buttons
{
    private Dock dock;
    
    public RoomButton(Dock _dock) {
        dock = _dock;
    }
    
    public void act() {
        change();
    }
    
    public void change() {
        if (Greenfoot.mouseClicked(this)) {
            Greenfoot.setWorld(dock.room);
        }
    }
}
with:
/**   In Dock class  */
RoomButton roomButton = new RoomButton(this);

/**  In Shop class  */
RoomButton roomButton = new RoomButton(dock);
danpost danpost

20 hours ago

#
I am guessing there is probably a better way to do all this, but it would require quite a bit of editing. My idea was, now that you have one, and only one, Room, Dock, and Shop object begin created, static fields to retain references to them would now be suitable. They can be each in their own class to make access to them really easy. You wouldn't need to keep passing the Dock object reference all over the place. Just make sure you create both other objects during the execution of the constructor of the initial world. ... and that all three constructors each assign themselves (keyword: this) to their field.
bonyay bonyay

19 hours ago

#
danpost wrote...
public class RoomButton extends Buttons
{
    private Dock dock;
    
    public RoomButton(Dock _dock) {
        dock = _dock;
    }
    
    public void act() {
        change();
    }
    
    public void change() {
        if (Greenfoot.mouseClicked(this)) {
            Greenfoot.setWorld(dock.room);
        }
    }
}
with:
/**   In Dock class  */
RoomButton roomButton = new RoomButton(this);

/**  In Shop class  */
RoomButton roomButton = new RoomButton(dock);
I've implemented this code, and it works just fine for going from Dock to another World, but once at the other World you cannot change to any other, I'm guessing because you are no longer starting at Dock? I have an idea, but I don't know if it'll mess anything up or if im even writing it correctly/its possible to use in this way:
public class RoomButton extends Buttons
{
    private Dock dock;
    /**
     * Act - do whatever the RoomButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        change();
    }
    public void change()
    {
        if(Greenfoot.mouseClicked(this)) && (World instanceof Dock) //i dont know exactly how to word or use this or if its even correct, ive never used instanceof before
        {
            Greenfoot.setWorld(dock.room);
        }
else if(Greenfoot.mouseClicked(this)) && (World instanceof Shop)
{
 Greenfoot.setWorld(new Shop()); //if its slightly mistyped, ignore it, i dont have it exactly memorized
}
    }
    public RoomButton(Dock _dock)
    {
        dock = _dock;
    }
}
If you know for certain the other way to do this, then you can send it. Thankfully, my instructor has extended the deadline to tomorrow! Also, I uploaded the updated version with the code you've given me. Edit: Hm, I tested out my idea and I'm guessing I'm just using "instanceof" incorrectly. Would it be more like
getWorld()==Dock
Or something along the lines of. Im just trying to express my general idea, not fully correct code. Second edit: ok i figured it out. its (getWorld() instanceof worldname). Lol. Updated code:
public class RoomButton extends Buttons
{
    private Dock dock;
    /**
     * Act - do whatever the RoomButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        change();
    }
    public void change()
    {
        if(Greenfoot.mouseClicked(this) && (getWorld() instanceof Dock))
        {
            Greenfoot.setWorld(dock.room);
        }
        if (Greenfoot.mouseClicked(this) && (getWorld() instanceof Shop))
        {
            Greenfoot.setWorld(new Shop());
        }
    }
    public RoomButton(Dock _dock)
    {
        dock = _dock;
    }
}
This doesnt give any errors, but the changing to Room from Shop doesnt work, both with and without "&& (getWorld() instanceof Shop))" on the second if statement and with and without "else if". I JUST REALIZED I WAS CODING IT TO GO TO THE SHOP WORLD IM VERY TIRED. Now movement between worlds should work.
bonyay bonyay

18 hours ago

#
bonyay wrote...
Now movement between worlds should work.
So now, the switching from Dock to Shop or Room and switching from Room to Shop and vice versa is fully functional, but the problem is still moving from Shop or Room to Dock.
public class ShopButton extends Buttons
{
    private Dock dock;
    /**
     * Act - do whatever the ShopButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        change();
    }
    public void change()
    {
        if(Greenfoot.mouseClicked(this)  && (getWorld() instanceof Dock))
        {
            Greenfoot.setWorld(dock.shop);
        }
        else if (Greenfoot.mouseClicked(this) && (getWorld() instanceof Room))
        {
            Greenfoot.setWorld(new Shop());
        }
    }
    public ShopButton(Dock _dock)
    {
        dock = _dock;
    }
}
Code below does not work:
private Dock dock;
    /**
     * Act - do whatever the DockButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        change();
    }
    public void change()
    {
        if(Greenfoot.mouseClicked(this) && getWorld() instanceof Dock) 
        {
            Greenfoot.setWorld(dock);  
        }
        else if (Greenfoot.mouseClicked(this) && getWorld() instanceof Shop)
        {
            Greenfoot.setWorld(dock.dock);
        }
    }
    public DockButton(Dock _dock)
    {
        dock = _dock;
    }
danpost danpost

18 hours ago

#
bonyay wrote...
public void change()
{
    if(Greenfoot.mouseClicked(this) && getWorld() instanceof Dock) 
    {
        Greenfoot.setWorld(dock);  
    }
    else if (Greenfoot.mouseClicked(this) && getWorld() instanceof Shop)
    {
        Greenfoot.setWorld(dock.dock);
    }
}
It should not matter what world you are in:
public void change() {
    if (Greenfoot.mouseClicked(this)) Greenfoot.setWorld(dock.dock);
}
This should be sufficient in the DockButton class. And the following in the RoomButton class:
public void change() {
    if (Greenfoot.mouseClicked(this)) Greenfoot.setWorld(dock.room);
}
And similarly for ShopButton class:
public void change() {
    if (Greenfoot.mouseCLicked(this)) Greenfoot.setWorld(dock.shop);
}
bonyay bonyay

18 hours ago

#
danpost wrote...
It should not matter what world you are in:
Yes, I had your code written originally but got an error (i cant remember exactly), i think it was something like "this.dock is null" so i added the instanceof code and it worked. I dont really understand how I could apply the same logic to the DockButton, though. I think I get the same error, but i cant check because im traveling home right now. Edit: Alright, Im back home now and it did gives me the errors: "java.lang.NullPointerException: Cannot read field "dock" because "this.dock" is null java.lang.NullPointerException: The given world cannot be null. java.lang.ClassCastException: class Shop cannot be cast to class Dock (Shop and Dock are in unnamed module of loader java.net.URLClassLoader @67e92a" Which I believe are the same ones I got when using the code you gave me in the quoted reply? Honestly, I might have written something incorrectly from a previous reply of yours..? Updated scenario with the new code
bonyay bonyay

14 hours ago

#
Update: In class, my instructor had said that part of my error was from not knowing if DockButton was in the world(or something like that). I didn't really get what he meant/why it would cause an error, but from what it sounded like to me, I had to check if the DockButton actor was in the world before coding anything with it. I added a check if it is, and now I am down to having only one error with two lines: "java.lang.NullPointerException: Cannot read field "dock" because "this.dock" is null at DockButton.change(DockButton.java:28) at DockButton.act(DockButton.java:18)"
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class DockButton here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DockButton extends Buttons
{
    private Dock dock;
    /**
     * Act - do whatever the DockButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        change();
    }
    public void change()
    {
        if(Greenfoot.mouseClicked(this) && getWorld() instanceof Dock) 
        {
            Greenfoot.setWorld(dock);  
        }
        else if (Greenfoot.mouseClicked(this) && getWorld() instanceof Shop && (getWorld()!=null))
        {
            Greenfoot.setWorld(dock.dock);
        }
    }
    public DockButton(Dock _dock)
    {
        dock = _dock;
    }
}
I think if this last error is solved, it will function?
danpost danpost

12 hours ago

#
bonyay wrote...
I think if this last error is solved, it will function?
My mistake. Line 28 should be:
Greenfoot.setWorld(Dock.dock);
My last edit to change methods still stands as it should not matter which world is currently active here. The referencing bypasses that issue. And, it should have nothing to do with actor not in world as act method is not executed when not in world.
bonyay bonyay

12 hours ago

#
danpost wrote...
bonyay wrote...
I think if this last error is solved, it will function?
My mistake. Line 28 should be:
Greenfoot.setWorld(Dock.dock);
My last edit to change methods still stands as it should not matter which world is currently active here. The referencing bypasses that issue. And, it should have nothing to do with actor not in world as act method is not executed when not in world.
Between the time I posted that and your reply, I searched far and wide for a solution and ended up basically revamping a lot of the code. Good news though, everything works! I just have to write out the rest of the upgrades and everything, but I think that if I just repeat my template, then it'll be fine. Thank you so, so much for all your help! (If you want to see what I did, I updated the scenario, also realized I accidentally had it uploaded twice so I took one down lol) (we aren't being graded on our code's efficiency, so honestly, it's the least of my concerns--as long as it works, I'm satisfied for now)
You need to login to post a reply.
1
2