开发者

Scaling Textures Correctly

Okay, I am going to post some code so you can see what I am doing and then at the end I will ask my question.

In my Weapon class to create and animate it:

-(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:

// 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 [CC开发者_StackOverflow社区Animation 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?


— 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 -

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 -

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜