Friday, 31 May 2013

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];                                               

No comments:

Post a Comment