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

2017/4/3

Why can't I jump on a block that created?

Rex553 Rex553

2017/4/3

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Jumper here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Jumper extends BaseClass
{
    private int ySpeed;
    private int speed=3;
    int groundLevel = 500;
    /**
     * Act - do whatever the Jumper wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int groundLevel = 500;
        boolean onGround = (getY() == groundLevel);
        if (!onGround) // in middle of jump
        {
            if(!(ySpeed < 15))
            {
                ySpeed=15;
            }
            ySpeed++; // adds gravity effect
            setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)
            if (getY()>=groundLevel) // has landed (reached ground level)
            {
                setLocation(getX(), groundLevel); // set on ground
                Greenfoot.getKey(); // clears any key pressed during jump
            }
        }
        else
        {
            if (Greenfoot.isKeyDown("w"))
            {
                ySpeed = -15; // add jump speedySpeed = -15; // add jump speed
                setLocation(getX(), getY()+ySpeed); // leave groundsetLocation(getX(), getY()+ySpeed); // leave 
            }
        }
        keys(); 
        gameOver();
    }

    public void setLocation(int x, int y)
    {
        int oldX = getX();
        int oldY = getY();
        super.setLocation(x, y);
        if(!getIntersectingObjects(Bark.class).isEmpty())
        {
            super.setLocation(oldX, oldY);
        }
    }

    public void keys()

    {
        if (Greenfoot.isKeyDown("a"))
        {
            setLocation(getX() - speed, getY());
            setImage("redcapleft.gif");
        }   

        if (Greenfoot.isKeyDown("d"))
        {
            setLocation(getX() + speed, getY());
            setImage("redcapright.gif");
        }

        if (Greenfoot.isKeyDown("q"))
        {
            removeObject(Announcement.class);
        }
    }
}
Could anyone help me with this problem?
danpost danpost

2017/4/3

#
The only things you currently have that restrict the downward movement of the actor is 'groundLevel' and Bark objects. What are your block objects that you are creating (what is the name of the class that creates them)?
Rex553 Rex553

2017/4/3

#
The bock objects are Bark
danpost danpost

2017/4/3

#
Rex553 wrote...
The bock objects are Bark
The 'setLocation' override should prevent any Jumper object from passing through a Bark object (unless the Bark object is quite small -- less than 15 horizontally or vertically).
Rex553 Rex553

2017/4/3

#
I don't glitch through the Bark object, but when I am on a Bark object I want to jump but I can't
danpost danpost

2017/4/3

#
You should unconditionally fall. Then, if below 500, reset there and set 'onGround' to true, else if touching Bark object, move back. At this point (when moving back), first see if ySpeed is positive or negative and if positive, set 'onGround' to true; then (in either case) reset ySpeed back to zero. The variable 'onGround' should be set to false at the beginning of the act method. It would look more like this:
public void act()
{
    boolean onGround = false; // set to true if found on ground or block
    ySpeed++; // gravity
    setLocation(getX(), getY()+ySpeed); // move vertically
    if (getX() > groundLevel) // hit bottom
    {
        setLocation(getX(), groundLevel);
        onGround = true;
    }
    else
    {
        Actor bark = getOneIntersectingObject(Bark.class);
        if (bark != null) // hit block
        {
            int direction = (int)Math.signum(ySpeed); // rose (-1) or fell (1)
            ySpeed = 0; // vertical speed blunted
            if (direction > 0) onGround = true; // on bark
            setLocation(getX(), bark.getY()-direction*(getImage().getHeight()+bark.getImage().getHeight())/2);
        }
    }
    if (onGround && Greenfoot.isKeyDown("w"))
    {
        ySpeed = -15; // add jump speed
    }
    keys();
    gameOver();
}
Remove your 'setLocation' overriding method (lines 48 through 57).
Rex553 Rex553

2017/4/3

#
I tried it, but now Jumper falls through the floor
danpost danpost

2017/4/3

#
Rex553 wrote...
I tried it, but now Jumper falls through the floor
Oops. Change line 6 to:
if (getY() > groundLevel)
Rex553 Rex553

2017/4/3

#
Now I have the problem of passing through the Bark object
danpost danpost

2017/4/3

#
Rex553 wrote...
Now I have the problem of passing through the Bark object
While moving vertically? It looks like I gave the proper code that prevents any passing through Bark objects while moving vertically.
Rex553 Rex553

2017/4/3

#
I can pass through it horizontally through it
danpost danpost

2017/4/3

#
Rex553 wrote...
I can pass through it horizontally through it
Change the 'keys' method to this:
public void keys()
{
    int dx = 0;
    if (Greenfoot.isKeyDown("a"))
    {
        dx--;
        setImage("redcapleft.gif");
    }   
    if (Greenfoot.isKeyDown("d"))
    {
        dx++;
        setImage("redcapright.gif");
    }
    setLocation(getX()+speed*dx, getY());
    Actor bark = getOneIntersectingObject(Bark.class);
    if (bark != null)
    {
        setLocation(bark.getX()-dx*(getImage().getWidth()+bark.getImage().getWidth())/2, getY());
    }
    if (Greenfoot.isKeyDown("q"))
    {
        removeObject(Announcement.class);
    }
}
Rex553 Rex553

2017/4/4

#
Thank you so much!
You need to login to post a reply.