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

2016/6/29

spawning

MrPancakes MrPancakes

2016/6/29

#
I have a small question. If I go inside a building and then go out, it spawns at the very beginning. How can I make it spawn at the door of the building?
danpost danpost

2016/6/29

#
MrPancakes wrote...
I have a small question. If I go inside a building and then go out, it spawns at the very beginning. How can I make it spawn at the door of the building?
Where is your code -- World subclass for both inside and outside plus class of character? (maybe of Door also)
MrPancakes MrPancakes

2016/6/30

#
Here is the inside
public class CenterInside extends World
{

    /**
     * Constructor for objects of class CenterInside.
     * 
     */
    public CenterInside()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(280, 150, 1);
        addObject (new menuPokemon(), 140, 150);
        addObject (new redLine(), 222, 149);
        addObject (new redLine(), 46, 149);
        addObject (new redLine(), 9, 53);
        addObject (new redLine(), 271, 53);
        addObject (new redLine(), 138, 6);
        addObject (new PCLobby(), 137, 76);
        addObject (new Ash(), 144, 97);
        
    }
}
I'd like to put the outside in but it is very, very, very long. here is the character:
public class Ash extends SmoothMover
{
    public double moveSpeed = 1.5;
    /**
     * Act - do whatever the Ash wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.isKeyDown ("up") || Greenfoot.isKeyDown ("w"))
        {
            setLocation(getExactX(), getExactY()-moveSpeed);
            setImage("roodkapjeachter.gif");
            if (isTouching(Tree.class))
            {
                setLocation(getExactX(), getExactY()+moveSpeed);
            } 
            if (isTouching(house.class))
            {
                setLocation(getExactX(), getExactY()+moveSpeed);
            }
            if (isTouching(redLine.class))
            {
                setLocation(getExactX(), getExactY()+moveSpeed);
            }
            if (isTouching(Man.class))
            {
                setLocation(getExactX(), getExactY()+moveSpeed);
            }
            if (isTouching(Ant2.class))
            {
                setLocation(getExactX(), getExactY()+moveSpeed);
            }
        }
        if (Greenfoot.isKeyDown ("down") || Greenfoot.isKeyDown ("s"))
        {
            setLocation(getExactX(), getExactY()+moveSpeed);
            setImage("roodkapjevoor.gif");
            if (isTouching(Tree.class))
            {
                setLocation(getExactX(), getExactY()-moveSpeed);
            } 
            if (isTouching(house.class))
            {
                setLocation(getExactX(), getExactY()-moveSpeed);
            }
            if (isTouching(redLine.class))
            {
                setLocation(getExactX(), getExactY()-moveSpeed);
            }
            if (isTouching(Man.class))
            {
                setLocation(getExactX(), getExactY()-moveSpeed);
            }
            if (isTouching(Ant2.class))
            {
                setLocation(getExactX(), getExactY()-moveSpeed);
            }
        }    
        if (Greenfoot.isKeyDown ("left") || Greenfoot.isKeyDown ("a"))
        {
            setLocation(getExactX()-moveSpeed, getExactY());
            setImage("roodkapjelinks.gif");
            if (isTouching(Tree.class))
            {
                setLocation(getExactX()+moveSpeed, getExactY());
            }
            if (isTouching(house.class))
            {
                setLocation(getExactX()+moveSpeed, getExactY());
            }
            if (isTouching(redLine.class))
            {
                setLocation(getExactX()+moveSpeed, getExactY());
            }
            if (isTouching(Man.class))
            {
                setLocation(getExactX()+moveSpeed, getExactY());
            }
            if (isTouching(Ant2.class))
            {
                setLocation(getExactX()+moveSpeed, getExactY());
            }
        }    
        if (Greenfoot.isKeyDown ("right") || Greenfoot.isKeyDown ("d"))
        {
            setLocation(getExactX()+moveSpeed, getExactY());
            setImage("roodkapjerechts.gif"); 
            if (isTouching(Tree.class))
            {
                setLocation(getExactX()-moveSpeed, getExactY());
            } 
            if (isTouching(house.class))
            {
                setLocation(getExactX()-moveSpeed, getExactY());
            }
            if (isTouching(redLine.class))
            {
                setLocation(getExactX()-moveSpeed, getExactY());
            }
            if (isTouching(Man.class))
            {
                setLocation(getExactX()-moveSpeed, getExactY());
            }
            if (isTouching(Ant2.class))
            {
                setLocation(getExactX()-moveSpeed, getExactY());
            }
        }
    }
}
danpost danpost

2016/6/30

#
Where is the code that transfers ash from the outside world to the inside one and back? There should be some condition (maybe collision with a door or location checking) to trigger the world changes somewhere. You made it seem from your first post that a world change was actually happening in both directions, but ash was not properly being placed when returning when going back outside.
MrPancakes MrPancakes

2016/7/4

#
this is the code to let Ash in.
public class Tent extends WorldChanger
{
    /**
     * Act - do whatever the Tent wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (isTouching(Ash.class))
        {
            Greenfoot.setWorld(new CenterInside());
        }    
    }
}
here is the codwe to let him out.
public class menuPokemon extends IntrosAndText
{
    /**
     * Act - do whatever the menuPokemon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (isTouching(Ash.class))
        {
            Greenfoot.setWorld(new MyWorld());
        }
    }    
}
danpost danpost

2016/7/4

#
Okay -- so you are not using the same Ash object in both the inside and outside worlds (that is alright -- just something that is good to know for providing more helpful assistance). You need first to track the state (not just check the state) of the Ash object intersecting the Tent object. So, you can change the Tent class to this:
import greenfoot.*;

public class Tent extends WorldChanger
{
    private boolean touchingAsh; // to track touching state

    public void act() 
    {
        if (touchingAsh != isTouching(Ash.class)) // does touching state change
        {
            touchingAsh = !touchingAsh; // changed state
            if (touchingAsh) // is it just now touching
            {
                CenterInside inside = new CenterInside();
                Greenfoot.setWorld(inside);
            }
        }    
    }
}
Now, the Ash object will only trigger going to a CenterInside world when first determined to be touching a Tent object. In other words, on subsequent act cycles where the Ash object continues to touch a Tent object, no change in worlds will occur. This is important -- so when returning to this same MyWorld world, it does not automatically return to a new CenterInside world again. To return to the same MyWorld world, the CenterInside world will need to receive a reference to the MyWorld world that just triggered its creation; adding a parameter to the CenterInside constructor to allow the world to be passed to it is one way (and probably the best way) to do this.
// change line 8 of CenterInside class to
public menuPokemon(World trigger)
That change just allows the world to be received; now, we need to pass it:
// change line 14 in my Tent class code above to
CenterInside inside = new CenterInside(getWorld());
Now that the MyWorld world is being passed and received, somewhere in the CenterInside world, or in an Actor object in that world, that reference to the MyWorld object needs to be saved (at this point, the referenced world has only made it to the constructor). The best place is in the menuPokemon class (since that is where we will return to the MyWorld world from). So,
/** in the menuPokemon class */
// add instance field
public World myWorld;
The CenterInside constructor now needs to set this field to the world passed to it. Replace line 12 in the CenterInside constructor with the following lines:
menuPokemon menu = new menuPokemon();
menu.myWorld = trigger;
addObject (menu, 140, 150);
Finally, to return to the MyWorld world, change line 11 in the menuPokemon class to:
Greenfoot.setWorld(myWorld);
That should do it. I know this may be a lot for a novice programmer to take in to understand; but, I hope that the step-by-step thought process helped.
You need to login to post a reply.