Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
interacting with object classes — Gideros Forum

interacting with object classes

boriskeyboriskey Member
edited July 2013 in Game & application design
sorry, guys, if this is a dumb question. I created a class Door that will put a sprite on stage for a door. When players touches the door, it will play the animation to open the door.

I spawn 10 doors on the same stage and now want to implement a logic as follows:
1) only 3 doors can be opened at the same time
2) when the 4th door will be opening, 3 other doors will close (the animation for closing the doors will start playing when player touches the 4th door)

now my question :) Where do I implement the logic like that? is that implemented within the Door class itself or from the calling code? Do I need to use events to implement 2)?

I am struggling to understand the concept basically how to interact with objects and make them work together.

Comments

  • OZAppsOZApps Guru
    edited July 2013
    you might have to have a wall object that has the doors contained and everytime you open a door, the wall keeps count of the doors, then you can close the first opened door to facilitate the latest opened door.

    I guess right now you are using the main to host the 10 doors, right? You need an objec outside of door to keep track, the door will only be open or closed state.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • thanks for your response! where would I handle ontouch events in this case? is that going to be in the wall object?

    yeah for now I create doors from main but I just started :) I was going to use scene manager once I come up with the base structure for classes.
  • ar2rsawseenar2rsawseen Maintainer
    @boriskey actually a scene manager is the answer, you would have to create a separate scene class for it and it could be the place to handle touch events and multiple doors :)

    When I do single scene prototypes I always wrap them in a class as I would when using scene manager, so I then could easily bind it to scene manager when needed and it also helps to structure the code better :)
  • talistalis Guru
    edited July 2013 Accepted Answer
    if you can share your code we can comment better i guess.

    But let me try to tell how will i implement;
    -In the door class i will create all events. (On touch etc..)
    -Door object will have a state variable called "open" , bool, true false.
    -On thouch event while it is changing the state from closed to open it will push object in a global stack.
    ( http://en.wikipedia.org/wiki/Stack_(abstract_data_type) )
    -On touch event off door before pushing it will check the stack if there are more than 3 object in the stack pop out the first object in the stack and change the state to closed.
    -Stack will be implemented as FIFO. (First in first out)

    -In the main i will create array of objects from door class like this.
    (Or in a loop)
    objdoor[1]=door.new(anything you want to pass a variable like coordinates etc)
    objdoor[2]=door.new(anything you want to pass a variable like coordinates etc)
    objdoor[3]=door.new(anything you want to pass a variable like coordinates etc)
    objdoor[4]=door.new(anything you want to pass a variable like coordinates etc)
    objdoor[5]=door.new(anything you want to pass a variable like coordinates etc)
    .......


    Of course this logic can change according to main goal of the game. This is just a simple solution and i am sure not the optimized one:D

  • My version
    "-In the door class i will create all events. (On touch etc..)"
    -Scene with code, that control doors
    -Generic List of objects, that's implement Enumerator pattern
    -And some consumers\predicates for List of Doors
    function Door:onClick(event)
      --checks
      throw ON_OPEN_DOoR event
    end
     
    function Scene:init()
      self.Doors = List.new()
      --feel self.Doors
      self.addEventListener(ON_OPEN_DOOR.....)
    end
     
    function Scene:onOpenDoor(door)
      local counter =  OpenDoorCounter.new()
      self.Doors:enumerate(counter)
      if (counter  == 3) then
         self.Doors:enumerate(closePredicate)
      end
     
      door:open()
    end
     
    function closePredicate(door)
      door:close()
    end
     
    function PpenDoorCounterPredicate:init()
      self.Counter = 0
    end
     
    OpenDoorCounterPredicate = Core.class(Predicate)
     
    function OpenDoorCounterPredicate:consume(door)
      if (door:isOpen()) then
        self.Counter = self.Counter + 1
      end
    end
  • In this modern world, just make them all arch ways, and you won't need to code for open and closing doors :D

    Likes: talis

    +1 -1 (+1 / -0 )Share on Facebook
  • if you can share your code we can comment better i guess.

    But let me try to tell how will i implement;
    -In the door class i will create all events. (On touch etc..)
    -Door object will have a state variable called "open" , bool, true false.
    -On thouch event while it is changing the state from closed to open it will push object in a global stack.
    ( http://en.wikipedia.org/wiki/Stack_(abstract_data_type) )
    -On touch event off door before pushing it will check the stack if there are more than 3 object in the stack pop out the first object in the stack and change the state to closed.
    -Stack will be implemented as FIFO. (First in first out)


    Of course this logic can change according to main goal of the game. This is just a simple solution and i am sure not the optimized one:D

    i like the stack idea - the only thing I am still puzzled how to access stack (which I guess created outside of the door class) from the door class? of course nothing prevents me in Lua to do that, but if I remember OOP concepts, it is a no-no to access external objects from the class.
  • In this modern world, just make them all arch ways, and you won't need to code for open and closing doors :D
    i will think about it :)

  • @boriskey actually a scene manager is the answer, you would have to create a separate scene class for it and it could be the place to handle touch events and multiple doors :)

    When I do single scene prototypes I always wrap them in a class as I would when using scene manager, so I then could easily bind it to scene manager when needed and it also helps to structure the code better :)
    Hi ar2rsawseen, so would it be the same thing as with your button class? when the button dispatches the event and you handle it from a scene class?
  • My version
    "-In the door class i will create all events. (On touch etc..)"
    -Scene with code, that control doors
    -Generic List of objects, that's implement Enumerator pattern
    -And some consumers\predicates for List of Doors
    very interesting, thanks!

  • @boriskey you are right it is not fully OOP logic but it works:D
    If you want you can create a stack class also.
Sign In or Register to comment.