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

2016/3/18

Jump then fall script, not working

Randy. Randy.

2016/3/18

#
import greenfoot.*; 

public class Character extends Actor
{
    double Force = 0;
    double Gravity = 0.5;
    double Boost_Speed = -6;
    int Wait = 0;
    
    public void act() 
    {
        setLocation( getX(), (int)(getY() + Force) );
        if(Greenfoot.isKeyDown("up")){
            Wait++;
            Force = Boost_Speed;
            if(Wait >= 8)
            {   
                setLocation( getX(), (int)(getY() + 1) );
                Wait = 0;
            } 
        }
        Force = Force + Gravity;
    } 
    
}
danpost danpost

2016/3/18

#
How is it not working? Simply said, your act method does this: - move vertically at current speed (line 12) - if 'up' key is pressed (line 13) * add to wait counter (line 14) * set vertical speed to initial jump speed (line 15) - if waited long enough (line 16) * drop down 1 cell (line 17) * begin new wait period (line 18) - apply gravity to vertical speed (line 22) Now, I do not understand why you would want the actor to drop down one cell every eighth act cycle that the 'up' key is pressed. That just does not make much sense. You should probably remove all lines dealing with that (lines 8, 14 and 16 thru 20). The only other thing that might be of concern is that vertical movement is not actually being tracked precisely, although vertical speed is. Each act cycle, you are rounding the new vertical location coordinate to an integer, so any fractional part is being lost, since the actual 'double' value is not being retained. This should not influence the general movement too much and would not be considered as a cause for the code "not working".
Randy. Randy.

2016/3/18

#
I simply want the player to be able to jump when "up" is pressed, this doesn't work because if you hold "up", you float/ fly like a jetpack effect.
Randy. Randy.

2016/3/18

#
Yea it didn't help removing those lines, the player can still float and multi-jump
danpost danpost

2016/3/18

#
Randy. wrote...
Yea it didn't help removing those lines, the player can still float and multi-jump
Well, when do you want any secondary jumping to be allowed? what conditions do you require for this?
Randy. Randy.

2016/3/18

#
Not at all, I just want a regular jump like Mario The code I wrote was suppose to allow you to jump once then you fall back down and after 1 second you're able to jump again
danpost danpost

2016/3/18

#
Randy. wrote...
Not at all, I just want a regular jump like Mario The code I wrote was suppose to allow you to jump once then you fall back down and after 1 second you're able to jump again
You could add a delay field that counts down to zero between jumps; its value must be set to a value that would allow a complete jump. The problem with this, however, is that if the player can land at different altitudes, the jump time will vary. Better would be to only allow a jump when the actor is on top of something (usually the ground or a platform -- but; could be other things as well).
Randy. Randy.

2016/3/18

#
How do I do this? can you point me toward a post or tutorial or give me source code?
danpost danpost

2016/3/18

#
Randy. wrote...
How do I do this? can you point me toward a post or tutorial or give me source code?
You could look at my Jump and Run Demo w/Moving Platform scenario to see how I would do it.
Randy. Randy.

2016/3/18

#
It says "Your browser is ignoring the <APPLET> tag." and when I tried to download the Java plugin it says it's available for Windows 64bit which is what I have...
danpost danpost

2016/3/19

#
Randy. wrote...
It says "Your browser is ignoring the <APPLET> tag." and when I tried to download the Java plugin it says it's available for Windows 64bit which is what I have...
Alright, the player class (Who class) has this in it:
import greenfoot.*;

/**
 * Class Who: class for the main actor of this demo
 */
public class Who extends Actor
{
    // class constants
    static final int GRAVITY = 2;
    static final int JUMP_FORCE = 30;
    // class images
        // static GreenfootImage rightFacing = new GreenfootImage("leftImage.png");
        // static GreenfootImage frontFacing = new GreenfootImage("idleImage.png")
        // static GreenfootImage leftFacing = new GreenfootImage("rightImage.png");
    // instance fields
    int xSpeed = 4; // currently a constant
    int ySpeed = 0;
    
    /**
     * Method act: controls movement of actor
     */
    public void act()
    {
        moveHorizontally();
        moveVertically();
    }
    
    /**
     * Method moveHorizontally: controls horizontal movement of actor
     */
    private void moveHorizontally()
    {
        // get values needed for method
        int worldWidth = getWorld().getWidth();
        int myWidth = getImage().getWidth();
        int dx = 0; // for dirction of movement
        // determine direction of movement
        if (Greenfoot.isKeyDown("left")) dx--;
        if (Greenfoot.isKeyDown("right")) dx++;
        // move actor
        setLocation(getX()+dx*xSpeed, getY());
        // set image here using value of 'dx'
            // if (dx == -1) setImage(leftFacing);
            // if (dx == 1) setImage(rightFacing);
            // if (dx == 0) setImage(frontFacing);
        // check world edges (left and right)
        if (getX() < myWidth/2) setLocation(myWidth/2, getY());
        if (getX() > worldWidth-myWidth/2) setLocation(worldWidth-myWidth/2, getY());
        // check for and move back off of any obstacles
        Actor obstacle = getOneIntersectingObject(null);
        while (obstacle != null)
        {
            setLocation(getX()-dx, getY());
            obstacle = getOneIntersectingObject(null);
        }
    }
    
    /**
     * Method moveVertically: controls vertical movement of actor (including jumping)
     */
    private void moveVertically()
    {
        // get values needed for method
        int worldHeight = getWorld().getHeight();
        int myHeight = getImage().getHeight();
        boolean onGround = false; // for can-jump state
        // fall
        ySpeed += GRAVITY;
        setLocation(getX(), getY()+ySpeed);
        // check for world edge (bottom)
        if (getY() > worldHeight-myHeight/2)
        {
            setLocation(getX(), worldHeight-myHeight/2);
            ySpeed = 0;
            onGround = true;
        }
        // check for and move back off of any obstacles
        int dy = (int)Math.signum(ySpeed); // direction of movement
        Actor obstacle = getOneIntersectingObject(null);
        if (obstacle != null)
        {
            ySpeed = 0;
            if (dy > 0) onGround = true;
        }
        int reps = 0;
        while (obstacle != null)
        {
            setLocation(getX(), getY()-dy);
            obstacle = getOneIntersectingObject(null);
            reps++;
            if (reps > myHeight/2)
            {
                Greenfoot.stop();
                return;
            }
        }
        // check jump
        if (onGround && Greenfoot.isKeyDown("up")) ySpeed = -JUMP_FORCE;
    }
}
Randy. Randy.

2016/3/19

#
danpost wrote...
Alright, the player class (Who class) has this in it:
I'm getting a java.lang.NullPointerException error for
Actor obstacle = getOneIntersectingObject(null);
so I added a sub-actor called "obstacle" and the error is still coming up
danpost danpost

2016/3/19

#
Randy. wrote...
I'm getting a java.lang.NullPointerException error for
Actor obstacle = getOneIntersectingObject(null);
so I added a sub-actor called "obstacle" and the error is still coming up
'obstacle' is a variable name, not a class name. Adding a sub-class of actor with that name will not accomplish anything. Anyway, the line given could not possibly produce that error. You need to show the error message you are getting by copy/pasting the entire trace output here.
You need to login to post a reply.