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;
private int vSpeed = 0;
private int acceleration = 2;
private int spriteHeight = getImage().getHeight();
private int spriteWidth = getImage().getWidth();
private int lookForGroundDistance = (int)spriteHeight/2;
private int lookForEdge = (int)spriteWidth/2;
private int speed = 1;
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()
{
checkFall();
move();
if(speed<0)
{
if(animationCount % 4 == 0)
animateLeft();
}
else
{
if(animationCount % 4 == 0)
animateRight();
}
animationCount ++;
}
public void move()
{
Actor ground = getOneObjectAtOffset(lookForEdge, lookForGroundDistance, Platform.class);
if(ground == null)
{
speed *= -1; // Reverses direction
lookForEdge *= -1; // Looks for a negative number
}
else
{
move(speed);
}
}
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++;
}
public void fall()
{
setLocation(getX(), getY() + vSpeed);
if(vSpeed <=9)
{
vSpeed = vSpeed + acceleration;
}
}
public boolean onGround()
{
int spriteHeight = getImage().getHeight();
int yDistance = (int)(spriteHeight/2) + 5;
Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);
if(ground == null)
{
return false;
}
else
{
moveToGround(ground);
return true;
}
}
public void moveToGround(Actor ground)
{
int groundHeight = ground.getImage().getHeight();
int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
setLocation(getX(), newY);
}
public void checkFall()
{
if(onGround())
{
vSpeed = 0;
}
else
{
fall();
}
}
}
Friday, January 17, 2014
Monsters and Platform Blocks
Today we worked on adding a new "Monster" class (see code below) and added sub-classes to the Platform class. We also explored 32x32 pixel blocks for possible use as Platform objects. Here's an "Google Search" for some examples:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment