Showing posts with label sprite. Show all posts
Showing posts with label sprite. Show all posts

Wednesday, 24 July 2013

Auto-change the physics of an object (created using Physics Editor) after a change of it's size in corona SDK

If you are using physics editor for making physics objects in your corona game, then at some point you may doubted about how to scale the physics area according to the sprite scale values... But it is very simple.

For doing such alternation, there is scaleFactor provided. You can assign physics to the object with this scale factor, as below:

    local scaleFactor = 1.0
    local physicsData = (require "shapedefs").physicsData(scaleFactor)
    local shape = display.newImage("objectname.png")
    physics.addBody( shape, physicsData:get("objectname") )


And when you change the scaleFactor, the size of physics area will automatically change.

Sunday, 9 June 2013

How to call sprite sheet animation: cocos2d

To add sprites from spritesheet and animating the frames, you can use the following code:

in .h:

        CCSprite *sprite_1;
        NSMutableArray *spriteAnimArray;

in .m:

    //In init method, add sprite frames from plist file
 
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"plistName.plist"]; 
         sprite_1 =[CCSprite spriteWithSpriteFrameName:@"sprite1.png"];
        sprite_1.position = ccp(240,240);
        [self addChild:sprite_1 z:2];

        [self spriteAnimation];        // call sprite animation function


  //SpriteAnimation function

    -(void)spriteAnimation{
        spriteAnimArray = [NSMutableArray new];

        // call sprite1 to sprite4 from spritesheet //
        for(int i=1; i<=4; s++)      
        {
            [spriteAnimArray addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]
                                                spriteFrameByName:[NSString stringWithFormat:@"sprite%d.png",i]]];
        }
       
        CCAnimation *animate = [CCAnimation animationWithFrames:spriteAnimArray delay:0.3f];
        CCRepeat *sprite_Action = [CCRepeatForever actionWithAction:
                                                    [CCAnimate actionWithAnimation:animate
                                                    restoreOriginalFrame:YES]];       

        [sprite_1 runAction:sprite_Action];
        [spriteAnimArray release];
    }

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 reset the image of a sprite: cocos2d

Cocos2d allows the programmer to reset the image of a sprite when needed. This can be done with the help of following code:




Syntax:

    [spriteName setTexture:[[CCTextureCache sharedTextureCache]
                                           addImage:@"newImageName.png"]];

How to call a sprite by tag: cocos2d

In cocos2d, you can call a sprite by it's tag. This can be done as follows:

Syntax:

      CCSprite *tempSprite =  (CCSprite *)[self getChildByTag:tagValue];

Example:

      CCSprite *tempSprite =  (CCSprite *)[self getChildByTag:10];

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