So Basically I need help creating making my Actor/Mario stop when it hits a wall/Ground, I need Mario to stop no matter what axis he runs into it. I have Done many methods but none seem to work.
I have no code for the Ground witch is named BlockStrip.
Here is Mario's code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Mario here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Mario extends Actor
{
private int moveSpeed = 2;
private int vSpeed = 2;
private int acceleration = 1/3;
private int apexTimer;
private int GRAVITY = 1;
static final int JUMP_FORCE = 12;
int xSpeed = 4;
int ySpeed = 0;
/**
* Act - do whatever the Mario wants to do. This method is called whenever
*/
public void act()
{
checkKeys();
moveHorizontal();
moveVertically();
}
public void moveHorizontal()
{
int worldWidth = getWorld().getWidth();
int myWidth = getImage().getWidth();
int dx = 0;
if(Greenfoot.isKeyDown("a")) dx--;
if(Greenfoot.isKeyDown("d")) dx++;
setLocation(getX()+dx*xSpeed, getY());
}
private void moveVertically()
{
int worldHeight=getWorld().getHeight();
int myHeight=getImage().getHeight();
boolean onGround = false;
ySpeed+=GRAVITY;
setLocation(getX(),getY()+ySpeed);
if(getY() > worldHeight-myHeight/2)
{
ySpeed = 0;
onGround = true;
}
if(onGround && Greenfoot.isKeyDown("space"))
{
ySpeed =-JUMP_FORCE;
}
}
private void checkKeys()
{
{
{
if (Greenfoot.isKeyDown ("right") || Greenfoot.isKeyDown ("d"))
{
setImage("Mario.png");
}
{
if (Greenfoot.isKeyDown ("left") || Greenfoot.isKeyDown ("a"))
{
setImage("MarioBack.png");
}
}
}
}
}
}
