SeanDp32
SeanDp32

Reputation: 309

how to make a multiple pong balls bounce around

Hey guys i want to put two or more pong balls on screen and make them bounce round. I want the pong game to be kinda different from the traditional pong style game by making the paddle collect the ball and the ball can bounce around all around the screen I don't want the ball bouncing around crazy it want it to make the ball bounce around at a slow and nice apse. I am trying to make the wall push the balls in the opposite direction. can you guys please help thanks

here is the code

 function update(event)
    -- Ball Movement
    ball.x = ball.x + xSpeed
    ball.y = ball.y + ySpeed


if(ball.x < 0) then ball.x = ball.x + 3 xSpeed = -xSpeed end--Left
if((ball.x + ball.width) > display.contentWidth) then ball.x = ball.x - 3 xSpeed = -xSpeed end--Right
if(ball.y < 0) then ySpeed = -ySpeed end--Up
--if(ball.y > 0) then ySpeed = ySpeed end--Up

if((ball.y + ball.width) > display.contentWidth) then ball.y = ball.x + 300 ySpeed = -20
 end--Right

end



function bounce(e)
    ySpeed = -5
    -- Paddle Collision, check the which side of the paddle the ball hits, left, right
    if((ball.x + ball.width * 0.5) < paddle.x) then
        xSpeed = -5
    elseif((ball.x + ball.width * 0.5) >= paddle.x) then
        xSpeed = 5
    end
end


paddle:addEventListener('collision', bounce)    

Runtime:addEventListener('enterFrame', update)

This code is not working for me can someone give me some code that works. Also a problem with this code is the balls stick up on the walls. I want to have multiple balls bouncing and moving in opposite direction when they hit the wall. I am coming from Game Salad programming in Game Salad i would use change velocity when the ball colloid with the wall but in corona its more harder and i can't figure this out i am stuck. I am new to Corona ,and i read the forums and tutorials but no luck.

I would love to get some help with this thanks ...:)

Upvotes: 1

Views: 685

Answers (1)

Doğancan Arabacı
Doğancan Arabacı

Reputation: 3982

It's hard and inefficient to achieve that in this way. You should use physics libraries functions for this kind of purposes. You should create physic objects with { friction = 0 } property and then you can change ball's speed with density part. After creating objects you should use applyForce functions to start movements of balls.

Upvotes: 1

Related Questions