Search code examples
objective-curlwebcocos2d-iphoneccsprite

Access a website when tapping an image in cocos2d


I am making an app using cocos2d for using images to display data. I want to make the display image as a "link". So, when the user taps the image, it opens up a particular website. I am using cocos2d and objective-c for the first time. I don't know how to proceed with this

Any help would be much appreciated.

Thanks!


Solution

  • Try this:

    -(void)createImageButton
    {
            CCSprite *image_1   = [CCSprite spriteWithFile:@"Image_1.png"];
            CCSprite *image_2   = [CCSprite spriteWithFile:@"Image_2.png"];
    
            CCMenuItemSprite *imageBtn = [CCMenuItemSprite itemFromNormalSprite: image_1
                                           selectedSprite:image_2
                                                   target:self
                                                 selector:@selector(imageBtnPress:) ];
    
            imageBtn.position = ccp(width*0.5f, height*0.5f);
    
    
            CCMenu  *menu = [CCMenu menuWithItems: imageBtn, nil];
            menu.position = ccp(0.0f, 0.0f);
            [self addChild: menu z:100];
    }
    
    -(void) imageBtnPress:(id)sender
    {
        //here open website link
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.apple.com"]];
    }