Today, we are trying to let our Genie move. Of course, we know that the so-called animation is to let the genie switch between multiple pictures. So we need to prepare 15 animated images before we start, then, use a tool to package these images. So we can start coding after we have everything ready.
void Player::run(){CCSpriteFrameCache * freamCache = CCSpriteFrameCache::sharedSpriteFrameCache();freamCache->addSpriteFramesWithFile("run.plist","run.png");CCSpriteFrame *frame = NULL;CCArray *freamlist =CCArray::create();for (int i =1; i <= 15 ; i++){frame = freamCache->spriteFrameByName(CCString::createWithFormat("run%d.png",i)->getCString());freamlist->addObject(frame);}CCAnimation *anination = CCAnimation::createWithSpriteFrames(freamlist);anination->setLoops(-1);anination->setDelayPerUnit(0.08f);CCAnimate *animate = CCAnimate::create(anination);m_sprite->runAction(animate);}
We add a Run function to the existing Player role class, and then call the run function after creating the player Object in the scenario. Then our genie can be moved.
CCSize size = CCDirector::sharedDirector()->getWinSize();CCSprite *sprite = CCSprite::create("player.png");Player *player = Player::create();player->BindSprite(sprite);map->addChild(player);CCTMXObjectGroup * objgroup=map->objectGroupNamed("player");CCDictionary * playerpoint =objgroup->objectNamed("playerpoint");float x = playerpoint->valueForKey("x")->floatValue();float y = playerpoint->valueForKey("y")->floatValue();player->setPosition(ccp(x,y));player->run();
This completes the final result: