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

2016/12/27

Jumping actor

1
2
Hondrok Hondrok

2016/12/27

#
Soooo... I want the arctor to jump at a certain height, and not to go beyond that limit and I don't know where should I set the limit
import greenfoot.*;
  
public class Dolphin extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrenght = 5;
    private Label myLabel;
    private int count = 0;
    
    public Dolphin (Label label)
    {
        myLabel = label;
    }
    
    public void act() 
    {
       checkKeys();
       checkFall();
    }
    
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
        {
           moveLeft();
        } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
           count++;
           myLabel.setText("COUNT: " + count);
       }
    }    

    public void checkFall()
    {
        if(onGround())
        {
           vSpeed=0; 
        }
        else
        {
            fall();
        }
    }

    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
    
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
        
    public void jump()
    {
        vSpeed = - jumpStrenght;
        fall();
    }
    
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
    }
    
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
    }
}
danpost danpost

2016/12/27

#
Needs clarification. You do not want the actor to jump more than a certain height from where is begins the jump -- or you do not want your actor to exceed a certain height in the world viewport? The first can be controlled by the value of 'jumpStrength' (or 'jumpStrenght', as you have it spelt). The other can be dealt with after 'fall'ing:
if vSpeed < 0 && getY() < 50) // limited to no closer than 50 pixels from top of world
{
    setLocation(getX(), 50);
    vSpeed = 0;
}
Hondrok Hondrok

2016/12/28

#
I want the actor to jump more than a certain height from where is begins the jump but I want the actot not to float when I press the up key and thank you
danpost danpost

2016/12/28

#
Hondrok wrote...
I want the actor to jump more than a certain height from where is begins the jump but I want the actot not to float when I press the up key and thank you
You are activating the jump unconditionally -- whether the actor is on the ground or not. You only want to initiate a jump when the actor is on the ground and the "up" key is pressed.
Hondrok Hondrok

2016/12/28

#
You didn't understand. I want the arctor to "finish" the jump for example when I keep pressing the key the actor doesn't stay in air, the arctor does just one jump.
Hondrok Hondrok

2016/12/28

#
danpost wrote...
Hondrok wrote...
I want the actor to jump more than a certain height from where is begins the jump but I want the actot not to float when I press the up key and thank you
You are activating the jump unconditionally -- whether the actor is on the ground or not. You only want to initiate a jump when the actor is on the ground and the "up" key is pressed.
You didn't understand. I want the arctor to "finish" the jump for example when I keep pressing the key the actor doesn't stay in air, the arctor does just one jump.
danpost danpost

2016/12/28

#
Hondrok wrote...
I want the arctor to "finish" the jump for example when I keep pressing the key the actor doesn't stay in air, the arctor does just one jump.
Then you need to keep track of the "up" key by adding an instance Boolean field, as:
private boolean upKeyDown;
The following will control the value of the field (placed at the end of the act method)
if (upKeyDown != Greenfoot.isKeyDown("up")) upKeyDown = ! upKeyDown;
Now you can add a third condition to jump; namely 'if ( ! upKeyDown)'.
Hondrok Hondrok

2016/12/29

#
danpost wrote...
Hondrok wrote...
I want the arctor to "finish" the jump for example when I keep pressing the key the actor doesn't stay in air, the arctor does just one jump.
Then you need to keep track of the "up" key by adding an instance Boolean field, as:
private boolean upKeyDown;
The following will control the value of the field (placed at the end of the act method)
if (upKeyDown != Greenfoot.isKeyDown("up")) upKeyDown = ! upKeyDown;
Now you can add a third condition to jump; namely 'if ( ! upKeyDown)'.
I don't understand what I should do...
import greenfoot.*;
  
public class Dolphin extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 5;
    private Label myLabel;
    private int count = 0;
    
    public Dolphin (Label label)
    {
        myLabel = label;
    }
    
    public void act() 
    {
       checkKeys();
       checkFall();
       hitGoomba();
       hitCastle();
    }
    
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
        {
           moveLeft();
           scoreCount();
        } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
           scoreCount();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
           scoreCount();
       }
    }    

    private boolean upKeyDown;
    {
        if (upKeyDown != Greenfoot.isKeyDown("up"))
        {
            upKeyDown = ! upKeyDown;
        }
    }
    
    public void checkFall()
    {
        if(onGround())
        {
           vSpeed=0; 
        }
        else
        {
            fall();
        }
    }

    public void scoreCount()
    {
        count+=10;
        myLabel.setText("SCOR: " + count);
    }
    
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
    
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
        
    public void jump()
    {
       vSpeed = - jumpStrength;
       fall();
       if( vSpeed < 0 && getY() < 500)
       {
           setLocation(getX(), 500);
           vSpeed = 0;
       }
    }
    
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
            myWorld.removeObject(goomba);
        }
    }
    
    public void hitCastle()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            World myWorld = getWorld();
            GameOver_Win gameover_win = new GameOver_Win();
            myWorld.addObject(gameover_win, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
        }
    }
    
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
    }
    
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
    }
    
    
}
If you ever played Mario you will understand better what I want to say. You know in Mario when you press the "up" key mario jumps and does the jumping motion even if you hold the key, well here is my game if you hold the "up" key the actor stays in the air, and I want it to do the same jumping-thing as it Mario does.
danpost danpost

2016/12/29

#
From what I can tell, Mario jumps normally (without the need for the 'upKeyDown' boolean field) -- where a jump will follow another jump if the "up" key is held down. Remove lines 44 through 50 in the Dolphin class. However, the horizontal movement is "accelerated" as the "left" or "right" key is held down. With your code above, I think you have some code misplaced. The 'jump' method in the Dolphin class should only set 'vSpeed' to 'jumpstrength; and, only if (1) on ground; and (2) the "up" key is pressed. Since 'jump' is only called when the "up" key is pressed, the only check to perform in the method is the on-ground one:
private void jump()
{
    if (onGround()) vSpeed = -jumpStrength;
}
That should set you in the right direction.
Hondrok Hondrok

2016/12/29

#
danpost wrote...
From what I can tell, Mario jumps normally (without the need for the 'upKeyDown' boolean field) -- where a jump will follow another jump if the "up" key is held down. Remove lines 44 through 50 in the Dolphin class. However, the horizontal movement is "accelerated" as the "left" or "right" key is held down. With your code above, I think you have some code misplaced. The 'jump' method in the Dolphin class should only set 'vSpeed' to 'jumpstrength; and, only if (1) on ground; and (2) the "up" key is pressed. Since 'jump' is only called when the "up" key is pressed, the only check to perform in the method is the on-ground one:
private void jump()
{
    if (onGround()) vSpeed = -jumpStrength;
}
That should set you in the right direction.
import greenfoot.*;
  
public class Dolphin extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 5;
    private Label myLabel;
    private int count = 0;
    
    public Dolphin (Label label)
    {
        myLabel = label;
    }
    
    public void act() 
    {
       checkKeys();
       checkFall();
       hitGoomba();
       hitCastle();
    }
    
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
        {
           moveLeft();
           scoreCount();
        } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
           scoreCount();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
           scoreCount();
       }
    }    

    public void checkFall()
    {
        if(onGround())
        {
           vSpeed=0; 
        }
        else
        {
            fall();
        }
    }

    public void scoreCount()
    {
        count+=10;
        myLabel.setText("SCOR: " + count);
    }
    
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
    
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
        
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
       fall();
       if( vSpeed < 0 && getY() < 500)
       {
           setLocation(getX(), 500);
           vSpeed = 0;
       }
    }
    
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
            myWorld.removeObject(goomba);
        }
    }
    
    public void hitCastle()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            World myWorld = getWorld();
            GameOver_Win gameover_win = new GameOver_Win();
            myWorld.addObject(gameover_win, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
        }
    }
    
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
    }
    
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
    }
    
    
}
I don't think that is the right thing. After I used what you just said it is not working.
danpost danpost

2016/12/29

#
Hondrok wrote...
I don't think that is the right thing. After I used what you just said it is not working.
Please remove lines 80 through 85. Then see.
Hondrok Hondrok

2016/12/29

#
danpost wrote...
Hondrok wrote...
I don't think that is the right thing. After I used what you just said it is not working.
Please remove lines 80 through 85. Then see.
It doesn't work. When I press the "up" key the actor does nothing.
danpost danpost

2016/12/29

#
Hondrok wrote...
It doesn't work. When I press the "up" key the actor does nothing.
Try changing line 46 (in the 'checkFall' method) to this:
if (onGround() && vSpeed != -jumpStrength)
Hondrok Hondrok

2016/12/29

#
danpost wrote...
Hondrok wrote...
It doesn't work. When I press the "up" key the actor does nothing.
Try changing line 46 (in the 'checkFall' method) to this:
if (onGround() && vSpeed != -jumpStrength)
Is not working.
import greenfoot.*;
   
public class Dolphin extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 5;
    private Label myLabel;
    private int count = 0;
     
    public Dolphin (Label label)
    {
        myLabel = label;
    }
     
    public void act() 
    {
       checkKeys();
       checkFall();
       hitGoomba();
       hitCastle();
    }
     
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
        {
           moveLeft();
           scoreCount();
        } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
           scoreCount();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
           scoreCount();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
        }
        else
        {
            fall();
        }
    }
 
    public void scoreCount()
    {
        count+=10;
        myLabel.setText("SCOR: " + count);
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
       fall();
       if( vSpeed < 0 && getY() < 500)
       {
           setLocation(getX(), 500);
           vSpeed = 0;
       }
    }
     
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
            myWorld.removeObject(goomba);
        }
    }
     
    public void hitCastle()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            World myWorld = getWorld();
            GameOver_Win gameover_win = new GameOver_Win();
            myWorld.addObject(gameover_win, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
        }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
    }
     
     
}
danpost danpost

2016/12/29

#
Hondrok wrote...
Is not working.
I though I suggested that you remove lines 80 through 85.
There are more replies on the next page.
1
2