Showing posts with label z index. Show all posts
Showing posts with label z index. Show all posts

Friday, 31 May 2013

How to change the z order of a sprite during execution: cocos2d

In cocos2d, the z order of any sprite can be changed programmatically during execution.

Syntax:

    CCSprite * Sprite = [CCSprite spriteWithFile:@"spriteFileName.png"];
    Sprite.position = ccp(250,150);
    [self addChild:Sprite z:1];
        ...
        ...
        ...
    [self reorderChild:Sprite z:5];        // reordering position
                                                        // z  from 1 to 5 means bottom to top 

How to add and position a sprite in a scene: cocos2d

You can add an image/sprite in your cocos2d scene as below:


    // Add the sprite from file
    CCSprite *spriteName = [CCSprite spriteWithFile:@"a.png"]; 

    // placing it in a particular position
    spriteName.position = ccp(100, 100);          
                           
    // giving a tag value to the sprite (optional)
    spriteName.tag = 10;                         

    // adding the sprite as a child of the layer (z indicates the z-index/order of layer)
    // z = -1: bottom layer;  higher z-index = layer on topper position                  
    [self addChild:spriteName z:1];