Cocos2d 中的缩放问题!

发布于 2024-11-05 01:57:51 字数 1925 浏览 0 评论 0原文

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

在我的 Weapon 类中创建它并为其设置动画:

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

So my problem is that when I scale the instance of the Weapon class (like I show below - 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?

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 [CCAnimation animationWithFrames:frames delay:delay];
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅窗月明清似水 2024-11-12 01:57:51

- 问题解决了 -

我必须说,我对此感到有点沮丧,所以我把它搁置了一段时间,想也许过了一段时间我可以再次漫步到它,也许找到解决方案......策略有效!

最初,我尝试在 Weapon 类中缩放它,就像我所展示的那样,并且还在 gamelayer 类中我创建 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;

我想踢自己,因为我没有想到尝试这样简单的事情,但无论如何它的工作原理是我现在就需要它。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文