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

2018/3/17

Problem with ball moving

R0mane R0mane

2018/3/17

#
Hello, I am making a game in which a little character has to go to the left or to the right and avoid little balls. The character is actually not moving, but the ground underneath is. When the edge of the ground reaches the side of the screen, it stops moving even if the arrow key is pressed. When the ground is moving, the balls are also moving faster or slower to move with the ground as reference. My problem is that when the ground has reached the side of the screen and is not moving anymore, the balls should move normally again, but if the arrow key is pressed, they move as they should when the ground is moving (except the ground is not moving anymore here). Hoy can I make them move normally in that situation when the ground doesn't move but the arrow key is pressed? Here is my code in the ball class:
if(Greenfoot.isKeyDown("right")){
            
            if(groundLoc <= (600 - groundWidth / 2)){
                
                this.setLocation(this.getX() + speedX, this.getY() + speedY);
            
            } else{
                
                this.setLocation(this.getX() + speedX - 5, this.getY() + speedY);
            
            }
        }
        
        if(Greenfoot.isKeyDown("left")){
            
            if(groundLoc >= (groundWidth / 2)){
                
                this.setLocation(this.getX() + speedX, this.getY() + speedY);
            
            } else{
                
                this.setLocation(this.getX() + speedX + 5, this.getY() + speedY);

            }
        }
         
        this.setLocation(this.getX() + speedX, this.getY() + speedY);
Thanks in advance :)
Game/maniac Game/maniac

2018/3/17

#
You could probably solve the issue by making your equality checks '<' and '>' rather than '<=' and '>='. I would recommend though that the ground object should contain the method that returns it's current state.
danpost danpost

2018/3/17

#
If you want the balls to move horizontally with the ground, them have them use the ground as a reference to where they should be:
import greenfoot.*;

public class Ball extends Actor
{
    private static Actor ground; // reference to ground
    private int offsetToGround; // constant horizonal offset to maintain
    private int speedY = 3: // set to whatever

    protected void addedToWorld(World world)
    {
        ground = (Actor)world.getObjects(Ground.class).get(0); // save ground reference
        offsetToGround = this.getX()-ground.getX(); // save offset
    }

    public void act()
    {
        setLocation(ground.getX()+offsetToGround, getY()+speedY);
    }
}
Add the following to your world constructor:
setActOrder(Ground.class);
so the balls are positioned after the ground each act cycle.
R0mane R0mane

2018/3/18

#
Thank you danpost for the help. But now the problem is that the balls are moving vertically when the ground is not moving but they should be moving diagonally (only faster or slower in the x-direction when the ground is moving too). I tried adding a speedX variable but it doesn't change anything. Can you help me with that too?
danpost danpost

2018/3/18

#
R0mane wrote...
the problem is that the balls are moving vertically when the ground is not moving but they should be moving diagonally (only faster or slower in the x-direction when the ground is moving too).
Add speedX to offsetToGround before moving.
R0mane R0mane

2018/3/19

#
Thank you it works.
You need to login to post a reply.