如何在Corona SDK中使多个对象弹动

嘿大家,我是 Corona sdk 的新手,想请教如何制作一些球随机地在屏幕上弹跳,我不知道代码怎么写,能否有人给我一段代码,能让球随机地在屏幕上弹跳而不会停下来。当球碰到墙壁时,球会朝相反的方向弹跳。

感谢您的帮助,我非常感激。

我尝试了这个,但它不起作用

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

能有人帮忙吗,谢谢!

点赞
用户2186639
用户2186639

你所需要做的,就是实现物理学。这是教程链接:http://developer.coronalabs.com/content/game-edition-box2d-physics-engine

2013-07-20 23:50:38
用户1605727
用户1605727

你需要在你的游戏中应用物理学。

尝试这个示例代码,它有墙和一个球。

_W = display.contentWidth
_H = display.contentHeight

local physics = require("physics")
physics.start()
physics.setGravity(0,0) --使一切浮动,零重力

--添加墙壁

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)
2013-07-21 02:23:40