views:

98

answers:

1

Sorry if this is answered somewhere else, but I couldn't find it.

It's pretty simple, I want to set the Z-order of the sprites I create in Objective-C, specifically in Cocos2D.

This is the error I get when trying to build the following code:

CCSprite *mySprite = [CCSprite spriteWithFile:@"mySpriteImage.png" rect:CGRectMake(0, 0, 96, 24)]; mySprite.zOrder = 0;

'...220: error: object cannot be set - either readonly property or no setter found'

Z-Order must be able to be set somehow - can it only be set on the line of instantiation and not after it's been created? Do I have to create a setter method for an attribute for CCSprite? Why wouldn't it already have those methods?

Thanks for your help, -JJR

A: 

Figured it out:

When adding the sprite to self, need to add a parameter:

CCSprite *mySprite = [CCSprite spriteWithFile:@"mySpriteImage.png" rect:CGRectMake(0, 0, 96, 24)];

[self addChild:mySprite z:1];

z = 0 is background, the highest z index will be on top of the other sprites

-JJR

JJR
you can also use reorderChild method to change its z order on the fly
GamingHorror