正确缩放纹理

时间:2011-05-02 15:26:17

标签: anchor scaling textures point ccsprite

好的,我将发布一些代码,以便您可以看到我在做什么,最后我会问我的问题。

在我的武器类中创建并制作动画:

-(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;
}

这是上面调用的处理动画的方法:

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

所以我的问题是当我缩放Weapon类的实例时(就像我上面显示的那样 - self.scale = 0.35f)它缩小到左下角,几乎就像锚点设置为[0.0,0.0 ]而不是[0.5,0.5],我希望它只是从精灵的中心缩放。我已经放入了一些NSLog,它说锚点位于[0.5,0.5]。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

- 问题解决了 -

我必须说,我对此感到有些沮丧所以我一个人离开了一段时间以为可能经过一段时间后我可能再次徘徊,也许找到解决方案......战略工作!

最初我尝试在Weapon类中进行缩放,就像我展示的那样,以及在我制作Weapon类实例的游戏层类中,在这两种情况下,图像只缩小到左下角 - 我也确保将锚点设置为[0.5f,0.5f]。

所以这次我尝试设置锚点并在将其添加到屏幕之后进行缩放,而不是之前:

之前 -

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

之后 -

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

我觉得自己没有想过尝试这样简单的事情,但是无论如何它正在我现在需要它。