iDevGames Forums
Scaling Problem in Cocos2d - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: Scaling Problem in Cocos2d (/thread-8923.html)



Scaling Problem in Cocos2d - Jerm #1 - May 5, 2011 01:08 PM

In my Weapon class to create and animate it:

Code:
-(id) initWithWeapon
    {
        // Load the Texture Atlas sprite frames, this also loads the Texture with the same name.
        CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
        [frameCache addSpriteFramesWithFile:@"weapon1.plist"];
        if ((self = [super initWithSpriteFrameName:@"Gun_image.png"])) {
            // create an animation object from all the sprite animation frames
            CCAnimation* anim = [CCAnimation animationWithFrame:@"Gun" frameCount:30 delay:0.08f];
    
            // run the animation by using the CCAnimate action
            CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
            CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];
            [self runAction:repeat];
        }
        self.scale = 0.35f;
        return self;
}

This is the the method called above that handles the animation:

Code:
// Creates an animation from sprite frames.
    +(CCAnimation*) animationWithFrame:(NSString*)frame frameCount:(int)frameCount delay:(float)delay
    {
        // load the weapon's animation frames as textures and create a sprite frame
        NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];
        for (int i = 0; i < frameCount; i++)
        {
            NSString* file = [NSString stringWithFormat:@"%@%i.png", frame, i];
            CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
            CCSpriteFrame* frame = [frameCache spriteFrameByName:file];
            [frames addObject:frame];
        }
    
        // return an animation object from all the sprite animation frames
        return [CCAnimation animationWithFrames:frames delay:delay];
    }

So my problem is that when I scale the instance of the Weapon class (like I show above - self.scale = 0.35f) it scales down to left bottom corner, almost like the anchor point is set to [0.0,0.0] instead of [0.5,0.5] and I want it to just scale from the center of the sprite. I have put in some NSLogs and it says the anchor point is at [0.5,0.5]. Can anyone help me figure this out?


RE: Scaling Problem in Cocos2d - SethWillits - May 5, 2011 01:26 PM

Are you scaling before the anchor point is set to {0.5, 0.5}? (You're not setting it here, but I assume it's {0.5, 0.5} by default anyway.)


RE: Scaling Problem in Cocos2d - Jerm #1 - May 5, 2011 01:36 PM

It is defaulted to [0.5,0.5] but I still tried setting it to that just to be sure. I actually messed around with the anchor point for awhile–changing it from [0.0,0.0] to [0.5,0.5] to [1.0,1.0]...nothing changed or helped.


RE: Scaling Problem in Cocos2d - skyhawk - May 6, 2011 02:00 PM

Tested in my app, and scaling is definitely happening appropriately based on where I have the anchor point set


RE: Scaling Problem in Cocos2d - Jerm #1 - May 9, 2011 02:29 PM

Any suggestions?


RE: Scaling Problem in Cocos2d - SethWillits - May 9, 2011 04:46 PM

Step through with the debugger into the Cocos2d code and trace what happens and why. That's as good as it gets.


RE: Scaling Problem in Cocos2d - Jerm #1 - May 17, 2011 04:17 PM

— Problem solved —

I must say, I was a little frustrated with this so I left it alone for awhile thinking that maybe after some time had passed I could meander over to it again and perhaps find a solution...THE STRATEGY WORKED!

Originally I tried scaling it in the Weapon class, like I show, and also in the gamelayer class where I make an instance of the Weapon class and in both cases the image was only scaling down to the bottom left corner—I also was making sure to set the anchor point to [0.5f,0.5f].

So this time I tried setting the anchor point and scaling it after it was added to the screen and not before:

Before -
Code:
WeaponClass *theWeapon = [WeaponClass weapon];
theWeapon.position = ccp(theScroll.viewSize.width * 0.5f,theScroll.viewSize.height * 0.5f);
theWeapon.anchorPoint = ccp(0.5f,0.5f);
theWeapon.scale = 0.5f;
[theScroll addChild:theWeapon];

After -
Code:
WeaponClass *theWeapon = [WeaponClass weapon];
theWeapon.position = ccp(theScroll.viewSize.width * 0.5f,theScroll.viewSize.height * 0.5f);
[theScroll addChild:theWeapon];
theWeapon.anchorPoint = ccp(0.5f,0.5f);
theWeapon.scale = 0.5f;


I feel like kicking myself for not thinking to try such a simple thing as this, but anyway it is working as I need it to now.


RE: Scaling Problem in Cocos2d - EvolPenguin - May 18, 2011 11:02 AM

The number one problem solver: come back later Smile.
Of course this assumes you have the time to do so Sad.
Alex