Cocos2D-x game development 16th: creating a moving genie

Source: Internet
Author: User

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:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.