Player Movement:
A - links D - rechts Leertaste - Springen
This commit is contained in:
+63
-27
@@ -1,38 +1,74 @@
|
||||
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
|
||||
import greenfoot.*;
|
||||
|
||||
/**
|
||||
* Write a description of class Player here.
|
||||
*
|
||||
* @author (your name)
|
||||
* @version (a version number or a date)
|
||||
*/
|
||||
public class Player extends Actor
|
||||
{
|
||||
int velocityY = 0;
|
||||
int speed = 5;
|
||||
private int speed = 4;
|
||||
private int jumpStrength = 12;
|
||||
private int gravity = 1;
|
||||
|
||||
private int verticalSpeed = 0;
|
||||
private boolean onGround = false;
|
||||
|
||||
public void act()
|
||||
{
|
||||
player_movement();
|
||||
moveLeftAndRight();
|
||||
jump();
|
||||
fall();
|
||||
}
|
||||
|
||||
public void player_movement(){
|
||||
if (Greenfoot.isKeyDown("D")){
|
||||
setLocation(getX()+speed, getY());
|
||||
|
||||
// Bewegung nach links und rechts
|
||||
private void moveLeftAndRight()
|
||||
{
|
||||
if (Greenfoot.isKeyDown("a"))
|
||||
{
|
||||
setLocation(getX() - speed, getY());
|
||||
}
|
||||
if (Greenfoot.isKeyDown("A")){
|
||||
setLocation(getX()-speed, getY());
|
||||
|
||||
if (Greenfoot.isKeyDown("d"))
|
||||
{
|
||||
setLocation(getX() + speed, getY());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean onGround(){
|
||||
int height = getImage().getHeight();
|
||||
|
||||
Actor floor = getOneObjectAtOffset(
|
||||
0,
|
||||
height / 2 + 1,
|
||||
Ground.class
|
||||
);
|
||||
|
||||
return floor != null;
|
||||
|
||||
// Springen
|
||||
private void jump()
|
||||
{
|
||||
if (Greenfoot.isKeyDown("space") && onGround)
|
||||
{
|
||||
verticalSpeed = -jumpStrength;
|
||||
onGround = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Schwerkraft und Boden-Kollision
|
||||
private void fall()
|
||||
{
|
||||
verticalSpeed += gravity;
|
||||
|
||||
int newY = getY() + verticalSpeed;
|
||||
|
||||
// Prüfen, ob wir auf Ground landen
|
||||
setLocation(getX(), newY);
|
||||
|
||||
Actor ground = getOneIntersectingObject(Ground.class);
|
||||
|
||||
if (ground != null)
|
||||
{
|
||||
// Wenn wir fallen, stehen wir auf dem Ground
|
||||
if (verticalSpeed >= 0)
|
||||
{
|
||||
int groundTop = ground.getY() - ground.getImage().getHeight() / 2;
|
||||
int characterHalfHeight = getImage().getHeight() / 2;
|
||||
|
||||
setLocation(getX(), groundTop - characterHalfHeight);
|
||||
|
||||
verticalSpeed = 0;
|
||||
onGround = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
onGround = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user