I'm creating a game (or app, depending on how you would define it...) where I'm moving objects (with sprites) on the screen.

I've got this to half-work in both scenarios, but I can't seem to get it to work completely using only one...

My problem is, I don't know where the touches should be handled for the objects. I have a separate layer that has all the objects on that layer. I put all the touch logic on that layer and it works fine, but I can only move 1 object at a time (I have to declare a currentObject variable to keep track of what is moving) and I don't know how to see if a touch is handled on the layer without a currentObject variable. Here's my touch began method:

-(BOOL)ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event{
       BOOL isTouchHandled = CGRectContainsBorder([current.sprite boundingBox], lastTouchLocation);

       return isTouchHandled;

I also tried to have all the touch control in the objects itself, which let me move all the objects separately, but I couldn't have the objects react to any of the layer variables or events (I can't figure out how to get the objects to see what the current states are of the variables on the layer). the code for that touch began is:

-(BOOL)ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event{
       BOOL isTouchHandled = CGRectContainsBorder([sprite boundingBox], lastTouchLocation);

       return isTouchHandled;

The only difference between them is that the layer is accessing the instance variable of the object, and the object accesses its own variable.

Note: these functions aren't being implemented at the same time, I'm trying one and seeing if it works and then trying the other and seeing if it works...

So, how could I fix this? Is there a way to be able to move all the objects independently? I have events that moving the objects react to once the move over a certain point, but I want the objects to be able to be moved after all the events happen as well and I can either get them to react to the events and move the current to the next object (control of touch is on the layer) or I can move all the objects independently but I can't access the layer's events (control is with the object)...


It depends on what you want to do... if the user should be able to move one object at a time, it should be sufficient to find the closest object and move that upon movement.

If you need to handle multiple touches, you should first and foremost set:

// put this in your AppDelegate's applicationDidFinishLaunching* method
[glView setMultipleTouchEnabled:YES];

In any case you might also want to implement your own ccTouchMoved instead of just ccTouchBegan, to keep track of movement. The cocos2d documentation has a nice summary of the touch delegates and the available methods.

Update: Just FYI: You can implement the touch controls in a separate class by implementing the CCStandardTouchDelegate or CCTargetedTouchDelegate protocol and add it as delegate to the CCTouchDispatcher. This way you can decouple your input logic from your display classes.. so maybe the input handler would know about the layer and the sprites, but layer and sprites won't know about the input handler. This will make up for a cleaner architecture and less cluttered classes.

| improve this answer | |
  • \$\begingroup\$ I do have the CCTouchMoved methods defined, I just didn't include them in the question :P \$\endgroup\$ – OghmaOsiris Jan 2 '12 at 23:24
  • \$\begingroup\$ @OghmaOsiris Did I misinterpreted your question then? What exactly is the issue you're facing? \$\endgroup\$ – bummzack Jan 2 '12 at 23:29
  • \$\begingroup\$ If I have the touch logic in the object itself, I can move them all freely, but I can't have the objects react to the layer events. But if I have the touch logic in the layer, then I can only move one at a time, but the events are happening them at the same time. \$\endgroup\$ – OghmaOsiris Jan 2 '12 at 23:35
  • \$\begingroup\$ @OghmaOsiris You should be able to get the same results, no matter where you implement your touch logic. It's all a matter of how you interpret and apply these touches. In case of the layer, you might not only check one single sprite (that current variable you speak of), but you would have to check all your sprites if they have been touched and return YES if that's the case... \$\endgroup\$ – bummzack Jan 3 '12 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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