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

2013/12/5

Shooting in the direction the direction

pr2alede pr2alede

2013/12/5

#
I have a game where a crab shoots ants. Managed to get the crab to shoot the ant but the ant only moves upwards. How do I get the ant to move in the direction the crab is facing, as my crab rotates. Here's the code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ant here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ant extends Animal
{
    private Crab myCrab;
    /**
     * Act - do whatever the Ant wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    
    public Ant(Crab myCrab)
    {
        this.myCrab = myCrab;
    }
    
    public void act() 
    {
    
        int ypos = getY();
        if (ypos > 0) {
            ypos = ypos - 40;
            setLocation(getX(), ypos);
                Actor lobster = getOneIntersectingObject(Lobster.class);
            if (lobster != null) {
                // We've hit an asteroid!
                getWorld().removeObject(lobster);
                getWorld().removeObject(this);
            }
        }
        else {
            // I reached the top of the screen
            getWorld().removeObject(this);
        }
    }    

}
danpost danpost

2013/12/5

#
Use the 'addedToWorld' method to set the rotation of the ant to that of the crab when the ant is added to the world. Then, change the movement code (lines 26 through 29) to using the 'move' method to move.
You need to login to post a reply.