ostap_holub
ostap_holub

Reputation: 585

cocos2d - Sprite doesn't show properly

I am using cocos2dx v3.11.1 with c++ to create simple game for iOS platform. But I run into strange issue with sprite movement.

Definition of main objects looks like that:

Sprite *logoBackSprite;
SpriteFrameCache *cache;
SpriteBatchNode *batchNode;

I create sprite from the plist like that:

cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("game_sprites1.plist");

batchNode = SpriteBatchNode::create("game_sprites1.png");
addChild(batchNode);

logoBackSprite = Sprite::createWithSpriteFrameName(fileName);
logoBackSprite->setPosition(Vec2(centerPoint.x-4, centerPoint.y + 200-43));
addChild(logoBackSprite, 0);

After that I call function which must perform simple moving animation for sprite. Code of this function looks like that:

void MainMenuScene::playMenuAnimations() {

   float startDelay = 0.0f;

   auto windowSize = Director::getInstance()->getVisibleSize();
   Point centerPoint = Vec2(windowSize.width/2, windowSize.height/2);       

   logoBackSprite->stopAllActions();
   logoBackSprite->setOpacity(0);
   logoBackSprite->runAction(Sequence::create(DelayTime::create(startDelay + 0.5 + 0.75),
                                           FadeTo::create(0.5f, 255),
                                           nullptr));
}

But when I run application - nothing happens. Could you point me what I am making wrong? Thank you.

EDIT After few hours of experiments, I have noticed, that runAction method of sprite objects doesn't work. For example, I have created vector of sprite frames and I want to run animation. When I run application I can see this sprite, but animation still does not work. Maybe, I missed smth important?

EDIT2 Today I have noticed that any action works prefect on previous scene.

Upvotes: 0

Views: 80

Answers (1)

ostap_holub
ostap_holub

Reputation: 585

Issue solved. Problem was in bad configuration of scene where I want to run animations. I should override onEnter() and onExit() methods. In onEnter() method of scene I call

Layer::onEnter();

Also in scene class I create init() method like that:

bool MainMenuScene::init() {
    if ( !Layer::init() )
    {
        return false;
    }

    return true;
}

And animation runs perfectly!

Upvotes: 0

Related Questions