This site requires JavaScript, please enable it in your browser!
Greenfoot back
¿ques♫
¿ques♫ wrote ...

2019/7/2

Jumping

1
2
danpost danpost

3 days ago

#
¿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.
You should not have to do anything extra to prevent that. Please show all your horizontal movement codes.
¿ques♫ ¿ques♫

2 days ago

#
Mario class:
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;
    }
}
Here is a link of a video displaying my issue: Mario game The method "moveHorizontally" only contains code that moves Mario which in turn scrolls the screen. I have tried to fix my problem of Mario teleporting to the top of objects when intersecting, but I think it is due to the line
this.getY() + this.getImage().getHeight()/2 > a.getY() - a.getImage().getHeight()/2
because this tests if the y-location of the bottom of Mario becomes more than the top of an object (since y = 0 at the top edge). Thanks for your help so far, I apologize for prolonging this discussion.
danpost danpost

2 days ago

#
You still need separate collision detection for when moving horizontally (put in the moveHorizontally method).
¿ques♫ ¿ques♫

yesterday

#
    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());
        }
    }
This doesnt seem to work...but I think it is because of this line: "this.getY() + this.getImage().getHeight()/2 > a.getY() - a.getImage().getHeight()/2" as whenever Mario's boot becomes less than the top of the pipe the location is set to the top of the pipe. This is why I originally put else ifs in that section of the code because Mario can't be intersecting more than one side of the pipe. But this did not work either.
danpost danpost

yesterday

#
Move first; then check for obstacles. Move back according to direction moved:
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());
¿ques♫ ¿ques♫

yesterday

#
Tried this ^, only works on the right side. Also, you seem to have accidentally put an extra bracket after the +1. This is my current code for the moveHorizontally() 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(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());
        }*/
    }
Video demonstration: Mario game
danpost danpost

yesterday

#
¿ques♫ wrote...
Tried this ^, only works on the right side. Also, you seem to have accidentally put an extra bracket after the +1.
Well -- no. I have accidentally missed placing the bracket that was to pair with that "extra" bracket. Try:
setLocation(a.getX()-dx*((getImage().getWidth()+a.getImage().getWidth())/2+1), getY());
¿ques♫ ¿ques♫

15 hours ago

#
Thanks, this works really well. Should I use the dy strategy for collision detection under objects? I am trying to modify the moveVertically() method so that if Mario jumps and hits an object above him, he bounces off of it and comes back down. Do you have any suggestions on how to do this, or can I find answers in your "Jump and Run Demo w/Moving Platform" demo?
danpost danpost

15 hours ago

#
¿ques♫ wrote...
Thanks, this works really well. Should I use the dy strategy for collision detection under objects? I am trying to modify the moveVertically() method so that if Mario jumps and hits an object above him, he bounces off of it and comes back down. Do you have any suggestions on how to do this, or can I find answers in your "Jump and Run Demo w/Moving Platform" demo?
Yes. You will find the answers there. It is the part labelled:
//check for and move back off any obstacles
in the moveVertically method.
You need to login to post a reply.
1
2