PopStar - Resume Button Implementation

  • MoseHas
    Likes 0

    Problem Description

    Hi there.

    Today I want to ask how to implement resume game in popstar.

    First, I thought it was popScene() and pushScene(), and I tried. I did successfully pop the game scene out and return to the main scene. However, when I tried to push the GameScene back, it became a new scene just like I use replaceScene(). It initialized a new star matrix and would not perserve the current score I have just played.

    Is there anything that I should deal with carefully when I try to resume my game? Or what I want is much more complex than simply popScene/pushScene?

     

    Please let me know if you need my source code. I think it will be a mess if I post them all.(with more than 150 lines)

     

    Thanks.

  • MoseHas
    Likes 0

    I am sorry. I just discovered that it DID return. I failed at level 4 and game was over. I clicked the return button and I could play level 4 again.

    The problem here is although its current score display was correct, it still initialized a new matrix.

  • Sonar Systems admin
    Likes 0

    Could you list in order the sequence of push and pops for the scenes and what triggers them please. This will help us better understand the situation.

     

     

  • MoseHas
    Likes 0

    bool GameScene::init()
    {
    	if(!Scene::init())
    	{
    		return false;
    	}
    
    	Size visibleSize = Director::getInstance()->getVisibleSize();
    
    	this->addChild(GameLayer::create());
    
    
    	MenuItemImage* pauseBtn = MenuItemImage::create("pause.png", "pause.png", CC_CALLBACK_1(GameScene::returnScene, this));
    	pauseBtn->setPosition(Point(-visibleSize.width * 0.4, visibleSize.height * 0.15));
    	pauseBtn->setScale(0.15);
    	Menu* pauseMenu = Menu::create(pauseBtn, NULL);
    	this->addChild(pauseMenu, 0);
    
    
    }
    
    void GameScene::returnScene(Ref* pSender)
    {
    	log("clicking return button");
    	Director::getInstance()->popScene();
    }

    I think it is not because of the code. I create the star matrix in GameLayer.cpp which includes StarMatrix.h and Star.h.

    Star.h is about the class of a star node, and StarMatrix.cpp has the most of the algorithm such as creating a star matrix, eliminating stars, adjusting matrix after stars are removed. They are a little bit complicated. One function calls another, so I think I should paste up everything.

    bool GameLayer::init()
    {
    	if(!Layer::init())
    	{
    		return false;
    	}
    
    	matrix = nullptr;
    
    	EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
    	listener->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan, this);
    	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
    
    	Size visibleSize = Director::getInstance()->getVisibleSize();
    	Vec2 origin = Director::getInstance()->getVisibleOrigin();
    	Sprite* background = Sprite::create("bg_mainscene.jpg");
    	background->setPosition(visibleSize.width / 2, visibleSize.height / 2);
    	this->addChild(background,-1);
    
    	menu = TopMenu::create();
    	this->addChild(menu);
    
    	//display the combo information
    	linkNum = Label::create("", FONT_PATH, visibleSize.width * FONT_SIZE);//using predefined font size parameter to make floating words fit the screen properly
    	linkNum->setPosition(visibleSize.width / 2, visibleSize.height * 11 / 16);
    	linkNum->setVisible(false);
    	this->addChild(linkNum, 1);
    
    	this->floatLevelWord();
    	this->scheduleUpdate();
    	return true;
    }

     

    Should I paste up the rest?


  • MoseHas
    Likes 0

    Forget it. I uploaded my Classes and Resources folders.

    https://mega.co.nz/#!foN0yArT!PJh_Vp4sUdW4tNizaIWwYjx15eYGAgPBGOHIX0DGn_w

    I think this will be simpler.


  • Sonar Systems admin
    Likes 0

    Coudl you just create a bullet point list of the navigation as there are a lot of files to go through.

  • barabasandras1
    Likes 0

    Do you really have to use a different scene for pausing the game? Why don’t you use cocos2d::Layer?

  • Sonar Systems admin
    Likes 0

    Layer?

     

    Can you explain a little more what you are talking about.

  • barabasandras1
    Likes 0

    For pausing the game I use another layer on the scene. Just like dialog windows.

  • Sonar Systems admin
    Likes 0

    You can do that as well


    This reply has been verified.
  • MoseHas
    Likes 0

    I believe barabasandras1 is talking about a poping-out menu resume. Well I think this is better than what I was thinking about.

    My orginal thought was returning to the main menu and I can press resume back to the game.

     

    Then I would like to ask that if there is any way to resume game from the main menu scene.

    Because my problem here is everytime I push the GameScene back, it will create a new matrix. Can I keep the original matrix when I resume the game?


    This reply has been verified.
  • barabasandras1
    Likes 0

    I’m thinking about its possible in some way… But it would be too difficult to handle everything. When you are in the GameScene, and press the Main menu button or whatever, you actually make a scene push (sorry it was a long time ago since I used cocos2d-x, I use cocos js now, but its almost the same).

    So when you create the scene at that moment (auto scene = new MainMenuScene::createScene(gameModel)) or something like that, you could pass the game model as the new scene’s argument. gameModel parameter (actually an instance of GameModel class) stores every important informations and objects from the GameScene. For example moving sprite positions, scores, everything. Back in the main menu, when you push resume game, you actually create a brand new GameScene (plot twist lol) but with the previously stored gameModel that you pass in the new GameScene::createScene(gameModel).

    I would not recommend this unless you don’t have any other option. Maybe there’s a really easy way to achieve your goal such as the pushScene/popScene. So please do not consider this as the only working option, because your game might not even be capable of this method. 


    This reply has been verified.
  • MoseHas
    Likes 0

    I have conquered this difficulty! Thanks to barabasandras1!

    My orginal way is to pop the GameScene out and return to MainScene by pushing GameScene. The problem is everytime I return to GameScene it will become a new scene instead of the old one.

    Therefore, I do things reversely. I push MenuScene after I pause the game in GameScene, and pop the MenuScene out to return to GameScene.

    And I successfully return to the GameScene without reinitializing. 

    Thank you very much!

Login to reply