Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Is there a way I can add body parts as a property or child? — Gideros Forum

Is there a way I can add body parts as a property or child?

NinjadoodleNinjadoodle Member
edited March 2016 in General questions
Hi guys

I'm not really sure how to explain this, but here goes ...

currently when setting up a monster character, I would make a container, then add a body and an eye.

self.monster1 = ...
self.monster1Body = ...
self.monster1Eye = ...

As you see I specifically preface all the body parts with - "monster1"

Is there a way, to set the bodyparts up as properties of the monster1 container?

self.monster1 = ...
self.monster1.body = ...
self.monster1.eye = ...

I'm not really sure if this is possible and I didn't explain it very well, but hopefully someone understands what I'm asking :)

Thank you heaps in advance!

PS. I've just tried this and it works. I can't believe I did thing the long way all this time lol.

Is this approach correct or am I going to run into problems?

Thank you again!

Comments

  • antixantix Member
    Accepted Answer
    It all depends on how many monsters you are going to be managing. You might have self.monster1, self.monster2, and self.monster 3 and this will be fine but if you wanted to have MANY monsters it would be a nightmare to manage. The following code is a simple way to manage game objects. It might not be the best way but it works :)
    local monsters = {}
     
    local function newMonster(name, body, eye)
      local monster = {
        name        = name,
        body        = body,
        eye         = eye,
        blinking    = false,
        blinkTimer  = 5000,
      }
      return monster
    end
     
    local function UpdateMonsters()
      for i = #monsters, 1, -1 do
        local monster = monsters[i] -- GET NEXT MONSTER
          if monster.blinking then-- PROCESS BLINKING
            local t = monster.blinkTimer - 1
            if t == 0 then
              -- DO BLINKING STUFF HERE
              t = 5000
            end
            monster.blinkTimer = t
          end
     
        -- PROCESS MORE MONSTER STUFF IN HERE
     
      end
    end
     
    newMonster("inky", 1, 1) -- CREATE 4 MONSTERS
    newMonster("blinky", 1, 1)
    newMonster("pinky", 1, 1)
    newMonster("clyde", 1, 1)
     
    local function onEnterFrame(event)
      updateMonsters() -- UPDATE MONSTERS
    end
     
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)

    Likes: rolfpancake

    +1 -1 (+1 / -0 )Share on Facebook
  • Hi @ antix

    Thank you for the reply, this really helps. I've managed to consolidate my code a lot!

    The scope of my "level" doesn't get much bugger than 10 monsters with random blinks and a few event listeners :)
Sign In or Register to comment.