有时间就写点,接着写了,
新建一个类JlmLayer在.h中进行一下声明:
#pragma once
#include "e:\cocos2d-x\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\layers_scenes_transitions_nodes\cclayer.h"
#include "cocos2d.h"
#include "JlmSprite.h"
class JlmLayer :publiccocos2d::CCLayer
{
public:
JlmLayer(void);
~JlmLayer(void);
virtual bool init();
CREATE_FUNC(JlmLayer);
cocos2d::CCSpriteFrameCache*_jlmCache;//精灵猫动画的缓存节点
cocos2d::CCSpriteBatchNode*_jlmBatchNode; //精灵猫的批处理节点
void jlmSpriteCreate(float dt);
JlmSprite* _jlmSprite;
bool _isJlmCreate;//判断角色是否已经产生
void setJlmInMap();
};
在其.cpp文件中进行一下定义:
#include "JlmLayer.h"
#include "PmdInAllLayer.h"
USING_NS_CC;
JlmLayer::JlmLayer(void)
{
this->_jlmCache=CCSpriteFrameCache::sharedSpriteFrameCache();
this->_jlmCache->addSpriteFramesWithFile("yuandi/yuandi.plist");
this->_jlmCache->retain();
this->_jlmBatchNode=CCSpriteBatchNode::create("yuandi/yuandi.pvr.ccz");
this->addChild(this->_jlmBatchNode);
this->_jlmBatchNode->retain();
this->_jlmSprite=NULL;
this->_isJlmCreate=false;
}
JlmLayer::~JlmLayer(void)
{
this->_jlmBatchNode->release();
this->_jlmCache->release();
}
boolJlmLayer::init()
{
if(!CCLayer::init())
{
return false;
}
schedule(schedule_selector(JlmLayer::jlmSpriteCreate),0.1f);
return true;
}
void JlmLayer::jlmSpriteCreate(float dt)
{
this->_jlmSprite = JlmSprite::create();
this->_jlmBatchNode->addChild(this->_jlmSprite);
this->setJlmInMap();
this->_jlmSprite->setScale(0.42);
this->_isJlmCreate=true;
if(this->_isJlmCreate)
{
unschedule(schedule_selector(JlmLayer::jlmSpriteCreate));
}
}
void JlmLayer::setJlmInMap()
{
CCTMXObjectGroup*objGroup =((PmdInAllLayer*)this->getParent())->_mapLayer->_allMap->objectGroupNamed("jlmPoint");
CCArray*objArray = objGroup->getObjects();
if( objArray&& objArray->count()>0)
{
CCObject* pObj = NULL;
CCARRAY_FOREACH(objArray, pObj)
{
CCDictionary*playerPointDic= (CCDictionary*)pObj; //将对象转化成字典,便于获取键值
float x = playerPointDic->valueForKey("x")->floatValue();
float y = playerPointDic->valueForKey("y")->floatValue();
this->_jlmSprite->setPosition(ccp(0.5*x,0.5*y));
}
}
}
然后我们来创建它的子节点,JlmSprite类:在头文件中进行以下声明:
#pragma once
#include "e:\cocos2d-x\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\sprite_nodes\ccsprite.h"
#include "cocos2d.h"
class JlmSprite :publiccocos2d::CCSprite
{
public:
JlmSprite(void);
~JlmSprite(void);
virtual bool init();
CREATE_FUNC(JlmSprite);
void yuanDiAnimation();
};
在其.cpp中进行这样的定义:
#include "JlmSprite.h"
USING_NS_CC;
JlmSprite::JlmSprite(void)
{
}
JlmSprite::~JlmSprite(void)
{
}
bool JlmSprite::init()
{
if(!CCSprite::initWithSpriteFrameName("zoulu2.png"))
{
return false;
}
this->yuanDiAnimation();
return true;
}
void JlmSprite::yuanDiAnimation()
{
//精灵猫的原地动画;
CCArray* jlmArray =CCArray::create();
jlmArray->retain();
int i;
for(i=1;ispriteFrameByName(CCString::createWithFormat("zoulu%d.png",i)->getCString());
jlmArray->addObject(jlmFrames);
}
CCAnimation* jlmAnination =CCAnimation::createWithSpriteFrames(jlmArray,0.02);
this->runAction(CCRepeatForever::create(CCAnimate::create(jlmAnination)));
}这样在地图上的精灵猫就产生了:
看下效果:
我在这里留个QQ群,,欢迎大家加入讨论:367086379