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

2011/10/30

Non accessible variable

kiarocks kiarocks

2011/10/30

#
I have tried to make a certain variable change, but i am having trouble accessing it. Here is the code:
Actor a = getOneObjectAtOffset(535,45,Actor.class);
        if(a != null)
            a.stop(stop);
danpost danpost

2011/10/30

#
You do realize that 'getOneObjectAtOffset(535,45,Actor.class);' will return an Actor that is at the location (this.getX() + 535, this.getY() + 45), if one exists there. Maybe you meant 'getOneObjectAtOffset(535 - getX(), 45 - getY(), Actor.class);' returning an Actor at the absolute location (535, 45), if one exists there.
kiarocks kiarocks

2011/10/30

#
ill see what it does
kiarocks kiarocks

2011/10/30

#
new code:
List b = getWorld().getObjectsAt(535,45,Button.class);  
        if(b != null)  
            b.stop(stop); 
danpost danpost

2011/10/30

#
That should work also; I did not want to go with to 'List' option. But you might have to use '(b.get(0)).stop(stop);', since b is now a 'List' of 'Button's.
kiarocks kiarocks

2011/10/30

#
ahh, thanks
kiarocks kiarocks

2011/10/30

#
reverted to this:
Actor b = getOneObjectAtOffset(535 - getX(),45 - getY(),Button.class);  
        if(b != null)  
            b.stop(stop); // this is highlited
and got this
cannot find symbol - method stop(boolean)
davmac davmac

2011/10/30

#
You declare 'b' to be an 'Actor': Actor b = ... So when you call "b.stop(...)" the compiler looks in the Actor class for the method. There is no such method in the Actor class, so you get an error. You need to declare 'b' to be a 'Button', and use a typecast:
Button b = (Button) getOneObjectAtOffset(535 - getX(),45 - getY(),Button.class);  
        if(b != null)  
            b.stop(stop);
kiarocks kiarocks

2011/10/30

#
ok, that works, but why doesn't this code?
if(Greenfoot.mouseClicked(this) && ((b2 && firsttime) || (b2 && SpaceShips.gotremoved)))
        {
            getWorld().addObject(new SpaceShips(),599,220);
            firsttime = false;
        }
        else if(Greenfoot.mouseClicked(this) && getWorld() instanceof CircutWorld)
        {

            Greenfoot.setWorld(new SpaceWorld(true));
            SpaceShips.stop = false;
        }
        else if(Greenfoot.mouseClicked(this) && stop && !b2)
        {

            Greenfoot.setWorld(new CircutWorld());
        }
kiarocks kiarocks

2011/10/30

#
i repositioned the b.stop(stop); to a different location and it fixed it.
danpost danpost

2011/10/30

#
Try this:
if (Greenfoot.mouseClicked(this))
{
    if (b2 && (firsttime || SpaceShips(gotremoved)))
    {
        getWorld().addObject(new SpaceShips(), 599, 220);
        firsttime = false;
    }
    else if (getWorld() instanceOf CircutWorld)
    {
        Greenfoot.setWorld(new SpaceWorld(true));
        SpaceShips.stop = false;
    }
    else if (stop && !b2)
    {
        Greenfoot.setWorld(new Circutworld());
    }
}
You need to login to post a reply.