Reputation: 9767
Using Cocos2D to create a shoot-em-up bullet hell style game. One finger drags to move the ship and there are fire layers in the corner.
From my main layer, I detect ccTouchesBegan, ccTouchesMoved and ccTouchesEnded. I pass a CGPoint into my logic controller.
If a touch down point is inside one of the 2 "fire button" layers, then I trigger a fire event. Otherwise I set a new location for the player ship. When a touch moves outside of the fire layer areas, I set a new location for the player ship.
Problem: Dragging a finger from within the fire layer into non-fire layer space causes the ship to jump to that point because of the "touch moves set player location" rule.
How can I architect this better?
Perhaps I need an array of "touch objects", and nullify them when crossing the boundary of the fire layer. I feel like I am missing some important concept about touch handling here that, if understood, would give the control I want without having to hack a "touch object" class that compares where you're tapping to an array of locations.
Please advise. Thanks!
(Yes, I saw: How do I limit touch handling to one layer when layers overlap? . That does not answer my question. The rectContainsPoint is how I determine if a touch is within or out of a fire layer).
Upvotes: 1
Views: 287
Reputation: 119242
Can you set a flag (a BOOL ivar) in touchesBegan to indicate that the touch began in the fire area, and if this flag is set, dont move the ship? You would re-set it at touchesEnded.
That sounds too simple, so I'm probably missing something.
I was indeed. To handle multi touch you would need an array as you guess in the question - on touches began, if the touch was in the fire zone, add it to the array, then don't allow that touch to subsequently move the ship. Remove on touches ended. I don't see anything wrong or hacky about this, except for this note in the UITouch docs:
A UITouch object is persistent throughout a multi-touch sequence. You should never retain an UITouch object when handling an event. If you need to keep information about a touch from one phase to another, you should copy that information from the UITouch object.
But the persistence seems to support what you want to do. I don't know if a different class is used in cocos2d but the principle remains the same.
Upvotes: 1