Scaling Problem in Cocos2d

Apprentice
Posts: 16
Joined: 2010.01
Post: #1
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?
Quote this message in a reply
⌘-R in Chief
Posts: 1,260
Joined: 2002.05
Post: #2
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.)
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.01
Post: #3
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.
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #4
Tested in my app, and scaling is definitely happening appropriately based on where I have the anchor point set
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.01
Post: #5
Any suggestions?
Quote this message in a reply
⌘-R in Chief
Posts: 1,260
Joined: 2002.05
Post: #6
Step through with the debugger into the Cocos2d code and trace what happens and why. That's as good as it gets.
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.01
Post: #7
— 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.
Quote this message in a reply
Moderator
Posts: 450
Joined: 2003.08
Post: #8
The number one problem solver: come back later Smile.
Of course this assumes you have the time to do so Sad.
Alex
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Scaling iOS Game: iPhone -> iPad s0ckman 2 1,465 Dec 9, 2013 07:34 AM
Last Post: s0ckman
  frame buffer 320x480 scaling images/2=retina? sefiroths 0 4,240 May 10, 2011 05:24 AM
Last Post: sefiroths
  Tearing when Scaling a Tilemap The_Golden_Gunman 7 5,589 Jun 28, 2010 06:00 AM
Last Post: The_Golden_Gunman