0

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!

Austin Greco
  • 32,997
  • 6
  • 55
  • 59
elena
  • 1
  • 1

1 Answers1

0

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

frankpujo
  • 125
  • 9
  • I tried but now it only shows 3 lines. It basically divided the screen in half... any suggestions? I checked the codes several times and I don't get what is wrong with them. – elena Dec 24 '20 at 19:01
  • Adding physics bodies shouldn't change at all the displayes result – frankpujo Dec 24 '20 at 22:31
  • Exactly! That's why I don't understand this weird behavior. Anyway I found a solution. Thanks for your answer :) – elena Dec 26 '20 at 18:33