我正在寻找帮助在不同的类中创建精灵并提交cocos2d-x文件。我已经使用cocos2d-x网站上的代码来为sprite和其他帖子创建子类,这对我来说不起作用。我已经按照声纳系统教程为“鸟”精灵(在我的情况下为“播放器”)制作一个单独的类,并且在尝试通过Player类构造函数传递helloworld层时遇到错误,该构造函数接受参数一层。错误说:“'Player :: Player(const Player&)':无法将参数1从'HelloWorld * const'转换为'const Player&' ApocalypseWorld c:\ cocosprojects \ apocalypseworld \ classes \ helloworldscene.cpp 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文件的末尾
答案 0 :(得分:0)
编辑代码:
Player.h:
QueueDStream
Player.cpp:
#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
然后在你的scene.h中:
#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;
}
在你的scene.cpp中:
#include "Player.h"