2011-05-02 118 views
0

好吧,我要发布一些代码,以便您可以看到我在做什么,然后最终我会问我的问题。正确缩放纹理

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

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

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

回答

0

- 问题解决 -

我必须说,我有点沮丧,这让我离开它单独一段时间认为一段时间已经过去了,也许以后我可以再次蜿蜒到它,可能会找到一个解决方案...策略工作!

最初我试图在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;; 

我觉得自己不想尝试像这样简单的事情,但无论如何,它正在工作,因为我现在需要它。