开发者

Accessing sprites in a NSMutableArray returns an error - simple question

开发者 https://www.devze.com 2022-12-21 17:22 出处:网络
Why doesn\'t this work? // spriteArray is an NSMutableArray int spriteWidth = [spriteArray objectAtIndex:0].contentSize.width;

Why doesn't this work?

// spriteArray is an NSMutableArray
int spriteWidth = [spriteArray objectAtIndex:0].contentSize.width;

I get the error:

Request for memeber 'contentSize' in something not a structure or union

If I change the code:

CCSprite *tempSprite = [spriteArray objectAtIndex:0];
int spriteWidth = tempSprite.contentSize.width;

Then it's okay.

I've tried casting:

int spriteWidth = (CCSprite*)[spriteArray objectAtInde开发者_开发知识库x:0].contentSize.width;

But it doesn't work either.

Is there a way to do this without creating the sprite reference?


The return type of objectAtIndex: is id, which is not a struct nor union. If you want to use casting, try

((CCSprite*)[spriteArray objectAtIndex:0]).contentSize.width

Otherwise, use a temporary variable.


I believe the . binds tighter than casting. Try

int spriteWidth = ((CCSprite*)[spriteArray objectAtIndex:0]).contentSize.width;

See this table.

0

精彩评论

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