The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.

ForumsProgramming ForumPong help

8 3766
Ghgt99
offline
Ghgt99
1,890 posts
Nomad

Hey guys. I just made a Pong game, [url]http://www.truploader.com/view/300391[/url] and the paddle can go up or down off the screen. I made it is AS3. Do you have any ideas how I can fix it??

  • 8 Replies
beech
offline
beech
107 posts
Nomad

not bad what you need is a 'limit' - so you'd use a condition in you movement controls that keeps the paddle >0 && <stage.stageHeight

it depend on the structure of your code as to how and 'where' you apply the condition

beech
offline
beech
107 posts
Nomad

i should clarify that by saying that you also need to include the 'height' of the paddle, for instance:

if( paddle.y>0 && paddle.y+paddle.height<stage.stageHeight ) {
//move the paddle
}

Ghgt99
offline
Ghgt99
1,890 posts
Nomad

Here is the paddle movement section.

//player paddle movement
if (Key.isDown(Key.UP)) {
playerPaddle.y -= 2;
}
if (Key.isDown(Key.DOWN)) {
playerPaddle.y += 2;
}
screen.fillRect(playerPaddle, 0xFFFFFF);
//computer paddle movement
if (velocity.x < 0) {
if (ball.y < computerPaddle.y + paddleHeight / 2) {
computerPaddle.y -= Math.abs(velocity.y) - 0.5;
} else {
computerPaddle.y += Math.abs(velocity.y) - 0.5;
}
}

beech
offline
beech
107 posts
Nomad

there you go - so one way of doing it would be:

//just storing the 'half-height' in this temp variable
var phh:Number = playerPaddle.height / 2;

if( Key.isDown(Key.Key.UP) && playerPaddle.y - phh - 2 > 0 ) {
playerPaddle.y -=2;
}

i'll leave it to you to figure out the other one

btw - Key.isDown is an AS2 method, i'm wondering how you're using that here in AS3, unless you have a class handling that elsewhere?

gaboloth
offline
gaboloth
1,612 posts
Peasant

What about
If (playerPaddle.y<0)
{
playerPaddle.y = 0
}
If (playerPaddle.y > the height of the stage )
{
playerPaddle.y = the height of the stage
}
It should work.

Ghgt99
offline
Ghgt99
1,890 posts
Nomad

My bad. I meant AS2. I am making a Tetris game in AS3, so I got confused. =P

Ghgt99
offline
Ghgt99
1,890 posts
Nomad

What about
If (playerPaddle.y<0)
{
playerPaddle.y = 0
}
If (playerPaddle.y > the height of the stage )
{
playerPaddle.y = the height of the stage
}
It should work.



The height of the stage is 2, but I have the height as 2.0, does that make a difference?
gaboloth
offline
gaboloth
1,612 posts
Peasant

In that case all the x and y have to become _x and _y, and I'm not sure that playerPaddle._x would work, but it's better asking beech about this things.

Showing 1-8 of 8