Here’s a quick tutorial and some code to hyperlink a label (or some text) in cocos2d. Hopefully could be helpful for others iPhone App developers.
Firstly declare your bounding box for the label:
CGRect bbox;
Then have your code written for the label as such, and find the bounding box of the label:
CCLabel* linkLabel = [CCLabel labelWithString:@" visit us at http://www.mysite.com" fontName:@"Arial" fontSize:20]; //insert your label text here
linkLabel.position = CGPointMake(screenSize.width / 2, screenSize.height / 4.5); // position the label
[self addChild:linkLabel z:100 tag:101]; //add the label to the scene (view)
bbox = [linkLabel boundingBox]; // returns the bounding box of the label
Finally, in your touch recognition method, you add the following code.
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];//get the touch point
NSString* url = @"http://www.mysite.com"; //url for the link.
//check to see if the touch location was inside the bouncing box, and open url.
if (CGRectContainsPoint(bbox, touchLocation)) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
}
Any problems give me a shout, or if there is an easier way let us know.
