Проблема масштабирования в Cocos2d! - PullRequest
0 голосов
/ 06 мая 2011

Так что моя проблема в том, что когда я масштабирую экземпляр класса Weapon (как я покажу ниже - 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];
    }

1 Ответ

3 голосов
/ 18 мая 2011

- проблема решена -

Я должен сказать, я был немного разочарован этим, поэтому я оставил это в покое на некоторое время, думая, что, может быть, через какое-то время я смогу вернуться к нему снова и, возможно, найти решение ... Сработала стратегия!

Первоначально я пытался масштабировать его в классе «Оружие», как я показываю, а также в классе «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;

Мне хочется пнуть себя за то, что я не думал попробовать такую ​​простую вещь, как эта, но в любом случае это работает так, как мне нужно сейчас.

...