Accessing properties x and y of CCSprite from NSMutableArray
If I have a sprite in a NSMutableArray of sprites in Cocos2d and need to access the x and y values of a specific sprite how can I do that?
[ar开发者_开发问答ray objectAtIndex:0].position.y // or .x for x value
doesn't work when trying to access the element of y from a specific sprite in the array. But, I can not think of any other way to get either the x or y value of a CCSprite. How can I get the x and y values of NSMutableArray containing CCSprites?
(And I can't find any getX or getY methods either in the doc...)
You need to cast the value returned by -[NSArray objectAtIndex:]
so the compiler knows it’s a CCSprite *
object:
((CCSprite *)[array objectAtIndex:0]).position.y;
Alternatively, you could store the return value in a CCSprite *
variable:
CCSprite *sprite = [array objectAtIndex:0];
and then use this variable to obtain the corresponding position:
sprite.position.y
精彩评论