Search code examples
iosxcodecocos2d-iphonetouchccsprite

Detect Touch on CCSprite


I'm new to cocos2d so excuse my ignorance, but I would like to know how to detect when a sprite has been touched and call a method when it has been touched.

I've defined and added my sprite like this:

CCSprite *infoButton = [CCSprite spriteWithFile: @"info.png"];
[infoButton setPosition:CGPointMake(450, 290)];
[menuBG addChild:infoButton];

I've followed various resources but they have been very vague, most of which the sprite was set in its own class.

Thanks in advance.


Solution

  • Why dont you use CCMenuItemImage?

     CCMenuItemImage* info = [CCMenuItemImage itemFromNormalImage:@"info.png" selectedImage:@"info.png" target:self selector:@selector(pressed:)];
    CCMenu* menu = [CCMenu menuWithItems:info, nil];
    menu.position = ccp(450,290);
    [menuBG addChild:menu];
    

    and another function whenever the user pressed the button..

    -(void)pressed:(id)sender
    {
    // whatever you would like to do here...
    }