guys,
I am making a pong game,
I was wondering how can I make the ball move . I am familar with transition.moveTo. but I was wonder if there was another way to move the ball object.
any help would be appreciated.
guys,
I am making a pong game,
I was wondering how can I make the ball move . I am familar with transition.moveTo. but I was wonder if there was another way to move the ball object.
any help would be appreciated.
Transition.moveTo is a very inefficient way to move something that will be moving back and forth in different directions, I recommend setLinearVelocity().
https://docs.coronalabs.com/api/type/Body/setLinearVelocity.html
Remember: to use setLinearVelocity, the object needs to have a physics body, otherwise you will get an error.
In fact, I am also making a pong game.
@sdktester15 and I have conversed about this in other posts.
You should check all discussions out:
https://www.youtube.com/watch?v=DJvz7mkwE-g
Ok i am just doing it to learn. I will let you know how i make out
The code at those links is still worth looking at w/o SSK2
how can have the paddle follow the ball
That was discussed in the forum posts I linked above, there are two videos about it, sample code at the posts and linked in the videos.
Please review the materials already given to you.
wouldn’t linear velocity be for the paddles and not the ball?
The answer is, it depends.
i.e. It depends on how you’re implementing your game.
I used a number of techniques for moving balls and paddles in the various samples I provided.
You post are helpful. I think its a bit conplex. I have seem sample pong games in python and they seem less complicated. I am very new to programming so i really appreciate your help.
I breaking things down in to parts . First is moving the ball and bouncing off walls. Then making paddles and moving them then making the ball hit the paddle. Then keeping score. I want to use lua only no addins. I am learn linear velocity now.
I will have more questions in the future so thanks in advance
Yes, sorry. These examples were not meant to be simple in the sense you’re describing. They use SSK2, they use modules, they implement a couple of basic ‘auto-paddle’ movement types.
Good luck on this. It sounds like a pretty solid approach to learning new things about Torque.
Note: SSK2 is pure Lua.
so far I figured out how to get one paddle to move as a player.
I will figure out how to move the ball next .
Good job, keep going!
I was thinking of using transition.moveTo. in conjunction with math.random()
in order to move the ball. This way the movement of the ball will change periodically.
Does anyone know how to use the math.random feature
i figured out how to use math.random with transition.to and setLinearVelocity.
I now have to figure out collssions with the paddles and how to get the non players paddle to hit the ball
I can 't get the ball to bounce off my paddle below is my code.
I hope I can get some help on this
–
– main.lua
–
– Your code here
local physics = require(“physics”)
physics.start()
– Create a rectangle player one
myRect = display.newRect( 0, 0, 50, 100 )
–local myRect=display.newImage (“paddle1.png”)
myRect.x =100
myRect.y =125
– Add a body to the rectangle
—physics.addBody( myRect, “dynamic” )
–removed dynamic cause the paddle would move on its own. static was better
—physics.addBody( myRect, “static” )
physics.addBody( myRect, “static” ,{density = 1.0, friction = .3, bounce = .2})
– Set the linear velocity
—myRect:setLinearVelocity( 100, 20 )
---------------move paddle player#1
local function ontouch (event)
if event.phase == “ended” then
----moves it any where i click–
— transition.to (myRect,{x=event.x, y=event.y})
—just moves it up and down
transition.to (myRect,{y=event.y})
end
end
Runtime:addEventListener(“touch”, ontouch)
----draw ball
local myCircle = display.newCircle( 200, 200, 30 )
myCircle:setFillColor( 0.5 )
myCircle.x =300
myCircle.y =125
physics.addBody(myCircle,“dynamic”,{density=0.1,bounce = 0.4, friction = 1})
–physics.addBody(myCircle,“static”,{density=0.1,bounce = 4, friction = 1})
–myCircle:setLinearVelocity(-100,20)
----temp commented out
–myCircle:setLinearVelocity(math.random(-300,300),20)
--transition.to (myCircle,{x=myCircle.x-300, y=myCircle.y})
–transition.to (myCircle,{x=myCircle.x-math.random(-300,300), myCircle.y+math.random(-300,300)})
transition.to (myCircle,{x=myCircle.x-300, y=myCircle.y})
—collsion when ball hits paddle
local function ballcollison( self, event)
if ( event.phase == “began” ) then
print (“event began”)
elseif ( event.phase == “ended” ) then
print (“ball hit paddle”)
--myCircle.y =125
end
end
myRect.collision = ballcollison
myRect:addEventListener(“collision”)
myCircle.collision = ballcollison
myCircle:addEventListener( “collision” )
Transition.moveTo is a very inefficient way to move something that will be moving back and forth in different directions, I recommend setLinearVelocity().
https://docs.coronalabs.com/api/type/Body/setLinearVelocity.html
Remember: to use setLinearVelocity, the object needs to have a physics body, otherwise you will get an error.
In fact, I am also making a pong game.