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

2015/5/17

Re-adding an object when it's been removed

vampire-wannabe vampire-wannabe

2015/5/17

#
import greenfoot.*;

/**
 * Write a description of class Air here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Air extends Aang
{
    /**
     * Act - do whatever the Air wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        move(-3);
        setRotation(90);
        airOffScreen();
        zukoRespawn();
    }
    
    public void airOffScreen()
    {
        Actor Zuko;
        int yCoord = getY();
        if(yCoord == 0)
        {
            getWorld().removeObject(this);
        }
        else
        {
            Actor zuko=getOneIntersectingObject(Zuko.class);
            if(zuko != null)
            {
                World zukoWorld=getWorld();
                zukoWorld.removeObject(zuko);
            }
        }
    }
    
    public void zukoRespawn()
    {
        Actor zuko=getOneIntersectingObject(Zuko.class);
        if(zuko != null)
        {
            int xCoord = Greenfoot.getRandomNumber(300);
            int yCoord = Greenfoot.getRandomNumber(50);
        }
    }
}
azazeel azazeel

2015/5/18

#
what is this ?
danpost danpost

2015/5/18

#
azazeel wrote...
what is this ?
I wonder this myself. This is what I see:
import greenfoot.*;

public class Air extends Aang
{
    public void act() 
    {
        move(-3);
        setRotation(90);
        if (getY() == 0) getWorld().removeObject(this); else removeTouching(Zuko.class);
    }
}
Your line 48 and 49 get some random numbers that are not being used; so line 46 through 50 are meaningless. Besides, the actor would have to come in contact with two Zuko object at one time for line 46 to detect one because you are removing one in the previously called 'airOffScreen' method. My line 9 is your 'airOffScreen' method call replaced with the code within the method (simplified). It could probably be reduced even further to this:
removeTouching(Zuko.class);
because your actor will probably never make it to where 'getY' will return a value of zero (if it does, then it would be in the world a very short time (at most 2 act cycles -- about 1/30th of a second). I say this because the way I see it your actor is moving backwards in a square pattern in a very tight clockwise direction (within a 4x4 cell area).
You need to login to post a reply.