Я ищу помощь в создании спрайта в другом классе и файле cocos2d-x. Я использовал код с сайта cocos2d-x для подкласса спрайта и других сообщений, и это не сработало бы для меня. Я следил за учебником по системам сонара для создания отдельного класса для «птичьего» спрайта («Player» в моем случае), и у меня возникают проблемы с ошибкой при попытке передать уровень helloworld через конструктор класса Player, который принимает параметр слой. ошибка говорит: «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;
}
я получаю немного красный Squigly линия под т в:
player = new Player(this);
в конце HelloWorldScene.cpp файла
Что и почему вы хотите достичь путем подкласса класса Sprite? – Jain
Поэтому я могу разделить разные спрайты на разные файлы, и поэтому я могу дать им свои собственные функции. @Jain – ElSnowman
Итак, какая у вас проблема? Не могли бы вы поделиться некоторым кодом? – Jain