Errors while building class cocos2d-x

2019-08-02 04:11发布

问题:

I added class GraphicsScene.h:

#pragma once

#include "cocos2d.h"

class GraphicsScene : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
    virtual bool init();
    CREATE_FUNC(GraphicsScene);
};

Then

#include "GraphicsScene.h"

USING_NS_CC;

Scene* GraphicsScene::createScene()
{
    auto scene = Scene::create();
    auto layer = GraphicsScene::create();
   scene->addChild(layer);

    return scene;
}

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

    auto sprite = Sprite::create("HelloWorld.png");
    sprite->setPosition(0, 0);

    this->addChild(sprite, 0);

    return true;
}

Then I added in AppDelegate instead of usual HelloWorld::createScene():

auto scene = GraphicsScene::createScene();
    // run
director->runWithScene(scene);

I added name of the class in Android.mk.

These are errors

What I m doing wrong? How to create class?

回答1:

Everything seems good.

I guess you edited wrong Android.mk file. Please check what Android.mk file are you editing ?

The one in proj.android or the one in proj.android-studio?

When compiling using --android-studio tag, you are compiling the project inside proj.android-studio, not proj.android.

cocos compile -p android --android-studio

You're not using include guard in GraphicsScene.h. Use include guard to avoid the same header file being included multiple times.

EDIT

remove static keyword from createScene method definition inside GraphicsScene.cpp

static Scene* GraphicsScene::createScene()
{
    auto scene = Scene::create();
    auto layer = GraphicsScene::create();
    scene->addChild(layer);
    return scene;
}

EDIT 2

Android.mk of proj.android-studio

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../../Classes/AppDelegate.cpp \
                   ../../../Classes/HelloWorldScene.cpp \
                   ../../Classes/GraphicsScene.cpp   --> Path is wrong for this file

so change this to ../../../Classes/GraphicsScene.cpp