4
votes

I want to make an action in my sprites when they get touched, this is the action method:

-(void) spriteEffect
{
    CCSprite *actionEffect = avatar;
    id jump = [CCJumpBy actionWithDuration:1 position: ccp(0, 0) height:50 jumps:1];
    id sequence = [CCSequence actions: jump, nil];
    [actionEffect runAction:sequence];
}

now, my problem is, that I don't know how to make the touch action connect with the sprite; should I use this?

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event

Msp are in a .png image with a .plist assigned.

3

3 Answers

2
votes
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{

    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 1) 
    {
        // Add Your Action
    }

}

OR

UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SpriteThouch)];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:gr];

and call method SpriteThouch

-(void)SpriteThouch
{
  // code here 

}
0
votes

I use CCNode+SFGestureRecognizers.h (https://github.com/krzysztofzablocki/CCNode-SFGestureRecognizers) to add gestures to my sprites.

So all that you need to do is:

  • import CCNode+SFGestureRecognizers.h

  • add sprite: CCSprite *button = [CCSprite spriteWithCGImage:....

  • add code for sprite

    button.isTouchEnabled = YES;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:selector];

    [button addGestureRecognizer:tap];

0
votes

I usually use CCMenuItemImage when I want to handle simple sprite touches :

Use the following method to create the menu item:

itemFromNormalImage:selectedImage:target:selector:

As you can see you can pass an image of the menu item (i.e sprite image) , pass the target (usually self which implements the action) and of course the method (selector) which will get called when the menu item has been touched.

If you need more help on that let me know...