Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Maze

edited October 2014 in Questions Posts: 175

Hi kind folks, I'm in the process of creating a fun (maybe) game about mazes, but I have problem with clearing array of physical objects, could you help me out a couple of lines of code? I would be very grateful to your if you suggestion/solution helps

P.s Should I attach the code?

demonstration

Tagged:

Comments

  • IgnatzIgnatz Mod
    Posts: 5,396

    Yes, code helps

  • edited October 2014 Posts: 175

    @Ignatz i make it like dave1707's example of multiple objects, so i attach his example .
    And this is my main class . I tried call body:destroy() func in line 53 , but this attempt didn't work

  • dave1707dave1707 Mod
    Posts: 7,909

    @lupino8211 To get rid of physics objects you don't want anymore, just use
    body:destroy() . If they're also in a table, remove that entry after the destroy.

  • dave1707dave1707 Mod
    Posts: 7,909

    @lupino8211 Here's an example. I create 200 physics objects in a table, destroy them and remove them from the table when they get to the line. The number below the line shows how many objects are in the table.


    displayMode(FULLSCREEN) supportedOrientations(PORTRAIT_ANY) function setup() tab={} for z=1,200 do b=physics.body(CIRCLE,20) b.x=math.random(WIDTH) b.y=math.random(HEIGHT,5*HEIGHT) b.gravityScale=.1 table.insert(tab,b) end end function draw() background(40,40,50) fill(255) text(#tab,WIDTH/2,250) stroke(255) strokeWidth(3) line(0,280,WIDTH,280) for a,b in pairs(tab) do -- loop to show and check objects ellipse(b.x,b.y,40) if b.y<300 then b:destroy() -- destroy object b.info="rem" -- set to indicate it needs to be removed end end for z=#tab,1,-1 do -- loop to remove them from the table if tab[z].info=="rem" then table.remove(tab,z) -- remove from table end end end
  • edited October 2014 Posts: 175

    @dave1707 thank you very much

  • edited October 2014 Posts: 175

    @dave1707 wtf is wrong?


  • edited October 2014 Posts: 175

    @dave1707 wait. I think i have got it

    Upd : no, i dont

  • IgnatzIgnatz Mod
    Posts: 5,396

    @lupino8211 - should be b:destroy()

  • @dave1707 @ Ignatz , thank you for you suggestions, but I decide not to mess around with physics no more

  • dave1707dave1707 Mod
    Posts: 7,909

    @lupino8211 A lot of what I showed will be used even if you don't use physics objects. So don't give up on physics just because you don't understand it. Physics objects are used in a lot of games and make the games easier to code then if you tried to do it without physics objects.

  • edited October 2014 Posts: 1,976

    @lupino In that for loop, a is the index of the variable, and b is the variable itself. So since the variable is your physics body, you should call b:destroy(). a is just the information of where the variable is in the table.

  • @dave1707 thank you, hope everything else will work good

  • @SkyTheCoder thank you, but i knew this. It's typo :)

Sign In or Register to comment.