简体   繁体   中英

How to create and move a group of bouncing objects on Corona sdk/solar2d?

I already found a similar question on this website but the answers didn't really help. I'm creating a game on Solar2d /Corona sdk and I'm trying to create multiple objects that bounce around the screen and even collide with each other changing the direction (just like in real life). I'm having a few issues... I tried to create some walls but I just see white lines and my objects can go past them since they only cover half the width and half the height. here is the code I used (I found it somewhere on the internet):

local leftWall = display.newRect (0, 0, 1, display.contentHeight);
local rightWall = display.newRect (display.contentWidth, 0, 1, display.contentHeight);
local ceiling = display.newRect (0, 0, display.contentWidth, 1);
local bottom = display.newRect (0, display.contentHeight, display.contentWidth, 1);

How can I set the edges of the screen as boundaries? And since I want to create several copies of the same object should I create them individually or as a group?

I'm new to the website so if I made any mistake I'm sorry!

You should use the physics library of Solar2D. (Call it with local physics = require( "physics" ) ). Then make all those walls into physics bodies using

physics.addBody( leftWall, "static", { --[[all other parameters]] } )
physics.addBody( rightWall, "static", { --[[all other parameters]] } )
physics.addBody( ceiling, "static", { --[[all other parameters]] } )
physics.addBody( bottom, "static", { --[[all other parameters]] } )

For the parameters you might want to read the documentation

Also I suggest to use display.newLine instead of display.newRect and make it very thin. It is definitely easier to manage and the code that I wrote up here doesn't change at all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM