Friday, January 17, 2014

Platformer: Platform Blocks and Monsters

To begin with, we're going to add some new subclasses to our Platform class.  I created one called Wall, one called Platform2, and one called Block -- but you can choose your own.  I then assigned them different images.  What this does is it will allow us to add different world objects that have the same properties as a Platform object -- meaning you can jump on them but not walk through them.
Next, we're going to add a "Monster" class.  Obviously you can call yours whatever you would like, but it will be up to you to follow the tutorial.  Here's a beginning code for our Monster class -- which should look similar to the other classes we have created:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Monster here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Monster extends Actor
{
    int frame = 0;
    private int animationCount;
    
    GreenfootImage monster1l = new GreenfootImage("monster1l.png");
    GreenfootImage monster2l = new GreenfootImage("monster2l.png");
    GreenfootImage monster3l = new GreenfootImage("monster3l.png");
    GreenfootImage monster4l = new GreenfootImage("monster4l.png");
    
    GreenfootImage monster1r = new GreenfootImage("monster1r.png");
    GreenfootImage monster2r = new GreenfootImage("monster2r.png");
    GreenfootImage monster3r = new GreenfootImage("monster3r.png");
    GreenfootImage monster4r = new GreenfootImage("monster4r.png");
    
    /**
     * Act - do whatever the Monster wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        animationCount ++;
    }    
    
    public void animateLeft()
    {
        if(frame == 0)
        {
            setImage(monster1l);
        }
        if(frame == 1)
        {
            setImage(monster2l);
        }
        if(frame == 2)
        {
            setImage(monster3l);
        }
        if(frame == 3)
        {
            setImage(monster4l);
            frame = 0;
        }
        frame++;
    }
    
        public void animateRight()
    {
        if(frame == 0)
        {
            setImage(monster1r);
        }
        if(frame == 1)
        {
            setImage(monster2r);
        }
        if(frame == 2)
        {
            setImage(monster3r);
        }
        if(frame == 3)
        {
            setImage(monster4r);
            frame = 0;
        }
        frame++;
    }
}
Now we will begin editing the code by copying chunks of our Player class to recycle.

No comments:

Post a Comment