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

2013/6/13

Can't get fireball to move

wslade wslade

2013/6/13

#
I am trying to get the fireball to move in a direction specific to which image is currently being used but it won't move. I want it to fire in one direction if the pig image if pointing one way and then fire in the opposite direction if the pig image is pointing the other way. Help!
public class Fireball extends Actor
{
private GreenfootImage image1;
private GreenfootImage image2;


public Fireball()
{
image1 = new GreenfootImage("pigCopy.png");
image2 = new GreenfootImage("pig.png");
public int getY();
public int getX();
}

    /**
     * Act - do whatever the Fireball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    { 
        if (getImage()== image1)
        {
getWorld().addObject(new Fireball(), getX() - 10, getY());
         
    }
     if (getImage()== image2)
        {
          getWorld().addObject(new Fireball(), getX() + 10, getY());
         
    }
        if (getX()>=getWorld().getWidth()-1)
        {
            getWorld().removeObject(this);
        }    
    }    
}
bourne bourne

2013/6/13

#
I'm confused on why the following is in the constructor: public int getY(); public int getX(); Also the Fireball has no code that makes it move. Your code looks like it creates new additional Fireballs in the desired direction instead. Try something like this:
if (getImage() == image1)
    setLocation(getX() - 10, getY());  
else if (getImage() == image2)  
    setLocation(getX() + 10, getY());  
Note: You have a check for when the Fireball gets to the right side of the World, but not also the left side (don't know if that was intentional or not).
bourne bourne

2013/6/13

#
Furthermore, I don't see any call to setImage(GreenfootImage) anywhere. So getImage() will not be one of image1 or image2.
wslade wslade

2013/6/13

#
Well, it doesn't do what I want it to do as it puts the pig image in and I want the fireball. I thought maybe I should move this into into Patty (the pig) as I want it to fire when I hit the space key. I have changed it to this but it still won't work. I have included both Patty and Fireball codes (see below)
public class Patty extends Actor

{
    private int VSpeed = 5;
    private int acceleration = 1;
    private int jumpPower = -10;
    private int canShoot = 0;
    private int speed = 2;
    private GreenfootImage image1;
private GreenfootImage image2;
public Patty()
{
image1 = new GreenfootImage("pigCopy.png");
image2 = new GreenfootImage("pig.png");
setImage(image1) ;

}
    /**
     * Act - do whatever the Patty wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveLeft();
        moveRight();
        checkKeys();
        checkFall();
        fire();
        canShoot = canShoot - 1;
        checkWall();
        
    }    
    
    private void checkKeys()
    {
        if(Greenfoot.isKeyDown("a"))
        {
            setImage("pigCopy.png");
            moveLeft();
        }
        if(Greenfoot.isKeyDown("d"))
        {
            setImage("pig.png");
            moveRight();
        }
        if(Greenfoot.isKeyDown("space"))
        {
            if (onPlatform()||onPlatform2() || onMovingPlatform())
            jump();
        }
    }
    
    public void moveLeft()
    {
       if(Greenfoot.isKeyDown("a"))
       {
           setLocation(getX()-speed, getY());
       }
    }
    
    public void moveRight()
    {
        if(Greenfoot.isKeyDown("d"))
        {
            setLocation(getX()+speed, getY());
        }
    }

    public void fall()
    {
        setLocation(getX(), getY()+VSpeed);
        VSpeed = VSpeed + acceleration;
    }
    
    public void checkFall()
    {
        if (onPlatform() || onPlatform2() || onMovingPlatform())
        {
            VSpeed = 0;
        }
        else
        {
            fall();
        }
    }
    public void checkWall()
    {
        if (onVerticleLine())
        {
            speed = 0;
        }
    }
    
    public boolean onPlatform()
    {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2 , Platform.class);
        return under != null;
    }
    
    public void jump()
    {
        VSpeed = jumpPower;
        fall();
    }
    
    public void fire()
    {
        if(Greenfoot.isKeyDown("w")&&(canShoot <=0))
        {
             if (getImage()== image1)
        {
getWorld().addObject(new Fireball(), getX() - 10, getY());
         
    }
     if (getImage()== image2)
        {
          getWorld().addObject(new Fireball(), getX() + 10, getY());
         
    }
            canShoot=50;
        }
    }
    
    public boolean onPlatform2()
    {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2 , Platform2.class);
        return under != null;
    }
    
    public boolean onMovingPlatform()
    {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2 , MovingPlatform.class);
        return under != null;
    }
    public boolean onVerticleLine()
    {
        Actor beside = getOneObjectAtOffset(getImage().getWidth()/2,0,VerticleLine.class);
        return beside !=null;
    }
}

Fireball code
public class Fireball extends Actor
{



    /**
     * Act - do whatever the Fireball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    { 
       
        if (getX()>=getWorld().getWidth()-1)
        {
            getWorld().removeObject(this);
        }    
    }    
}
danpost danpost

2013/6/13

#
There is nothing in the Fireball class that will cause the object to move; besides that, currently, the direction of movement for any Fireball objects has to be determined.
wslade wslade

2013/6/13

#
Okay. Thanks for the help. I think I have it sorted out. I have created a new Fireball2 class and added a setLocation into it going the other way then created another fire2() method
public class Patty extends Actor

{
    private int VSpeed = 5;
    private int acceleration = 1;
    private int jumpPower = -10;

    private int speed = 2;
    private GreenfootImage image1;
    private GreenfootImage image2;
    private int canShoot = 0;

    public Patty()
    {
        image1 = new GreenfootImage("pigCopy.png");
        image2 = new GreenfootImage("pig.png");
        setImage(image2) ;

    }

    /**
     * Act - do whatever the Patty wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveLeft();
        moveRight();
        checkKeys();
        checkFall();
        fire();
        fire2();
        canShoot--;
        checkWall();

    }    

    private void checkKeys()
    {
        if(Greenfoot.isKeyDown("a"))
        {
            setImage(image1);
            moveLeft();
        }
        if(Greenfoot.isKeyDown("d"))
        {
            setImage(image2);
            moveRight();
        }
        if(Greenfoot.isKeyDown("space"))
        {
            if (onPlatform()||onPlatform2() || onMovingPlatform())
                jump();
        }
    }

    public void moveLeft()
    {
        if(Greenfoot.isKeyDown("a"))
        {
            setLocation(getX()-speed, getY());
        }
    }

    public void moveRight()
    {
        if(Greenfoot.isKeyDown("d"))
        {
            setLocation(getX()+speed, getY());
        }
    }

    public void fall()
    {
        setLocation(getX(), getY()+VSpeed);
        VSpeed = VSpeed + acceleration;
    }

    public void checkFall()
    {
        if (onPlatform() || onPlatform2() || onMovingPlatform())
        {
            VSpeed = 0;
        }
        else
        {
            fall();
        }
    }

    public void checkWall()
    {
        if (onVerticleLine())
        {
            speed = 0;
        }
    }

    public boolean onPlatform()
    {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2 , Platform.class);
        return under != null;
    }

    public void jump()
    {
        VSpeed = jumpPower;
        fall();
    }

    
    public void fire2()
    {
        if(Greenfoot.isKeyDown("w")&&(canShoot <= 0)&&(getImage() == image2) )
        {

            getWorld().addObject(new Fireball2(), getX() , getY());
            canShoot = 25;

        }}

    public void fire()
    {
        if(Greenfoot.isKeyDown("w")&&(canShoot <= 0)&&(getImage() == image1) )
        {

            getWorld().addObject(new Fireball(), getX() , getY());
            canShoot = 25;
 
        }
    }

    public boolean onPlatform2()
    {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2 , Platform2.class);
        return under != null;
    }

    public boolean onMovingPlatform()
    {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2 , MovingPlatform.class);
        return under != null;
    }

    public boolean onVerticleLine()
    {
        Actor beside = getOneObjectAtOffset(getImage().getWidth()/2,0,VerticleLine.class);
        return beside !=null;
    }
}
wslade wslade

2013/6/14

#
I am trying to remove an object. I have a VerticleLine() class and I want to remove it when the Patty() intersects with easy(). I can get easy object to remove but not the verticleline object.
public class easy extends Actor
{private VerticleLine knife;

    /**
     * Act - do whatever the easy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    if  (getOneIntersectingObject(Patty.class) != null)
    {
        Actor Patty = getOneIntersectingObject(Patty.class);
        Greenfoot.playSound("easy.wav"); 
        getWorld().removeObject(this);
       getWorld().removeObject(knife);
    }    
}
}
danpost danpost

2013/6/14

#
Change line 15 to:
knife.getWorld().removeObject(knife);
You removed 'this' from the world on the previous line; so, 'this.getWorld()' returns a 'null' value (the 'this.' part being understood-- like a plus sign in a number).
wslade wslade

2013/6/14

#
When I add the line I still have a problem when I run the program. I get this when the Patty() intersects with easy(). java.lang.NullPointerException at easy.act(easy.java:23) 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) java.lang.NullPointerException at easy.act(easy.java:22) 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

2013/6/14

#
I think you need to explain a little more as to what you are trying to do. It appears you are working with three different objects: a line, a patty, and an easy (whatever an 'easy' is). When the patty and the easy intersect, you want the line and the easy to be removed from the world. Is that correct? and if so, will there only be one VerticleLine object in the world? and will there always be one in the world when an easy object is in the world?
wslade wslade

2013/6/14

#
Yes, that is correct. There is only one VerticleLine object in the world and yes, there will always be one in the world when an easy object is in the world. The VerticleLine object is a barrier in the game. I want this VerticleLine removed when I get to the easy() button. So, basically hitting the easy button will remove the barrier (the VerticleLine) so I can get across the other side. The image I have shows a pig (Patty), easy(the easy button) and VerticleLine (the knife).
danpost danpost

2013/6/14

#
Then try this:
public void act() 
{
    if  (getOneIntersectingObject(Patty.class) != null)
    {
        Greenfoot.playSound("easy.wav"); 
        getWorld().removeObjects(getWorld().getObjects(VerticleLine.class));
        getWorld().removeObject(this);
    }
}
wslade wslade

2013/6/14

#
Awesome! That did the trick.
You need to login to post a reply.