我正在寻找帮助创建一个不同的类和文件cocos2d-x精灵。我已经使用了来自cocos2d-x网站的代码来实现精灵和其他帖子的子类化,但它对我不起作用。我按照声纳系统教程为一个"bird“精灵(在我的例子中是”Player“)创建了一个单独的类,但在尝试通过使用层参数的Player类构造函数传递helloworld层时遇到了错误。错误显示:"' Player :: Player (const Player &)':无法将参数1从'HelloWorld *const‘转换为'const Player &’ApocalypseWorld Player 39“
下面是代码: Player.h:
#pragma once
#include "cocos2d.h"
class Player
{
public:
Player( cocos2d::Layer* layer );
private:
cocos2d::Sprite *player1;
};Player.cpp:
#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:
#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:
#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下面画了一条小红线:
player = new Player(this); 在HelloWorldScene.cpp文件的末尾
发布于 2015-12-14 16:52:01
编辑代码:
Player.h:
#ifndef __Sample__Player__
#define __Sample__Player__
#include "cocos2d.h"
USING_NS_CC;
class Player : public Node
{
public:
static Player* createPlayer(Layer* layer);
Sprite* head;
};
#endifPlayer.cpp:
#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:
#include "Player.h"在你的scene.cpp中:
auto player = Player::createPlayer(this);
addChild(player);https://stackoverflow.com/questions/34211077
复制相似问题