Hello. I am studying cocos2d-x.
First of all, please understand that my English is not good enough.
the code is like this
<Entity.h>
class Entity
{
protected:
cocos2d::Sprite* sprite;
cocos2d::RepeatForever* actionStateDefault;
cocos2d::RepeatForever* actionStateMoving;
cocos2d::Animate* animateDefault;
cocos2d::Animate* animateMoving;
cocos2d::Animation* animationDefault;
cocos2d::Animation* animationMoving;
cocos2d::SpriteBatchNode* batchNode;
.
.//SKIP//
.
}
<Player.h>
class Player : public Entity
//SKIP//
<Player.cpp>
// node and spite
batchNode = SpriteBatchNode::create(“res/characters/revenant/revenant_original.pvr.ccz”);
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("res/characters/revenant/revenant_original.plist");
sprite = cocos2d::Sprite::createWithSpriteFrameName("revenant_idle_00.png");
sprite->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
sprite->setTag(TAG_PLAYER);
// animation for default state
animationDefault = Animation::create();
for (int i = 0; i < 4; i++)
{
char szImageFileName[128] = { 0 };
sprintf(szImageFileName, "revenant_idle_%02d.png", i);
animationDefault->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(szImageFileName));
}
animationDefault->setDelayPerUnit(0.13f);
animationDefault->setRestoreOriginalFrame(true);
animateDefault = cocos2d::Animate::create(animationDefault);
actionStateDefault = RepeatForever::create(animateDefault);
actionStateDefault->retain();
.
.//SKIP//
.
_map = map;
auto pos = _map->getObjectGroup(“Objects”)->getObject(“Player”);
Vec2 temp{};
if (!pos.empty())
{
temp = Vec2(pos[“x”].asInt(), pos[“y”].asInt());
_pos.x = temp.x / map->getTileSize().width;
_pos.y = map->getMapSize().height - ((map->getMapSize().height * map->getTileSize().height - temp.y) / map->getTileSize().height);
}
// create physics
this->world = world;
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(_pos.x, _pos.y);
bodyDef.userData = this;
/
///SKIP//
/
// set default state
setStateDefault();
changeDirection(direction);
batchNode->addChild(sprite);
return true;
Enemy.h is the same as the player.h
My current problem is, I don’t have a problem adding sprites and actions to batchNode inherited from Entity.
However, when I try to add sprites individually in GameScene, the problem occurs in the player->update.
Inherited Entity::Sprite* sprite; this is probably why.
So I tried many things. like
// GameScene.cpp
cocos2d::Sprite * ball = Sprite::create(“ball.png”);
ball->setTag(TAG_BALL);
this->addChild(ball);
or
sprite = Sprite::create(“ball.png”); //inherited from Entity
this->addChild(sprite);
All of these attempts will be aborted without leaving any error codes.
I’m trying to put a ball image on Revolute Joint.
I search and ask questions from place to place, but I can’t get an answer.
Please let me know if you want more detailed code.
It’s a really important problem for me. Thank you in advance.