I am making a game, and I want a number to be added to a label when the user taps a button
I am making a game, and I want a number to be added to a label when the us开发者_开发知识库er taps a button. Like it adds 5 points when the button.
Declare IBOutlet UILabel *scoreLabel;
and implement this method:
-(IBAction)buttonPressed {
scoreLabel.text = [NSString stringWithFormat:@"%d",[scoreLabel.text intValue]+5];
}
Make sure you set up the connections in the IB (scoreLabel points to the label and the button triggers the action.
In Cocos2d you can create labels that can have a numerical value attributed to them. The pseudocode for this question would be as such:
int points = 5;
UIButton *pushButton;
UILabel *Label;
-(void)OnButtonPress{
points =+ 5 //add 5 points
}
label.text = [NSString stringWithInt:points];
For a tutorial on Cocos2d labels, please see: http://www.usightread.com/2009/06/labels-menus-and-options-in-cocos2d-iphone/
精彩评论