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

2011/11/20

Public Variables, we meet again

1
2
darkmist255 darkmist255

2011/11/20

#
I'm adding to my Asteroid game to make them move faster the more you destroy. Okay, once again I am at a stage where I think I need to use a public variable but am having no luck. Right now what I'm using to keep track of my score is I have a persistent actor with the following methods in it:
    public void addDestroyed()
    {
        destroyedCount = (destroyedCount + 1);
    }
    
    public double getDestroyedCount()
    {
        return destroyedCount;
    }
However, based on the results from running the program, this doesn't seem to be working. Within the asteroid class:
    Castle castle = new Castle();
    private int moveSpeed = (int)(Math.sqrt(castle.getDestroyedCount()));

    public void act() 
    {
       if(moveSpeed == 0)
    {
        moveSpeed = 1;
    }
       move(moveSpeed);
The getDestroyedCount() seems to be having no effect, the speed is just staying at 1. Is this because castle is new Castle()? If it's new, doesn't that mean the value would be 0? How can I reference the castle that already exists. If I'm over-complicating this, please inform me of a more straightforward way to do public variables.
davmac davmac

2011/11/20

#
Is this because castle is new Castle()?
Yes, because you need to access the value from a specific Castle, creating a new one is not the right solution.
If it's new, doesn't that mean the value would be 0?
It should probably mean that, although I can't see enough of your code to be sure. Perhaps you're initialising the variable to a non-zero value, or perhaps your Castle constructor calls addDestroyed() or causes it to be called.
How can I reference the castle that already exists.
See the tutorial here , for starters.
darkmist255 darkmist255

2011/11/21

#
Thanks, and yes the variable starts with 0 value.
darkmist255 darkmist255

2011/11/21

#
Okay, so now I have a new problem... One I just recently got rid of by fixing my references, however this time the references are correct (I could be wrong, but I've 6x checked). I am getting the "non-static method cannot be referenced from a static context" error again. This happens only when I call a method that is in the SpaceWorld class. Are the Actors unable to access the Worlds?
bourne bourne

2011/11/21

#
I don't know the exact context of what the problem is. But within an Actor, calling getWorld() will return a World and not a SpaceWorld. So to obtain access to SpaceWorld methods you would need to cast it to a SpaceWorld. ((SpaceWorld)getWorld()).someMethod();
kiarocks kiarocks

2011/11/21

#
i usually make a variable
World sw = ((SpaceWorld)getWorld());
.
.
.
public void act()
{
//code here
sw = ((SpaceWorld)getWorld());
sw.someMethod();
//more code
}
darkmist255 darkmist255

2011/11/21

#
I'm gonna change that to getWorld() tomorrow :D. Now one question, you both used ((SpaceWorld)getWorld()). Is there a difference between that and just (getWorld())?
bourne bourne

2011/11/21

#
Like I said getWorld() returns a World (that which the Actor is in), SpaceWorld is a World but a more specific World. You cast it as a SpaceWorld cause you know as the programmer that it is a safe to do so (in that it will in fact be a SpaceWorld). When you cast it as a SpaceWorld you then can treat it as such.
darkmist255 darkmist255

2011/11/21

#
Ok so changed that, this is my code now: Bullet class:
    Castle castle = new Castle();
    World sw = ((SpaceWorld)getWorld());

    public void checkDestroy()
    {
        if(used == 0){
        Asteroid asteroid = (Asteroid)getOneIntersectingObject (Asteroid.class);
        if(asteroid != null) {
            Castle castle = sw.getCastle();
            castle.addDestroyed();
            getWorld().removeObject(asteroid);
            used = 1;
        }
    }
    }
Then in the SpaceWorld class:
    private void prepare()
    {
        Castle castle = new Castle();
        addObject(castle, 300, 399);
    }

    public Castle getCastle()
    {
        return castle;
    }
But in the Bullet class it doesn't like sw.getCastle(); and says "cannot find symbol - method getCastle()"
kiarocks kiarocks

2011/11/22

#
I dont know, but keep doing
sw = ((SpaceWorld)getWorld());
in the act method.Also, new castle() is not the right way to do that variable
darkmist255 darkmist255

2011/11/22

#
Really? What is the right way?
kiarocks kiarocks

2011/11/22

#
Castle castle = ((Castle))getWorld().getObjects(Castle.class))); P.S.: Not sure if parentheses are needed
davmac davmac

2011/11/22

#
darkmist255, the problem is in this line: World sw = ((SpaceWorld)getWorld()); You are using the getWorld() method, which returns a World. Because you know it's a SpaceWorld, you then cast it to SpaceWorld. However, you then store it back into a variable whose type is again World. When the compiler gets to this line: Castle castle = sw.getCastle(); ... it knows that "sw" is a variable of type World, so it looks in the World class for the "getCastle()" method - of course, it doesn't exist there, so you get a compilation error. To fix it just change: World sw = ((SpaceWorld)getWorld()); To: SpaceWorld sw = (SpaceWorld)getWorld(); This will change the type of the "sw" variable from World to SpaceWorld. (I also got rid of the unnecessary pair of ()s, but you can leave them if you prefer).
davmac davmac

2011/11/22

#
Another problem. In you SpaceWorld's prepare() method:
    private void prepare()
    {
        Castle castle = new Castle();
        addObject(castle, 300, 399);
    }
... you are creating a local variable called castle, instead of assigning to the instance variable (which is probably what you intended). Most likely you can change it to:
    private void prepare()
    {
        castle = new Castle();
        addObject(castle, 300, 399);
    }
darkmist255 darkmist255

2011/11/23

#
Ooh, will try, will try.
There are more replies on the next page.
1
2