import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends Actor
{
private int vSpeed = 0;
private int acceleration = 1;
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkFall();
}
public void fall()
{
setLocation(getX(), getY() + vSpeed);
if(vSpeed <=9)
{
vSpeed = vSpeed + acceleration;
}
}
public boolean onGround()
{
int spriteHeight = getImage().getHeight();
int lookForGround = (int)(spriteHeight/2) + 5;
Actor ground = getOneObjectAtOffset(0, lookForGround, 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();
}
}
}
Wednesday, January 8, 2014
Platformer: Getting Started
Today we will build a basic platformer game. We will start with a player and a platform, and we will have the player "fall" until it hits the "ground" (or platform).
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment