首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在不同的文件中创建精灵(在类中)

在不同的文件中创建精灵(在类中)
EN

Stack Overflow用户
提问于 2015-12-11 04:25:27
回答 1查看 1.4K关注 0票数 2

我正在寻找帮助创建一个不同的类和文件cocos2d-x精灵。我已经使用了来自cocos2d-x网站的代码来实现精灵和其他帖子的子类化,但它对我不起作用。我按照声纳系统教程为一个"bird“精灵(在我的例子中是”Player“)创建了一个单独的类,但在尝试通过使用层参数的Player类构造函数传递helloworld层时遇到了错误。错误显示:"' Player :: Player (const Player &)':无法将参数1从'HelloWorld *const‘转换为'const Player &’ApocalypseWorld Player 39“

下面是代码: Player.h:

代码语言:javascript
复制
#pragma once
#include "cocos2d.h"

class Player
{
public:
 Player( cocos2d::Layer* layer );

private:
 cocos2d::Sprite *player1;
};

Player.cpp:

代码语言:javascript
复制
#include "Player.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"

USING_NS_CC;

Player::Player( cocos2d::Layer* layer )
{

    player1 = Sprite::create("PlayerHead.png");
    player1->setPosition(Point(200, 200));

    layer->addChild(player1, 100);
}

HelloWorldScene.h:

代码语言:javascript
复制
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "Player.h"

class HelloWorld : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();

    Player* player;

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp:

代码语言:javascript
复制
#include "HelloWorldScene.h"
#include "Player.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"

USING_NS_CC;

using namespace cocostudio::timeline;

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;
    }

    auto rootNode = CSLoader::createNode("MainScene.csb");

    addChild(rootNode);

player = new Player(this);


return true;

}

我在t下面画了一条小红线:

代码语言:javascript
复制
player = new Player(this); 

在HelloWorldScene.cpp文件的末尾

EN

回答 1

Stack Overflow用户

发布于 2015-12-14 16:52:01

编辑代码:

Player.h:

代码语言:javascript
复制
#ifndef __Sample__Player__
#define __Sample__Player__

#include "cocos2d.h"

USING_NS_CC;

class Player : public Node
{
public:
    static Player* createPlayer(Layer* layer);

    Sprite* head;
};

#endif

Player.cpp:

代码语言:javascript
复制
#include "Player.h"

Player* Player::createPlayer(Layer* layer) {
    auto ret = new (std::nothrow) Player;
    if(ret && ret->init()) {
        ret->autorelease();
        ret->head = Sprite::create("PlayerHead.png");
        ret->addChild(ret->head);
        layer->addChild(ret, 100);
        return ret;
    }
    CC_SAFE_RELEASE(ret);
    return nullptr;
}

然后在你的场景中。h:

代码语言:javascript
复制
#include "Player.h"

在你的scene.cpp中:

代码语言:javascript
复制
auto player = Player::createPlayer(this);
addChild(player);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34211077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档