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

2018/5/31

Need help returning int from other class

Game4Lols Game4Lols

2018/5/31

#
So I have this player class, who is supposed to return a variable whenever a method is called from another class, and I think I've done it right, but the value never changes from 0. This is the snippet of code here:
public int getCurrentWorld()
    {
        return world;
    }
And in my other class, I have this to call it:
Player plyr = new Player();
public void act() 
    {
        plyrWorld = plyr.getCurrentWorld();
        if (plyrWorld == locationMap)
        {
            Barrel.setTransparency(255);
            setImage(Barrel);
        }
        else
        {
            Barrel.setTransparency(0);
            setImage(Barrel);
        }
    }    
Ideally, the barrel would hide unless both plyrWorld and locationMap are equal, but plyr.getCurrentWorld() always returns 0. What have I done wrong?
danpost danpost

2018/5/31

#
I think there are two misconceptions that you have here. The first is "creating a new player will help gain access to the player in the world" (see line 1). You are having some Actor object create a new Player object. This is a NEW Player object that (1) is not in any world; (2) is not the one in your world; and (3) contains a different world field than the one in the world (probably why it seems to always be zero). The other misconception is that using class name will gain access to objects belonging to that class (objects created from the class). Again, you can create multiple objects from a single class; The class would not know which one you might want to access. Even if you only created one, the class name cannot be used to access it. You have given little background on what kind of set up you are wanting. Apparently, there are multiple world and they are numbered !?? and barrels are supposed to show or hide depending !?? Seems like maybe the entire set up is flawed; but, cannot really say without knowing the main points for the codes.
Game4Lols Game4Lols

2018/6/1

#
I likely have done heaps of stuff wrong, I only started coding this year. Sorry. Basically, I have four "Worlds" that the character can be in, and by walking to the end of the screen, it will change the "world" to a new one by changing the background. I also have the barrel, which I only want to be visible if the player is in the same world as the barrel. The "Worlds" is just top left, bottom right etc. So the int world has a range of 0-3. I want barrel, (Who is given a random world of its own at the start,) to find the players world, and hide if it's world isn't the same.
danpost danpost

2018/6/1

#
Game4Lols wrote...
Basically, I have four "Worlds" that the character can be in, and by walking to the end of the screen, it will change the "world" to a new one by changing the background. I also have the barrel, which I only want to be visible if the player is in the same world as the barrel. The "Worlds" is just top left, bottom right etc. So the int world has a range of 0-3. I want barrel, (Who is given a random world of its own at the start,) to find the players world, and hide if it's world isn't the same.
You are having the barrel constantly check on the condition that the player is in its world. That is wasteful as you only need to check this condition when the player changes worlds (basically, any time you set a background). You can just perform the check and, if right, add the barrel into the world (no need to mess with the transparency of the image).
You need to login to post a reply.