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

2012/11/21

teleport between pipes

1
2
kentan kentan

2012/11/21

#
Hi, i don't know how to make my smiley to teleport between two pipe classes/image(leftpipe and rightpipe). I want my smiley when it goes/touch leftpipe it will transport to rightpipe and reverse. I would be glad with some help. I have came this far. I will put here my world and my smiley. leftpipe and rightpipe are emty. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class escaper here. * * @author (your name) * @version (a version number or a date) */ public class Smiley extends Actor { /** * Act - do whatever the escaper wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public int x = 1; public int y = 0; public void act() { move(); if(Greenfoot.isKeyDown("left")) { x = -1; y = 0; } if(Greenfoot.isKeyDown("right")) { x = 1; y = 0; } if(Greenfoot.isKeyDown("up") ) { y = -1; x = 0; } if(Greenfoot.isKeyDown("down")) { y = 1; x = 0; } } public void move() { setLocation(getX()+x,getY()+y); } } and import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Back here. * * @author (your name) * @version (a version number or a date) */ public class Back extends World { public Back() { // Create a new world with 20x20 cells with a cell size of 10x10 pixels. super(295, 200, 2); addObject(new Smiley(), 20, 100); addObject(new Rightpipe(), 20, Greenfoot.getRandomNumber(200)); addObject(new Leftpipe(), 280, Greenfoot.getRandomNumber(200)); } }
danpost danpost

2012/11/21

#
You do not show any attempt at coding the teleporting of the object from one pipe to another. What have you tried?
Sl3nDeRm4n Sl3nDeRm4n

2012/11/21

#
You write in you first class with the move() into you act method:
if(getOneIntersectingObject(righttube.class) != null){
setLocation(xL,yL); // xL and yL are the coordinates of your lefttube
}
if(getOneIntersectingObject(lefttube.class) != null){
setLocation(xR,yR); // xR and yR are the coordinates of your righttube
}
danpost danpost

2012/11/21

#
@Sl3nDeRm4n, the problem with that is that as soon as you go from one to the other, you trigger the other to go back to the one, and the actor will flicker between the two pipes. @kentan, you might want to look at the code of my Teleport Demo scenario to get an idea of what you may need to do.
Sl3nDeRm4n Sl3nDeRm4n

2012/11/21

#
ok then he has to increase one of the numbers xL,yL and yR,xR so that the actor does not intersect the other pipe
danpost danpost

2012/11/21

#
@Sl3nDeRm4n, kentan just needs to use an instance boolean field in his Smiley class, called something like 'teleporting'. Then
if (!teleporting && !getIntersectingObjects(leftpipe.class).isEmpty())
{
    teleporting = true;
    // get the rightpipe object
    // move to rightpipe object's location
}
if (!teleporting && !getIntersectingObjects(rightpipe.class).isEmpty())
{
    teleporting = true;
    // get the leftpipe object
    // move to leftpipe object's location
if (teleporting && getIntersectingObjects(leftpipe.class).isEmpty() && getIntersectingObjects(rightpipe.class).isEmpty()) teleporting = false;
That way it will not look so peculiar with the ship showing up outside the pipe's area.
kentan kentan

2012/11/22

#
Ok, danpost, i tried by myself but i just messed it up, so i thought if you could give me some code. I have been trying but i can't make it happen. When i start my game and when my smiley touch one of the pipes, greenfoot stops and some error appear, i will post my code and the error.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class escaper here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Smiley  extends Actor
{
    /**
     * Act - do whatever the escaper wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public int x = 1;
    public int y = 0;
    
    public boolean teleporting = true;
    
    public void act() 
    {
        move();
        checkPipe();
        checkKey();
    }
    
    public void checkKey()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            x = -1;
            y = 0;
        }
        if(Greenfoot.isKeyDown("right"))
        {
              x = 1; 
              y = 0; 
        }
        if(Greenfoot.isKeyDown("up") )          
        {
            y = -1;
            x = 0;
        }
        if(Greenfoot.isKeyDown("down"))
        {
            y = 1;
            x = 0;
        }
    }
    
    public void move()
    {
        setLocation(getX()+x,getY()+y);
    }    
    
    private void checkPipe()
    {
    
        if (!teleporting && !getIntersectingObjects(Leftpipe.class).isEmpty())  
        {       
            teleporting = true;  
            // get the rightpipe object 
            Rightpipe rightpipe = (Rightpipe) getOneIntersectingObject(Rightpipe.class);
            // move to rightpipe object's location  
            setLocation(rightpipe.getX(), rightpipe.getY());
        }  
        if (!teleporting && !getIntersectingObjects(Rightpipe.class).isEmpty())  
        {  
            teleporting = true;  
            // get the leftpipe object  
            Leftpipe leftpipe = (Leftpipe) getOneIntersectingObject(Leftpipe.class);
            // move to leftpipe object's location  
            setLocation(leftpipe.getX(), leftpipe.getY());
        }
        
        if (teleporting && getIntersectingObjects(Leftpipe.class).isEmpty() && getIntersectingObjects(Rightpipe.class).isEmpty()) teleporting = false; 
        
    }
}
java.lang.NullPointerException at Smiley.checkPipe(Smiley.java:66) at Smiley.act(Smiley.java:24) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
danpost danpost

2012/11/22

#
In line 66 in your Smiley class (checkPipe method -- see first 'at' line in trace), you are trying to get the world coordinaes of 'rightPipe', which the error message says is 'null' (that is the only thing in that line that could be). In line 64, you are trying to get a reference to it, but you are not intersecting that pipe when (as line 60 states) you are intersecting the other pipe ('leftpipe'). You could, instead of using 'getOneIntersectingObject', try using the World class method 'getObjects'.
kentan kentan

2012/11/22

#
I don't get it, instead of getOneIntersectingObject I should use getObjects. Could you rewrite my checkPipe method, so you can show how you mean? Thanks!
danpost danpost

2012/11/22

#
All I was saying was that if your actor is intersecting one pipe, then it cannot be intersecting the other one. The code starting at line 61 will only be executed when the condition (in line 60) that you are intersecting the left pipe object. Then, inside that block you are always setting rightpipe to null; because you cannot be intersecting it. Try the following
kentan kentan

2012/11/23

#
thank you, it works! And one more thing, I have now made a Barrier class. I don't want my Smiley to pass through it. How do i do that with this code below me. I haven't made a method for it yet.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class escaper here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Smiley  extends Actor
{
    /**
     * Act - do whatever the escaper wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public int x = 1;
    public int y = 0;
    
    public boolean teleporting = false;
    
    public void act() 
    {
        move();
        checkPipe();
        checkKey();
    }
    
    public void checkKey()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            x = -1;
            y = 0;
        }
        if(Greenfoot.isKeyDown("right"))
        {
              x = 1; 
              y = 0; 
        }
        if(Greenfoot.isKeyDown("up") )          
        {
            y = -1;
            x = 0;
        }
        if(Greenfoot.isKeyDown("down"))
        {
            y = 1;
            x = 0;
        }
    }
    
    public void move()
    {
        setLocation(getX()+x,getY()+y);
    }    
    
    private void checkPipe()
    {       
        if (!teleporting && !getIntersectingObjects(Leftpipe.class).isEmpty())  
        {             
            teleporting = true;  
            // get the rightpipe object 
            Rightpipe rightpipe = (Rightpipe) getWorld().getObjects(Rightpipe.class).get(0);
            // move to rightpipe object's location  
            setLocation(rightpipe.getX(), rightpipe.getY());
        }
        if (!teleporting && !getIntersectingObjects(Rightpipe.class).isEmpty())  
        {             
             teleporting = true;  
             // get the leftpipe object  
             Leftpipe leftpipe = (Leftpipe) getWorld().getObjects(Leftpipe.class).get(0);
             // move to leftpipe object's location  
             setLocation(leftpipe.getX(), leftpipe.getY());
        }
        if (teleporting && getIntersectingObjects(Leftpipe.class).isEmpty() && getIntersectingObjects(Rightpipe.class).isEmpty()) teleporting = false; 
    }
}
danpost danpost

2012/11/23

#
You are wanting to put a condition on your movement. The condition being: if the actor is set to move to a location that (if that location is in the world) does not have a barrier object, then move there.
if(getX()+x>=0 && getX()+x<getWorld().getWidth() &&
   getY()+y>=0 && getY()+y<getWorld().getHeight() &&
   getObjectsAtOffset(x, y, Barrier.class).isEmpty()) 
setLocation(getX()+x, getY()+y);
The code above checks for moves that place the actor outside the world bounds as well as to locations where barrier are present. Now, this may (or may not) work for your scenario (it depends on how your scenario is laid out). Another way to phrase the condition is: if the actor moves into a location that intersects a Barrier object then retract the move immediately.
setLocation(getX()+x, getY()+y);
if(!getIntersectingObjects(Barrier.class).isEmpty()) setLocation(getX()-x, getY()-y);
However, this code does not check for world boundaries; but does not change the behaviour of the actor otherwise.
Sl3nDeRm4n Sl3nDeRm4n

2012/11/23

#
coudnt you handle it witha easy boolean? like this:
boolean teleported = false;
 // then put that into your act method
if(getOneIntersectingObject(rightpipe.class) != null && !teleported){
teleported == true;
setLocation(xOfLeftPipe,yOfLeftPipe);
}
if(getOneIntersectingObject(leftpipe.class) != null && !teleported){
teleported = true;
setLocation(xOfRightPipe,yOfRightPipe);
if(getOneIntersectingObject(rightpioe.class) == null && getOneIntersectingObject(leftpipe.class)){
teleported = false;
}
danpost danpost

2012/11/23

#
@Sl3nDeRm4n, that part is already figured out. kentan is now adding Barrier objects and want to use them to restrict the actor's movements.
Sl3nDeRm4n Sl3nDeRm4n

2012/11/23

#
ah ok
There are more replies on the next page.
1
2