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

2013/2/14

Move Actor to another world

Qslick Qslick

2013/2/14

#
Hello I have a game with stages in it. when my actor hits the target i want to set his location to another world. For example the actor is in myWorld and i want to set his position to myWorldStage2. How do I do That?
danpost danpost

2013/2/14

#
Add the actor when you create the new world, as in the following:
World ws2=new myWorldStage2();
ws2.addObject(actor, 10, 200);
Greenfoot.setWorld(ws2);
Qslick Qslick

2013/2/15

#
Ok so when the actor reaches the objective I wold put that but I only want to se myWorld2 when that happens not the first one. How would I go about that?
danpost danpost

2013/2/15

#
You need to put that code within an 'if' block; where the condition(s) for changing worlds can be declared.
Qslick Qslick

2013/2/15

#
World ws2=new myWorldStage2(); ws2.addObject(actor, 10, 200); Greenfoot.setWorld(ws2); would this code go in my myWorldStage2() class? or the if block you were talking about.
Qslick Qslick

2013/2/15

#
Also when i run this i get a error that says " can not find symbol - variable player" player is the name of my actor. do i have to define him in my method like int spriteWidth = getImage() I tried this but it says "incompatible types" public void nextLevel() { Actor portal = getOneObjectAtOffset(0, getImage().getHeight()/2, portal.class); if(portal == null) { } else { World world2 = new myWorldStage2(); world2.addObject(player, 10, 200); Greenfoot.setWorld(world2); } }
danpost danpost

2013/2/15

#
Change your 'nextLevel' method to the following:
public void nextLevel()
{
    if (getOneObjectAtOffset(0, getImage().getHeight()/2, portal.class) != null)
    {
        World world2 = new myWorldStage2();
        world2.addObject(this, 10, 200);
        Greenfoot.setWorld(world2);
    }
}
'this' is a keyword that refers to the object that is being acted on.
Qslick Qslick

2013/2/16

#
the logic behinds this seems like it would work but nothing happens when I do this. I have defined portal as such. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class portal extends Actor { public void act() { // Add your action code here. } } I don't have syntax errors. myWorldStage2 does not have any code in it except this import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class myWorldStage2 extends World { public myWorldStage2() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); } } Do you know what could be the problem. let me know if you need more information. Thanks!
danpost danpost

2013/2/16

#
I believe the problem is in the use of 'getOneObjectAtOffset' in this situation. Please clearly describe what the state of the player object must be in with respect to the portal object for the new world to be instantiated.
Qslick Qslick

2013/2/16

#
The player should be in contract with the portal. When the player comes in contact with the portal the portal should not be null thus placing the player in a new world. This is code I used to reset the player when he gets hurt. I was thinking this detection method would work. public void hurt() { Actor spike = getOneObjectAtOffset(0, getImage().getHeight()/2, spike.class); if(spike == null) { } else { setLocation(40 , 408); helpCounter += 1; // will display a help message in another method electricity.play(); //Greenfoot.playSound("Electricity2.wav"); } I don't know if that helps Thanks for your help. I've got to finish this game up for a project.
danpost danpost

2013/2/16

#
What you probably want is 'if (getOneObjectAtOffset(0, 0, portal.class) != null)' in the 'nextLevel' method.
Qslick Qslick

2013/2/16

#
Do you mean like this? public void nextLevel() { if(getOneObjectAtOffset(0, getImage().getHeight()/2, portal2.class)!= null) { World world2 = new myWorldStage2(); world2.addObject(this, 10, 200); Greenfoot.setWorld(world2); } }
danpost danpost

2013/2/16

#
What I meant was:
public void nextLevel()
{
    if (getOneObjectAtOffset(0, 0, portal.class) != null)
    {
        World world2 = new myWorldStage2();
        world2.addObject(this, 10, 200);
        Greenfoot.setWorld(world2);
    }
}
BTW, is it 'portal.class' or 'portal2.class'?
Qslick Qslick

2013/2/16

#
it's portal.class
You need to login to post a reply.