Error replacing scene

  • 0971MPG
    Likes 0

    Problem Description

    Hello, thank you for your online tutorial, I’ve bought your book “Cocos2d-x Game Developement Essentials” and I am experiencing a problem. I am on a Windows 10 Machine with Cocos-2dx version 3.10 and Android NDK r10e. I want to replace scene from HelloWorldScene to GameMode scene. Here is the code:

     

    Android.mk

     

    LOCAL_PATH := $(call my-dir)

     

    include $(CLEAR_VARS)

     

    $(call import-add-path,$(LOCAL_PATH)/../../cocos2d)

    $(call import-add-path,$(LOCAL_PATH)/../../cocos2d/external)

    $(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos)

     

    LOCAL_MODULE := cocos2dcpp_shared

     

    LOCAL_MODULE_FILENAME := libcocos2dcpp

     

    LOCAL_SRC_FILES := hellocpp/main.cpp \

                       ../../Classes/AppDelegate.cpp \

                       ../../Classes/HelloWorldScene.cpp \

                       ../../Classes/GameModeScene.cpp

     

    LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes

     

    # _COCOS_HEADER_ANDROID_BEGIN

    # _COCOS_HEADER_ANDROID_END

     

     

    LOCAL_STATIC_LIBRARIES := cocos2dx_static

     

    # _COCOS_LIB_ANDROID_BEGIN

    # _COCOS_LIB_ANDROID_END

     

    include $(BUILD_SHARED_LIBRARY)

     

    $(call import-module,.)

     

    # _COCOS_LIB_IMPORT_ANDROID_BEGIN

    # _COCOS_LIB_IMPORT_ANDROID_END

     

    HelloWorldScene.cpp

     

    #include "HelloWorldScene.h"

    #include "GameModeScene.h"

     

    USING_NS_CC;

     

    Scene* HelloWorld::createScene()

    {

        // 'scene' is an autorelease object

        auto scene = Scene::create();

       

        // 'layer' is an autorelease object

        auto layer = HelloWorld::create();

     

        // add layer as a child to scene

        scene->addChild(layer);

     

        // return the scene

        return scene;

    }

     

    // on "init" you need to initialize your instance

    bool HelloWorld::init()

    {

        //////////////////////////////

        // 1. super init first

        if ( !Layer::init() )

        {

            return false;

        }

       

        Size visibleSize = Director::getInstance()->getVisibleSize();

        Vec2 origin = Director::getInstance()->getVisibleOrigin();

     

        //creo il menu

        auto play_button = MenuItemImage::create("play.png", "play_pressed.png",CC_CALLBACK_1(HelloWorld::GoToGameMode, this));

        play_button->setPosition(Point(visibleSize.width / 2, (visibleSize.height / 7) * 5));

     

        auto menu = Menu::create(play_button, NULL);

        this->addChild(menu, 1);

       

        return true;

    }

     

    void HelloWorld::GoToGameMode(cocos2d::Ref *pSender)

    {

        auto scene = GameMode::createScene();

     

        Director::getInstance()->replaceScene(scene);

    }

     

    HelloWorldScene.h

     

    #ifndef __HELLOWORLD_SCENE_H__

    #define __HELLOWORLD_SCENE_H__

     

    #include "cocos2d.h"

     

    class HelloWorld : public cocos2d::Layer

    {

    public:

        static cocos2d::Scene* createScene();

     

        virtual bool init();

       

        // a selector callback

        void menuCloseCallback(cocos2d::Ref* pSender);

       

        void GoToGameMode(Ref *pSender);

     

        // implement the "static create()" method manually

        CREATE_FUNC(HelloWorld);

    };

     

    #endif // __HELLOWORLD_SCENE_H__

     

     

    GameModeScene.h

     

    #ifndef __GAMEMODE_SCENE_H__

    #define __GAMEMODE_SCENE_H__

     

    #include "cocos2d.h"

     

    class GameMode : public cocos2d::Layer

    {

    public:

        static cocos2d::Scene* createScene();

     

        virtual bool init();

       

        // a selector callback

        void menuCloseCallback(cocos2d::Ref* pSender);

       

     

        // implement the "static create()" method manually

        CREATE_FUNC(GameMode);

    };

     

    #endif // __GAMEMODE_SCENE_H__

     

    GameModeScene.cpp

     

    #include "GameModeScene.h"

     

     

    USING_NS_CC;

     

    Scene* GameMode::createScene()

    {

        // 'scene' is an autorelease object

        auto scene = Scene::create();

       

        // 'layer' is an autorelease object

        auto layer = GameMode::create();

     

        // add layer as a child to scene

        scene->addChild(layer);

     

        // return the scene

        return scene;

    }

     

    // on "init" you need to initialize your instance

    bool GameMode::init()

    {

        //////////////////////////////

        // 1. super init first

        if ( !Layer::init() )

        {

            return false;

        }

       

        Size visibleSize = Director::getInstance()->getVisibleSize();

        Vec2 origin = Director::getInstance()->getVisibleOrigin();

     

        return true;

    }

     

     

    This is the error I experience when i run in the command prompt:

    “cd folder of the game” and “cocos.py compile -p android –android-studio”

    Error:

    jni/../../../Classes/HelloWorldScene.cpp:46: error: undefined reference to 'GameMode::createScene()'

    collect2.exe: error: ld returned 1 exit status

    make.exe: *** [obj/local/armeabi/libcocos2dcpp.so] Error 1

    make.exe: Leaving directory `C:/Users/HK/Documents/Programmazione/Colours/proj.android-studio/app'

    Error running command, return code: 2.

     

    I know that it’s a basic function but I followed step by step the book and the online tutorials and I always have this error. I have the same error with pushing the scene as well.

     

    Thank you

  • Sonar Systems admin
    Likes 0

    Does it run if the code is commented out.

  • 0971MPG
    Likes 0

    Yes, if i comment out the content like this

     

    void HelloWorld::GoToGameMode(cocos2d::Ref *pSender)

    {

        //auto scene = GameMode::createScene();

     

       // Director::getInstance()->replaceScene(scene);

    }

    it runs. I’ve got a similar error with pushScene.

  • Sonar Systems admin
    Likes 0

    Can you show us a screenshot of the console with all the different errors


  • 0971MPG
    Likes 0

    Here is a screenshot of the command prompt:

    https://drive.google.com/file/d/0BxhTsfUz-P1-aEg0MGgxRVYybWM/view?usp=sharing

  • Sonar Systems admin
    Likes 0

    Have you tried using the source code.

  • 0971MPG
    Likes 0

    No, i haven’t. How can i do it?

  • 0971MPG
    Likes 0

    After many attempts I’ve found the solution, I post it here so anyone with the same error can solve it. We need to change the Android.mk in the ...\proj.android-studio\app\jni folder just like the Android.mk in the proj.android\app\jni folder by adding the new file namescene.cpp

     

  • Sonar Systems admin
    Likes 0

    Great to here you solved it.

Login to reply