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

2019/4/28

I cannot get the wombat to shoot in the direction its facing. It only shoots to the right.

Rawlfish Rawlfish

2019/4/28

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * Wombat. A Wombat is controlled by the up, down, left and right
 * arrow keys. The Wombat can only move north, south, east or west.
 * 
 * Wombats try to find leaves to eat but must go around rocks that get in the way.
 * 
 *  */
public class Wombat extends Actor
{
    // constants that hold the four directions:
    private static final int EAST = 0;
    private static final int WEST = 180;
    private static final int NORTH = 270;
    private static final int SOUTH = 90;

    // keep track of the wombat's current direction
    private int direction;
    private Counter counter;
   
    public void fire()
    {

      //  if(Greenfoot.isKeyDown("space"))
      //  {
            Bullet bullet = new Bullet();
            getWorld().addObject(new Bullet(), this.getX(), this.getY());
            bullet.setRotation( this.getRotation() );
            bullet.move(20);
            /// testing
            
           
            
            ///end  of testing
            
            
            
            
      //  }
    }

    public Wombat(Counter pointCounter)
    {
        this();
        counter = pointCounter;

    }

    /**
     * Constructor in used to review the concept of constructors
     */
    public Wombat()
    {
        direction = EAST;
        setRotation( direction );

    }

    /**
     * Do whatever the wombat likes to to just now.
     */
    public void act()
    {
        checkKeys();
        lookForFood();
       // fire();
    }

    /**
     * Moves the wombat according to which keys are pressed
     */
    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("up")) {
            direction = NORTH;
            setRotation( direction );
            move();
        }
        if (Greenfoot.isKeyDown("right")) {
            direction = EAST;
            setRotation( direction );
            move();
        }
        if (Greenfoot.isKeyDown("down")) {
            direction = SOUTH;
            setRotation( direction );
            move();
        }
        if (Greenfoot.isKeyDown("left")) {
            direction = WEST;
            setRotation( direction );
            move();

        }
        //if(Greenfoot.isKeyDown("space")) {
        //    fire();
        //}
        if ("space".equals(Greenfoot.getKey() )    ) 
        {
            fire();
        }
        
        
        
    }

    /**
     * Search for food
     */
    public void lookForFood()
    {
        Actor leaf = getOneIntersectingObject( Leaf.class );
        if (leaf != null) {
            getWorld().removeObject(leaf);

            counter.setValue(counter.getValue()+1);}
    }

    /**
     * Returns the number of leaves the wombat has eaten
     */
    // public int getLeavesEaten()
    //{
    // return numOfLeaf;//counter.getValue();
    //counter.setValue(counter.getValue()+1);
    //}

    /**
     * Moves the wombat forward one location
     */
    public void move()
    {
        if (canMove()) {
            switch( getRotation() ) {
                case EAST:
                setLocation( getX() + 1, getY() );
                break;
                case SOUTH:
                setLocation( getX(), getY()+1 );
                break;
                case WEST:
                setLocation( getX() - 1, getY() );
                break;
                case NORTH:
                setLocation( getX(), getY() - 1);
                break;
            }
        }
    }

    /**
     * Determines if a Rock is located in front of the Wombat
     */
    public boolean canMove()
    {
        switch( getRotation() ) {
            case EAST:
            return getOneObjectAtOffset(25,0,wallRect.class)==null;
            case SOUTH:
            return getOneObjectAtOffset(0,25,wallRect.class)==null;
            case WEST:
            return getOneObjectAtOffset(-25,0,wallRect.class)==null;
            case NORTH:
            return getOneObjectAtOffset(0,-25,wallRect.class)==null;
        }
        return true;
    }

}
---------------------------------------- Above is Wombat code----------------------------
Rawlfish Rawlfish

2019/4/28

#
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class mazeWorld extends World { private Counter actCounter; public void act() { actCounter.setValue(actCounter.getValue() +1); } /** * Constructor for objects of class mazeWorld. * */ public mazeWorld() { super(800, 800, 1); wallRect block ; actCounter = new Counter(); addObject(actCounter, 698,51); // OUTER RECTANGLE // OUTER RECTANGLE // make a horizontal wall 17 units long for (int i = 3 ; i < 25 ; i++) { block = new wallRect() ; addObject(block, 30 * i, 150) ; } // make a horizontal wall 17 units long for (int i = 3 ; i < 25 ; i++) { block = new wallRect() ; addObject(block, 30 * i, 620) ; } // make a vertical wall 14 unts long for (int i = 5 ; i < 20 ; i++) { block = new wallRect() ; addObject(block, 90, 32 * i) ; } // make a vertical wall 4 unts long for (int i = 5 ; i < 9 ; i++) { block = new wallRect() ; addObject(block, 548, 40 * i) ; } // make a vertical wall 4 unts long for (int i = 15; i < 20 ; i++) { block = new wallRect() ; addObject(block, 548, 30 * i) ; } for (int i = 15; i < 17 ; i++) { block = new wallRect() ; addObject(block, 300, 23 * i) ; } prepare(); } // //public void gameOver() // { // // removeObjects(getObjects(wallRect.class)); // //removeObjects(getObjects(Lizard.class)); // } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Counter counter = new Counter(); addObject(counter, 689,51); Wombat wombat = new Wombat(counter); addObject(wombat, 216, 510); Timer timer = new Timer(); addObject(timer, 604, 54); Leaf leaf = new Leaf(); addObject(leaf, 453, 443); Leaf leaf2 = new Leaf(); addObject(leaf2, 451, 323); Leaf leaf3 = new Leaf(); addObject(leaf3, 424, 246); removeObject(timer); counter.setLocation(602, 50); counter.setLocation(605, 51); leaf.setLocation(194, 237); leaf2.setLocation(374, 508); Leaf leaf4 = new Leaf(); addObject(leaf4, 712, 449); Leaf leaf5 = new Leaf(); addObject(leaf5, 327, 36); Leaf leaf6 = new Leaf(); addObject(leaf6, 66, 62); Leaf leaf7 = new Leaf(); addObject(leaf7, 69, 745); Leaf leaf8 = new Leaf(); addObject(leaf8, 732, 764); Leaf leaf9 = new Leaf(); addObject(leaf9, 355, 739); Leaf leaf10 = new Leaf(); addObject(leaf10, 19, 332); Leaf leaf11 = new Leaf(); addObject(leaf11, 753, 225); } } -------------------------Above is the world----------------------------------------
Rawlfish Rawlfish

2019/4/28

#
import greenfoot.*; /** * Write a description of class Bullet here. * * @author (your name) * @version (a version number or a date) */ public class Bullet extends Actor { /** * Act - do whatever the Bullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(9); } } ---------------this is bullet subclass------------------
danpost danpost

2019/4/28

#
You are creating 2 Bullet objects, adding one to the world and changing the rotation of the other (Wombat class, lines 27 - 29).
Rawlfish Rawlfish

2019/4/28

#
Should I comment a line out? or get rid of the first line?
danpost danpost

2019/4/28

#
Rawlfish wrote...
Should I comment a line out? or get rid of the first line?
Neither, You should not create 2 Bullet objects. Add the Bullet already created into the world instead on adding a new one.
Rawlfish Rawlfish

2019/4/28

#
Im lost D: I cant seem to fix it. Probably simple but I am very new to this.
danpost danpost

2019/4/28

#
Rawlfish wrote...
Im lost D: I cant seem to fix it. Probably simple but I am very new to this.
You create a Bullet object and place a reference to it in a local variable you named bullet on line 27. Use this reference to place it in the world on line 28 (instead of creating another Bullet object and adding it into the world).
Rawlfish Rawlfish

2019/4/28

#
Bullet bullet = new Bullet(); bullet.setRotation (getRotation()); getWorld().addObject( bullet, getX(), getY() ); Worked, thanks for your help. You rock.
Rawlfish Rawlfish

2019/4/28

#
Now I just need to get a life counter and make the leaves give the wombat health back.... lol
You need to login to post a reply.