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

2013/12/29

Kicking A Ball

Moynzy Moynzy

2013/12/29

#
Hello users of Green Foot, it's a pleasure to meet you all. I have currently made a game where the player can move the ball in 8 directions. But I want the ball to move an x amount of pixels when the space bar has been pressed. if ( space bar has been pressed && there is collision with the ball from the right) //Move the ball 50pixels to the right If (space bar has been pressed && there is collision with the ball from the left) // move the ball 50pixels to the left Thank you, can i have some tips on how this can be achieved?
shrucis1 shrucis1

2013/12/29

#
Moynzy, I think that here you're trying to understand how to do collision detection, correct? Here's a snippet of code that I would use to achieve this in your Ball class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
int speed = 5; //This determines the player's movement speed
 
if(Greenfoot.isKeyDown("right")) {
    //When the right key is pressed down, the player moves to the right
    setLocation(getX() + speed, getY());
    if(isTouching(Wall.class)) { //Here I assume that the barriers are an object call 'Wall'
        //If the player just tried to moved into a wall, go left
        setLocation(getX() - speed, getY());
    }
}
 
if(Greenfoot.isKeyDown("left")) {
    //When the left key is pressed down, the player moves to the left
    setLocation(getX() - speed, getY());
    if(isTouching(Wall.class)) { //Here I assume that the barriers are an object call 'Wall'
        //If the player just tried to moved into a wall, go right
        setLocation(getX() + speed, getY());
    }
}
 
if(Greenfoot.isKeyDown("down")) {
    //When the down key is pressed down, the player moves down
    setLocation(getX(), getY() + speed);
    if(isTouching(Wall.class)) { //Here I assume that the barriers are an object call 'Wall'
        //If the player just tried to moved into a wall, go up
        setLocation(getX(), getY() - speed);
    }
}
 
if(Greenfoot.isKeyDown("up")) {
    //When the up key is pressed down, the player moves up
    setLocation(getX(), getY() - speed);
    if(isTouching(Wall.class)) { //Here I assume that the barriers are an object call 'Wall'
        //If the player just tried to moved into a wall, go down
        setLocation(getX(), getY() + speed);
    }
}
What this essentially does, is if a certain key is pressed, move in that direction. If, after moving, we've ran into a Wall, then move back, because we can't walk into a wall. If you don't understand the syntax I'm using, please let me know so I can explain clearly how each line works. Also, what you described above,
Moynzy wrote...
if ( space bar has been pressed && there is collision with the ball from the right) //Move the ball 50pixels to the right If (space bar has been pressed && there is collision with the ball from the left) // move the ball 50pixels to the left
Would not work, because if the space bar is pressed, it would try to move in both directions at once, giving it the appearance that it was not moving at all. Also, I assume you mean && there is not a collision with the ball from the right/left. I hope that this answers your question, and if you don't understand the code I put, please don't hesitate to ask what it means.
danpost danpost

2013/12/29

#
I think what you mean is that if a ball is at either side of the player and the space bar is pressed, that the ball is to be 'pushed' 50 pixels away. First, confirm my understanding. Then, is the ball to move progressively toward the point 50 pixels away or instantly appear at that point? If progressively, then with what behavior (describe how you wish the animation to transpire). These are the kind of questions you need to consider before the actual coding.
shrucis1 shrucis1

2013/12/29

#
Oh, it seems I misunderstood what he was saying. Thank you for the clarification, danpost, I wasn't reading what he asked very clearly:
Moynzy wrote...
I have currently made a game where the player can move the ball in 8 directions...
Sorry if I caused any confusion.
Moynzy Moynzy

2013/12/30

#
Thank you for the comments, i scrapped the idea.
You need to login to post a reply.