2011-05-02 120 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;; 

我覺得自己不想嘗試像這樣簡單的事情,但無論如何,它正在工作,因爲我現在需要它。