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

2017/4/22

What must I write that I can't only hold the buttum and do nothing so that the Player alwyas jumps??

Timon Timon

2017/4/22

#
I want that when I hold the buttum that I only jump once and not always. So that I must press "up" always when I want to jump. That is the "Mans.class" code.
import greenfoot.*;
public class Mans extends Actor
{
    public int ySpeed;
    public int groundLevel=489;
    private boolean pressed;
    public void act()
    {
        boolean onGround = false;
        ySpeed++;
        setLocation(getX(), getY()+ySpeed);
        // at ground level
        if (getY() > groundLevel)
        {
            ySpeed = 0;
            setLocation(getX(), groundLevel);
            onGround = true;
        }
        // hitting box
        Actor object = getOneIntersectingObject(Objects.class);
        if (object != null)
        {
            int yDir = (int)Math.signum(ySpeed);
            setLocation(getX(), object.getY()-yDir*((object.getImage().getHeight()+getImage().getHeight())/2+1));
            ySpeed = 0;
            if (yDir > 0) onGround = true;
        }
        // jumping
        if (onGround && Greenfoot.isKeyDown("up")) ySpeed = -15; 
        // firing weapon       
        if (pressed == Greenfoot.isKeyDown("space"))
        {
            pressed = !pressed;
            if (pressed) getWorld().addObject(new Weapons(), getX()+80, getY()-25); 
        } 
        //die
        if (this.isAtEdge())
        {   this.removeTouching(Objects.class);
            this.setImage("Mans3.png");
            this.setLocation(400, 400);
            Greenfoot.playSound("game-over.wav");
            Greenfoot.stop();
            MyWorld m = (MyWorld) getWorld();
            m.GameOver();
        }

    }
}

Super_Hippo Super_Hippo

2017/4/22

#
You can do it similar like how you did it with shooting.
Timon Timon

2017/4/22

#
But it didn't work.
import greenfoot.*;
public class Mans extends Actor
{
    public int ySpeed;
    public int groundLevel=489;
    private boolean pressed;
    public void act()
    {
        boolean onGround = false;
        ySpeed++;
        setLocation(getX(), getY()+ySpeed);
        // at ground level
        if (getY() > groundLevel)
        {
            if (pressed)
            ySpeed = 0;
            setLocation(getX(), groundLevel);
            onGround = true;
        }
        // hitting box
        Actor object = getOneIntersectingObject(Objects.class);
        if (object != null)
        {
            int yDir = (int)Math.signum(ySpeed);
            setLocation(getX(), object.getY()-yDir*((object.getImage().getHeight()+getImage().getHeight())/2+1));
            ySpeed = 0;
            if (yDir > 0) onGround = true;
        }
        // jumping
        if (onGround && pressed == Greenfoot.isKeyDown("up"))
        {
            ySpeed = -15;
            pressed = !pressed;
        }
        // firing weapon       
        if (pressed == Greenfoot.isKeyDown("space"))
        {
            pressed = !pressed;
            if (pressed) getWorld().addObject(new Weapons(), getX()+80, getY()-25); 
        } 
        //die
        if (this.isAtEdge())
        {   this.removeTouching(Objects.class);
            this.setImage("Mans3.png");
            this.setLocation(400, 400);
            Greenfoot.playSound("game-over.wav");
            Greenfoot.stop();
            MyWorld m = (MyWorld) getWorld();
            m.GameOver();
        }

    }
}

Timon Timon

2017/4/22

#
What is false??
Super_Hippo Super_Hippo

2017/4/22

#
Create and use a different boolean.
Timon Timon

2017/4/22

#
Now always when I want to jump/shoot it do the other thing (jump/shoot) also.
import greenfoot.*;
public class Mans extends Actor
{
    public int ySpeed;
    public int groundLevel=489;
    private boolean pressed;
    private boolean pushed;
    public void act()
    {
        boolean onGround = false;
        ySpeed++;
        setLocation(getX(), getY()+ySpeed);
        // at ground level
        if (getY() > groundLevel)
        {
            if (pushed)
            ySpeed = 0;
            setLocation(getX(), groundLevel);
            onGround = true;
        }
        // hitting box
        Actor object = getOneIntersectingObject(Objects.class);
        if (object != null)
        {
            int yDir = (int)Math.signum(ySpeed);
            setLocation(getX(), object.getY()-yDir*((object.getImage().getHeight()+getImage().getHeight())/2+1));
            ySpeed = 0;
            if (yDir > 0) onGround = true;
        }
        // jumping
        if (onGround && pushed == Greenfoot.isKeyDown("up"))
        {
            ySpeed = -15;
            pushed = !pushed;
        }
        // firing weapon       
        if (pressed == Greenfoot.isKeyDown("space"))
        {
            pressed = !pressed;
            if (pressed) getWorld().addObject(new Weapons(), getX()+80, getY()-25); 
        } 
        //die
        if (this.isAtEdge())
        {   this.removeTouching(Objects.class);
            this.setImage("Mans3.png");
            this.setLocation(400, 400);
            Greenfoot.playSound("game-over.wav");
            Greenfoot.stop();
            MyWorld m = (MyWorld) getWorld();
            m.GameOver();
        }

    }
}
Super_Hippo Super_Hippo

2017/4/22

#
I am not sure how this happens but you need to check if 'pushed' is true just like how the 'if (pressed)' in the jump code. Btw, what is line 16 supposed to do?
Timon Timon

2017/4/22

#
I have write that because I had write this also at the firing Weapon (ther I have write "if(pressed)")
Timon Timon

2017/4/22

#
Now I have removed the "if(pushed)" code and have placed it in line 32 and it works.
 if (onGround && pushed == Greenfoot.isKeyDown("up"))
        {
            pushed = !pushed;
            if (pushed)ySpeed = -15;
        }
But it is a Little bit too slow.How can I make it a bit faster?
Super_Hippo Super_Hippo

2017/4/22

#
What do you mean with 'too slow'? It should jump whenever you hit the up arrow key when it is on the ground. If it should jump higher, you have to change the -15 to a smaller value (higher negative value).
Timon Timon

2017/4/23

#
Thanks I have it.
You need to login to post a reply.