现在有一个layer类,如果在layer中直接创建一个cocos2d::sprite(没有任何继承关系),addChild()到layer中没有一点问题。
但是,如果将我自定义的继承了sprite的类addChild到layer中就运行报错,不知道为什么。下面上代码:
------自定义sprite------------
#include “MarioHeroSprite.h”
//超级玛丽精灵
MarioHeroSprite* marioSprite;
//向右移动精灵表缓存
cocos2d::Texture2D* moveRightCache;
//向右移动帧动画
cocos2d::Animate* moveRightAnimate;
MarioHeroSprite* MarioHeroSprite::createMarioHeroSprite(){
//利用缓存创建显示部分的精灵
moveRightCache = cocos2d::Director::getInstance()->getTextureCache()->addImage("Mario_move_right.png");
marioSprite = MarioHeroSprite::create();
marioSprite->setTexture(moveRightCache);
marioSprite->setTextureRect(cocos2d::Rect(356, 2, 146, 256));
return marioSprite;
}
bool MarioHeroSprite::init(){
//初始化向右移动帧动画
cocos2d::Animation* moveRightAnimation = cocos2d::Animation::create();
moveRightAnimation->addSpriteFrameWithTexture(moveRightCache, cocos2d::Rect(2, 260, 169, 256));
moveRightAnimation->addSpriteFrameWithTexture(moveRightCache, cocos2d::Rect(356, 2, 146,
moveRightAnimation->setDelayPerUnit(0.1f);
moveRightAnimation->setRestoreOriginalFrame(false);
moveRightAnimate = cocos2d::Animate::create(moveRightAnimation);
return true;
}
------正常的Layer------------
bool HelloWorld::init(){
MarioHeroSprite* marioHeroSprite = MarioHeroSprite::createMarioHeroSprite();
marioHeroSprite->setPosition(Vec2(50,50));
this->addChild(marioHeroSprite);
marioHeroSprite->moveRight();
return true;
}