开发者

variable always equals zero

Hi dear developers! I'm just wrote my first iphone ga开发者_运维技巧me. But i have faced an information output problem below

myStr = [NSString  stringWithFormat:@"Best Score:", counter];

The variable "counter" each time changes value at hit in the enemy. But in the information output I receiving always a zero, like "Best Score: 0". Help to understand and fix that problem.

Class Texture & Sprite I've got from John Ward from www.karmatoad.co.uk

Some code here:

myStr = [NSString  stringWithFormat:@"Best Score:", counter];
_Texture = [[Texture alloc] initWithText: myStr fontSize:24];
_Sprite = [[Sprite alloc] initWithTexture: _Texture];
[_Sprite drawSprite];


try:

myStr = [NSString  stringWithFormat:@"Best Score: %d", counter];

and %d is an integer type if counter is float

myStr = [NSString  stringWithFormat:@"Best Score: %f", counter];


use below

myStr = [NSString  stringWithFormat:@"Best Score: %d", counter];


As others have suggested, you are missing the placeholder for your variable. I just wanted to provide some background:

NSString supports the format characters defined for the ANSI C function printf(), plus %@ for any object.

In format strings, a % character announces a placeholder for a value, with the characters that follow determining the kind of value expected and how to format it, i.e. format specifiers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜