¿ques♫ wrote...
I tried this, but it doesnt seem to work:
<< Code Omitted >>
I added the else ifs ^ above to prevent Mario from teleporting to the top of objects but this doesn't work.
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
{
static final int gravity = 2; //force of gravity
static final int jumpAbility = 30; //the jumping ability of Mario (high he can reach)
int ySpeed = 0; //the vertical speed of Mario
boolean onPlatform; //checks if Mario is standing on the ground on a platform
int groundLocation = 655; //y-location of top of ground
/**
* Greenfoot cycles through the act method at a speed of approximately 60 frames per second.
* Custom methods and code that need to be constantly checked should be placed here.
*/
public void act()
{
moveVertically(); //call custom method
moveHorizontally(); //call custom method
}
public void moveHorizontally() {
if (Greenfoot.isKeyDown("left")) {
this.setLocation(this.getX() - 5, this.getY());
}
if (Greenfoot.isKeyDown("right")) {
this.setLocation(this.getX() + 5, this.getY());
}
}
/**
* Responsible for the vertical movement of Mario,
* Involving jumping, falling, and platform detection
*/
public void moveVertically() {
int worldHeight = getWorld().getHeight();
int marioHeight = getImage().getHeight();
boolean onPlatform = false; //originally, Mario is in the air
ySpeed += gravity; //represents acceleration, updates vertical speed accordingly
setLocation(getX(), getY() + ySpeed); //updates Mario's location based on vertical speed
//if Mario is on the ground
if (getY() > groundLocation) {
setLocation(getX(), groundLocation);
ySpeed = 0;
onPlatform = true;
}
//if Mario is on a pipe
Actor a = getOneIntersectingObject(Actor.class);
if (a != null) {
if (this.getY() + this.getImage().getHeight()/2 > a.getY() - a.getImage().getHeight()/2) {
setLocation(this.getX(), a.getY() - a.getImage().getHeight()/2 - this.getImage().getHeight()/2);
ySpeed = 0;
onPlatform = true;
}
}
//if Mario is on top of a platform or the ground and the space bar is pressed, enable jumping
if (onPlatform && Greenfoot.isKeyDown("space")) ySpeed = -jumpAbility;
}
} public void moveHorizontally() {
/*Actor a = getOneIntersectingObject(Actor.class);
if (a != null) {
if (this.getX() + this.getImage().getWidth()/2 > a.getX() - a.getImage().getWidth()/2) { //doesnt work
setLocation(a.getX() - a.getImage().getWidth()/2 - this.getImage().getWidth()/2, this.getY());
} else if (this.getX() - this.getImage().getWidth()/2 < a.getX() + a.getImage().getWidth()/2) { //doesnt work
setLocation(a.getX() + a.getImage().getWidth()/2 + this.getImage().getWidth()/2, this.getY());
}
}*/
if (Greenfoot.isKeyDown("left")) {
this.setLocation(this.getX() - 5, this.getY());
}
if (Greenfoot.isKeyDown("right")) {
this.setLocation(this.getX() + 5, this.getY());
}
}int dx = 0;
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("right")) dx++;
if (dx == 0) return;
setLocation(getX()+dx*5, getY());
Actor a = getOneIntersectingObject(Actor.class);
if (a == null) return;
setLocation(a.getX()-dx*(getImage().getWidth()+a.getImage().getWidth())/2+1), getY()); public void moveHorizontally() {
int dx = 0;
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("right")) dx++;
if (dx == 0) return;
setLocation(getX()+dx*5, getY());
Actor a = getOneIntersectingObject(Actor.class);
if (a == null) return;
setLocation(a.getX()-dx*(getImage().getWidth()+a.getImage().getWidth())/2+1, getY());
/*if (Greenfoot.isKeyDown("left")) {
this.setLocation(this.getX() - 5, this.getY());
}
if (Greenfoot.isKeyDown("right")) {
this.setLocation(this.getX() + 5, this.getY());
}*/
}setLocation(a.getX()-dx*((getImage().getWidth()+a.getImage().getWidth())/2+1), getY());
//check for and move back off any obstacles
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
{
static final int gravity = 2; //force of gravity
static final int jumpAbility = 30; //the jumping ability of Mario (high he can reach)
int ySpeed = 0; //the vertical speed of Mario
boolean onPlatform; //checks if Mario is standing on the ground on a platform
int groundLocation = 655; //y-location of top of ground
/**
* Greenfoot cycles through the act method at a speed of approximately 60 frames per second.
* Custom methods and code that need to be constantly checked should be placed here.
*/
public void act()
{
moveVertically(); //call custom method
moveHorizontally(); //call custom method
}
public void moveHorizontally() {
int dx = 0;
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("right")) dx++;
if (dx == 0) return;
setLocation(getX()+dx*5, getY());
Actor a = getOneIntersectingObject(Pipe.class);
if (a == null) return;
setLocation(a.getX()-dx*((getImage().getWidth()+a.getImage().getWidth())/2+1), getY());
/*if (Greenfoot.isKeyDown("left")) {
this.setLocation(this.getX() - 5, this.getY());
}
if (Greenfoot.isKeyDown("right")) {
this.setLocation(this.getX() + 5, this.getY());
}*/
}
/**
* Responsible for the vertical movement of Mario,
* Involving jumping, falling, and platform detection
*/
public void moveVertically() {
int worldHeight = getWorld().getHeight();
int marioHeight = getImage().getHeight();
boolean onPlatform = false; //originally, Mario is in the air
ySpeed += gravity; //represents acceleration, updates vertical speed accordingly
setLocation(getX(), getY() + ySpeed); //updates Mario's location based on vertical speed
//if Mario is on the ground
if (getY() > groundLocation) {
setLocation(getX(), groundLocation);
ySpeed = 0;
onPlatform = true;
}
//if Mario is on a pipe
Actor a = getOneIntersectingObject(Pipe.class);
if (a != null) {
if (this.getY() + this.getImage().getHeight()/2 > a.getY() - a.getImage().getHeight()/2) {
setLocation(this.getX(), a.getY() - a.getImage().getHeight()/2 - this.getImage().getHeight()/2);
ySpeed = 0;
onPlatform = true;
}
}
/*int dy = (int)Math.signum(ySpeed);
while (getOneIntersectingObject(null) != null) { //doesnt change anything but causes collision detection with clouds (dont want)
setLocation(getX(), getY()-dy);
if(dy>0) onPlatform = true;
ySpeed = 0;
}*/
//if Mario is on top of a platform or the ground and the space bar is pressed, enable jumping
if (onPlatform && Greenfoot.isKeyDown("space")) ySpeed = -jumpAbility;
}
}int dy = (int)Math.signum(ySpeed); setLocation(getX(), a.getY()-dy*((a.getImage().getHeight()+getImage().getHeight())/2+1)); if (dy > 0) onPlatform = true; ySpeed = 0;
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
{
static final int gravity = 2; //force of gravity
static final int jumpAbility = 30; //the jumping ability of Mario (high he can reach)
int ySpeed = 0; //the vertical speed of Mario
boolean onPlatform; //checks if Mario is standing on the ground on a platform
int groundLocation = 655; //y-location of top of ground
/**
* Greenfoot cycles through the act method at a speed of approximately 60 frames per second.
* Custom methods and code that need to be constantly checked should be placed here.
*/
public void act()
{
moveVertically(); //call custom method
moveHorizontally(); //call custom method
}
public void moveHorizontally() {
int dx = 0;
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("right")) dx++;
if (dx == 0) return;
setLocation(getX()+dx*5, getY());
Actor a = getOneIntersectingObject(Pipe.class);
if (a == null) return;
setLocation(a.getX()-dx*((getImage().getWidth()+a.getImage().getWidth())/2+1), getY());
/*if (Greenfoot.isKeyDown("left")) {
this.setLocation(this.getX() - 5, this.getY());
}
if (Greenfoot.isKeyDown("right")) {
this.setLocation(this.getX() + 5, this.getY());
}*/
}
/**
* Responsible for the vertical movement of Mario,
* Involving jumping, falling, and platform detection
*/
public void moveVertically() {
int worldHeight = getWorld().getHeight();
int marioHeight = getImage().getHeight();
boolean onPlatform = false; //originally, Mario is in the air
ySpeed += gravity; //represents acceleration, updates vertical speed accordingly
setLocation(getX(), getY() + ySpeed); //updates Mario's location based on vertical speed
//if Mario is on the ground
if (getY() > groundLocation) {
setLocation(getX(), groundLocation);
ySpeed = 0;
onPlatform = true;
}
//if Mario is on a pipe
Actor a = getOneIntersectingObject(Pipe.class);
if (a != null) {
int dy = (int)Math.signum(ySpeed);
setLocation(getX(), a.getY()-dy*((a.getImage().getHeight()+getImage().getHeight())/2+1));
if (dy > 0) onPlatform = true;
ySpeed = 0;
}
int dy = (int)Math.signum(ySpeed);
while (getOneIntersectingObject(null) != null) { //causes collision detection with clouds (dont want)
setLocation(getX(), getY()-dy);
if(dy>0) onPlatform = true;
ySpeed = 0;
}
//if Mario is on top of a platform or the ground and the space bar is pressed, enable jumping
if (onPlatform && Greenfoot.isKeyDown("space")) ySpeed = -jumpAbility;
}
}