问题
Hey guys I am new to Corona sdk and would like some help with make some balls to bounce around the screen randomly, I don't know the code for this so could someone give me a piece of code that would make the balls bounce randomly around the screen with out stopping or anything. Also when they hit the wall the ball would go in opposite direction.
Thanks for you help I thank you a million.
I tried this but it don't work
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
Can someone help thanks
回答1:
All you have to do is to implement physics. Here is the tutorial : http://developer.coronalabs.com/content/game-edition-box2d-physics-engine
回答2:
You need to apply physics in your game.
Try this sample code, it has walls and a ball.
_W = display.contentWidth
_H = display.contentHeight
local physics = require("physics")
physics.start()
physics.setGravity(0,0) --To make everything float, zero gravity
--Lets add walls
local left_wall = display.newRect(0,0,1,_H)
physics.addBody(left_wall,"static")
local right_wall = display.newRect(_W-1,0,2,_H)
physics.addBody(right_wall,"static")
local top_wall = display.newRect(0,0,_W,2)
physics.addBody(top_wall,"static")
local bottom_wall = display.newRect(0,_H,_W,2)
physics.addBody(bottom_wall,"static")
local ball = display.newCircle(math.random(100,_W-100),math.random(100,_H-100),10)
physics.addBody(ball,"dynamic",{bounce = 1, friction = 0})
ball:setLinearVelocity(900,1500)
来源:https://stackoverflow.com/questions/17766773/how-to-make-multiple-objects-bounce-around-in-corona-sdk