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

2018/10/24

Help with my pong ball

gustavo.kill gustavo.kill

2018/10/24

#
I need help: I'm crafting a project with some friends and I'm having difficulty with the random move of the pong ball, I've moved it using the width and length of the world, so the turn and the turn can not be used, whoever can help of a touch there that I share the code for better understanding.
    public void changeDirection(){
        Pong pong = (Pong)getOneIntersectingObject(Pong.class);
        Pong2 pong2 = (Pong2)getOneIntersectingObject(Pong2.class);         
        if(getX()>=getWorld().getWidth() - 5){
            hDirection*=-1;
        }
        if(getY()>=getWorld().getHeight() - 5){
            vDirection*=-1;
        }
        if(getX() <= 5){
            hDirection*=-1;
        }
        if(getY() <= 5){
            vDirection*=-1;
        }
        if(getY() <= 30 && isTouching(BarraLateral.class)){
            vDirection*=-1;
        }
        if(getY() <= 367 && isTouching(BarraLateral2.class)){
            vDirection*=-1;
        }
        if((getX() <= 60) && pong != null){
            hDirection*= -1;
        }
        if((getY() <= 650) && pong2 != null){
            hDirection*= - 1;
        }
    }
Note: Initial random motion only, after the score changes the behavior of the ball.
danpost danpost

2018/10/24

#
What Actor subclass is this code in and what are the Pong and Pong2 objects?
gustavo.kill gustavo.kill

2018/10/26

#
The pong and pong2 objects are basically the players, and that part of the code is the ball, so she chooses the direction.
danpost danpost

2018/10/26

#
gustavo.kill wrote...
The pong and pong2 objects are basically the players, and that part of the code is the ball, so she chooses the direction.
Oh -- okay. Add a second condition to all your if statements to only change directions when a specific direction is set. For example, your first if block can be:
if(getX()>=getWorld().getWidth() - 5 && hDirection > 0){
    hDirection*=-1;
}
This limits the changing of direction to happen only if the ball is moving right (and near the right edge). I am presuming that this is not actually going to be a standard pong game. If it was, you would not be changing directions at all 4 edges. At any rate, always check the current direction of movement before changing it. You have yet to show any movement code at all. No help can therefore be extended in that regard (unless you we just having issues with changing directions).
gustavo.kill gustavo.kill

2018/10/26

#
Ty my bro.
You need to login to post a reply.