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

2013/9/20

Teleporting Destination uncertainty.

JasonZhu JasonZhu

2013/9/20

#
Hi guys! I am trying to make it so that a destination teleports to the opposite corner of a map and when the player reaches the destination a second time, it teleports back to the original corner it was at and continues the cycle. I tried using an if statement, but that obviously doesnt work. I also tried to use a switch statement, however I couldnt accomplish anything. Does anyone know how to do such a feat?
danpost danpost

2013/9/20

#
You could review the code of my Teleport Demo scenario.
JasonZhu JasonZhu

2013/9/20

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;


public class Player extends Actor
{
    private static final int EAST = 0;
    private static final int SOUTH = 90;
    private static final int WEST = 180;
    private static final int NORTH = 270;

    public Player()
    {
        GreenfootImage endpoint = new GreenfootImage(25,25);
        endpoint.setColor(Color.BLUE);
        endpoint.fill();
        setImage(endpoint);
    }
    
    public boolean canWalk()
    {
        return !facingWall() && !facingEdge();
    }
    
    public boolean facingWall()
    {
        int xOffset=0, yOffset=0;
        switch( getRotation() ) {
            case EAST: xOffset=1; break;
            case SOUTH: yOffset=1; break;
            case WEST: xOffset=-1; break;
            case NORTH: yOffset=-1; break;
        }
        Actor wall = getOneObjectAtOffset(xOffset,yOffset,Wall.class);
        return wall != null;
    }
    
    public void walk()
    {
        int dx=0, dy=0;
        switch( getRotation() ) {
            case EAST: dx=1; break;
            case SOUTH: dy=1; break;
            case WEST: dx=-1; break;
            case NORTH: dy=-1; break;
        }
        setLocation(getX()+dx,getY()+dy);
    }
    
    public boolean facingEdge()
    {
        switch ( getRotation()) {
            case EAST: return getX()==getWorld().getWidth()-1;
            case SOUTH: return getY()==getWorld().getHeight()-1;
            case WEST: return getX()==0;
            case NORTH: return getY()==0;
        }
        return false;
    }
    
        public void act()  
    {  
        turn(-90);  
        while (!canWalk()) turn(90);  
        walk();  
    }
}
   
 
Yikes... That is really complicated to me. Im just a beginner coder. If this helps: The algorithm now works to move the Player from one corner to the opposite side. I want it so that the Target cell teleports to the opposite coner each time the Player touches it; like a forever maze.
danpost danpost

2013/9/20

#
Ok, all you want to do is, when the player is at one location, change its location to another one. Use 'getX' and 'getY', to determine if the player is at the teleport location and use 'setLocation' to move the player back to the other corner.
// add to act method
if (getX() == teleportX && getY() == teleportY) setLocation(originX, originY);
You will have to supply the values for the various named fields.
JasonZhu JasonZhu

2013/9/20

#
It doesnt seem to work... The algorithm makes the Player go from the start to the end. When it reaches the end, I want the new end to be where the player started because eventually, the Player reaches back to the start. I want this to keep looping. Thanks.
danpost danpost

2013/9/20

#
Do this:
// add this instance field to the Player class
private int direction = 1;
// and change your act method to
public void act()
{
    turn (-direction*90);
    if (!facingWall) turn(direction*90);
    walk();
    if (getY() == 0 && (getX() == 0 || getX() == getWorld().getWidth()-1))
    {
        turn(180);
        direction = -direction;
    }
}
This should make the player go from top-left corner to top-right corner and back, continuously.
JasonZhu JasonZhu

2013/9/20

#
if (getX() == 32 && getY() == 0) {
            //set location of target to (0,24)
        }else {
            if (getX() == 0 && getY() == 24) {
                //set location of target to (32,0)
        }
        }
Can I use: By the way, my world is (32,24,25)
JasonZhu JasonZhu

2013/9/20

#
I think youre misunderstanding me... The algorithm works fine. I just want the target to keep teleporting to the opposite corner each time the Player intersects it, as the Player can get from its start and back no problem.
danpost danpost

2013/9/20

#
JasonZhu wrote...
if (getX() == 32 && getY() == 0) {
            //set location of target to (0,24)
        }else {
            if (getX() == 0 && getY() == 24) {
                //set location of target to (32,0)
        }
        }
Can I use: By the way, my world is (32,24,25)
That will not work. 'getX()' will never be 32 and 'getY() will never be 24. Java starts counting at zero and the last x value will be 31 and the last y will be 23. Now that I see you have a 'target' object, I realize you have been going about this all wrong. Do nothing in the Player class (the code for the Player class you gave at the beginning of this discussion thread is good). In the Target class, add an act method as follows:
public void act()
{
    if (getOneIntersectingObject(Player.class) != null)
    {
        if (getX == 0) setLocation(getWorld().getWidth()-1, 0); else setLocation(0, getWorld().getHeight()-1);
    }
}
JasonZhu JasonZhu

2013/9/20

#
I will give it a try, thanks dan.
JasonZhu JasonZhu

2013/9/21

#
Wow, this works... Can you please explain to me the code? It also looks weird because I'm not used to code looking like that. Thanks
danpost danpost

2013/9/21

#
I figured that since it was the target that was moving when it sees the player, the code should go in the Target class. Let me break it down line by line:
public void act()
{
    if (getOneIntersectingObject(Player.class) != null)
    {
        if (getX() == 0)
        {
            setLocation(getWorld.getWidth()-1, 0);
        }
        else
        {
            setLocation(0, getWorld().getHeight()-1);
        }
    }
}
Line 3 determines if the player has reached the target. Line 5 determines if the target is on the left side of the screen. Line 7 moves the target to the right side of the screen. Line 11 moves the target to the left side of the screen. The target is basically saying "If the player has landed on me, then if I am on the left, move me right; else, move me left."
JasonZhu JasonZhu

2013/9/21

#
Thanks for your explanation, we need more people in the world like you! Thanks for all your help, dan!
You need to login to post a reply.