0

所以我的问题是,当我缩放武器类的实例时(如下所示 - self.scale = 0.35f),它缩小到左下角,几乎就像锚点设置为 [0.0,0.0] 而不是[0.5,0.5] 我希望它只是从精灵的中心缩放。我已经输入了一些 NSLogs,它说锚点在 [0.5,0.5]。谁能帮我解决这个问题?

在我的武器类中创建和动画它:

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

1 回答 1

3

— 问题解决了 —</p>

我必须说,我对此感到有些沮丧,所以我暂时搁置了它,想着也许过了一段时间后我可以再次徘徊,也许会找到解决方案……策略奏效了!

最初我尝试在 Weapon 类中缩放它,就像我展示的那样,以及在我创建 Weapon 类的实例的 gamelayer 类中,在这两种情况下,图像都只是缩小到左下角——我也在确保将锚点设置为 [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;

我觉得自己没有想过尝试这样一个简单的事情,但无论如何它正在按照我现在需要的方式工作。

于 2011-05-17T23:20:21.163 回答