Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm working in iOS.

I am trying to make a very simple 2D environment where there are some basic shapes you can drag around with your finger. These shapes should interact in various ways when dropped on each other, or when single-tapped versus double-tapped, etc.

I don't know the name for the design pattern I'm thinking of. Basically, you have a bunch of arrays named after attributes, such as "double-tappable" or "draggable" or "stackable". You assign these attributes to the shapes by putting the shapes in the arrays.

So, if there's a double-tap event, the code gets the location of it, then iterates through the "double-tappable" array to see if any of its members are in that location. And so on: every interactive event causes a scan through the appropriate array or arrays.

It seems like that should work, but I'm wondering if there's a better pattern for the purpose.

share|improve this question
1  
i don't know iOS but a complete scan or a complete iteration through each array at each call sounds a really bad idea, really bad for performances and without a real logic, you are supposed to do something more "atomic" when you receive an input from the user, you should also abstract more and shouldn't talk about data containers when you are planning your actions for your game; plan in terms of actions-in-game and not in terms of data. You also have to specify the language that you would like to use. –  user827992 Aug 27 '12 at 18:14
    
@user827992: Thanks for the response. Can you put it in an actual answer, so we can talk about it there? Is there some disadvantage to that? I'm using the native iOS language, Objective-C. –  Le Mot Juiced Aug 27 '12 at 18:39

2 Answers 2

up vote 0 down vote accepted

The Decorator pattern is probably what you are looking for. To quote Wikipedia: "The Decorator pattern can be used to extend (decorate) the functionality of a certain object at run-time, independently of other instances of the same class, provided some groundwork is done at design time." Please refer to the full article for implementation details.

Define DraggableDecorator, StackableDecorator, etc. and make your objects decorable as described in the article. At run-time, you can then attach whatever Decorator you want to your objects.

share|improve this answer
    
I really like this pattern, thanks for suggesting it. In Objective-C it seems like you could get the same functionality using categories. Do you see any drawback to using categories instead of the kind of decorators described on the Wikipedia page? –  Le Mot Juiced Aug 28 '12 at 18:05
    
My understanding of Objective-C's categories is only based on a five minutes Google search, but it seems as if categories are just a language-specific way of implementing the Decorator pattern. If that is true, using categories will most likely have advantages compared to your own class-based solution, as it is embedded in the language itself. –  BerndBrot Aug 28 '12 at 18:41
    
Actually, I realize after reading more that categories are a very different thing, especially since they can't be dynamically assigned. This is a very helpful suggestion, thanks for turning me on to this pattern. –  Le Mot Juiced Sep 4 '12 at 3:17

This might work for a small number of objects I suppose, and be convenient to code. But if you plan on having a large number of objects or shape variants I would suggest creating classes for each attribute (draggable / stackable / etc.) and have the shapes inherit the attribute classes that are to be associated with them. It would save you a lot of memory and processing on a larger scale. This method would also make the code more organized for modifying the behaviors of the attributes, it could save you a lot of time in the long run.

:)

share|improve this answer
    
Then I'd have to know in advance what attributes were possible for each object, wouldn't I? They wouldn't be dynamically assignable. –  Le Mot Juiced Aug 27 '12 at 19:43
    
You could implement Draggable, Stackable, etc. by means of the Decorator pattern, which makes it possible to assign these behaviors dynamically. –  BerndBrot Aug 27 '12 at 20:10
    
@BerndBrot: can you give me any pointers on the Decorator pattern? Also, would you mind re-posting that comment as an actual answer, so that I can choose it as best answer if it turns out to be? –  Le Mot Juiced Aug 27 '12 at 21:03
    
Yes you would have to know in advance for this method. Another thing you could do to save processing but not memory (the memory would be roughly the same) is to have an array of attributes inside the shape that you have methods to modify. Thus if object A is double-tapped you don't have to search the entire list of double-tap-able objects to see if it's there, you only have to check it's attributes to see if double-tap is an option for that specific shape. –  Joseph Lormand Aug 27 '12 at 21:11
    
@JosephLormand: I have to go read up on the Decorator pattern to get a better handle on this, but I wonder if, in your opinion, the "array of attributes" pattern is better for this purpose than the Decorator pattern? –  Le Mot Juiced Aug 28 '12 at 15:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.