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

2012/2/22

Making things happed when in a specific world

tallyaka tallyaka

2012/2/22

#
I want to make it so that my actor switches worlds when he goes to the edge of the screen but I can't figure out how to make him go to a different world depending on what world he's in. I tried using the getWorld() command but I couldn't figure out how to make it work.
davmac davmac

2012/2/23

#
tallyaka, please post the code you tried and then we can help you to fix it.
tallyaka tallyaka

2012/2/24

#
    private int x;
    private int y;
    public void checkWorldChange()
    {
        if (atWorldEdgeR())
        {
            if (getWorld() == Greece1)
            {
                getX() = x;
                getY() = y;
                Greenfoot.setWorld(Greece2);
                getWord().addobject(this, x, y);
            }
        }
    }
}
this is using an edditted code from mover which is as follows:
    public boolean atWorldEdgeR()
    {
        if(getX() > getWorld().getWidth() - 5)
            return true;
        else
            return false;
    }
danpost danpost

2012/2/24

#
I have not done too much with working with different worlds, but I do know the use of '==' is not a good idea with worlds. Use the 'isInstanceOf(Object object)' method instead.
if (Greece1.isInstanceOf(getWorld()))
. I think I have them in the correct order.
davmac davmac

2012/2/24

#
Are Greece1 and Greece2 world classes, or world objects? Do you get a compiler error - if so, on which line, and what does it say? Danpost: I'm dubious of the code you posted. Perhaps you meant:
    if (Greece1.class.isInstance(getWorld()))
This assumes that Greece1 is a class, of course. You could just as well do:
    if (getWorld() instanceof Greece1)
tallyaka tallyaka

2012/2/25

#
    public void checkWorldChange()  
    {  
        if (atWorldEdgeR())  
        {  
            if (getWorld() instanceof Geece1)  
            {  
                getX() = x;  
                getY() = y;  
                Greenfoot.setWorld(Greece2);  
                getWord().addobject(this, x, y);  
            }  
        }  
    }  
with this it says "cannot find symbol - class Greece2. It definitely exists and is a seperate world.
public void checkWorldChange()  
{  
    if (atWorldEdgeR())  
    {  
        if (getWorld() == Greece1)  
        {  
            getX() = x;  
            getY() = y;  
            Greenfoot.setWorld(Greece2);  
            getWord().addobject(this, x, y);  
        }  
    }  
}
and for this it says the in cannot find the variable Greece1
Duta Duta

2012/2/25

#
This *should* work. I haven't tested it, just coded it into the reply box.
//All of the following code is assuming Greece1 and Greece2 are classes.

//First you should change checkWorldChange() to this:
public void checkWorldChange()
{
	if(atWorldEdgeR() && getWorld() instanceof Greece1)
		Greenfoot.setWorld(new Greece2(this, getX(), getY()));
}

//You should then go to Greece2.class and add in the following constructor:
public Greece2(Actor a, int x, int y)
{
	this();
	addObject(a, x, y);
}
davmac davmac

2012/2/25

#
with this it says "cannot find symbol - class Greece2. It definitely exists and is a seperate world.
But is it a class, or a reference? The Greenfoot.setWorld() method requires a world instance as a parameter. Try replacing: Greenfoot.setWorld(Greece2); with: Greenfoot.setWorld(new Greece2()); (Although, the error message doesn't really make sense - are you sure that's what it says with the code you posted?)
tallyaka tallyaka

2012/2/26

#
OK, I think it's all working now except the
if (Greece1.isInstanceOf(getWorld()))  
part. That's my problem.
Duta Duta

2012/2/26

#
Did you notice my post...?
davmac davmac

2012/2/26

#
Or mine, from 2 days ago? if (Greece1.class.isInstance(getWorld()))
Duta Duta

2012/2/26

#
davmac wrote...
Or mine, from 2 days ago? if (Greece1.class.isInstance(getWorld()))
Fair enough - I'm just going to leave this thread alone now: he's got multiple answers, he just needs to see that they're there
tallyaka tallyaka

2012/2/27

#
    public void checkWorldChange()  
    {  
        if(atWorldEdgeR() && Greece1.class.isInstance(getWorld()))
        {
            Greenfoot.setWorld(new Greece2(this, getX(), getY()));
        }
    } 
That's my code, and it compiles, but it doesn't work. If I go to the edge of the world I don't switch worlds :( Just FYI I also have the World code that Duta posted earlier
tallyaka tallyaka

2012/2/27

#
Nevermind! I got it to work! Thanks so much for all your help! I really appreciate it!
You need to login to post a reply.