Reputation: 373
I have the following code in my game:
int x = 50;
int y = 400;
for (int i = 1; i < 30; i++) {
if (x+54 > self.boundingBox.size.width) {
x = 50;
y -= 70;
ccDrawCircle(CGPointMake(x, y), 20, 3.14, 100, NO);
} else {
ccDrawCircle(CGPointMake(x, y), 20, 3.14, 100, NO);
x += 72;
}
}
How can I get each of these circles to react to touches? Specifically I'm thinking about giving them a button press effect (shrinks on touch, resize on end touch), and also change colors on touch.
Upvotes: 0
Views: 263
Reputation: 24760
If using Cocos, it is easiest to make your circles CCSprites and then respond to touches on those sprites, using this method in your layer:
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
Then, iterate through your sprites using a common technique like this:
isTouchHandled= CGRectContainsPoint([sprite boundingbox], [[CCDirector sharedDirector] convertToGL:[touch locationInView: [touch view]]]);
If isTouchHandled
is TRUE, then you can do what you need to do with that sprite or anything else.
Note that you can use this method either in the layer itself or in a subclass of the sprite, and in whichever you choose, you must register the node with the touch dispatcher with this in the init
or somewhere:
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
Upvotes: 1