Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Cocos2d-x 使用 TexturePacker制作一个英雄,老外写的大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

Create spritesheet

Have you seen this sort of image before? The first time I saw this I thought this is created using Photoshop,and I also have no idea how to use thisBIGimage in the game.

Until… I started to look in the game development,I only know that this is actually created byspritesheetprogram/app.

Here I will be usingTexturePacker.

Why? I have seen many game tutorials are talking about this,I tried it and found that it is very easy to use.

How to use demo

First of all,I’m going to do this in Cocos2d-x ENGIne,so SELEctCocos2dhere.

Now,drag images to thebox

Here I drag 3 bird images,and you can see the left pane,there arehero_01.png,hero_02.png,244)">hero_03.png,these are the original file name.

Once you’ve done,SELEctPublish sprite sheet

It will prompt you twice,one is to save the plist file contains the meta data of the original images such asframe,244)">offset,244)">rotated,etc. It may sound complicated,but no worry,Cocos2d-x will handle it.

The second prompt is to save the merged image.

And you’ve done.

Use in Cocos2d-x

File structure

InAppDelegate.cppapplicationDidFinishLaunching()@H_425_28@method,add

// 1. fix the resolution glview->setDesignResolutionSize(320, 480, ResolutionPolicy::EXACT_FIT); director->setContentScaleFactor(1);  // 2. add search path for images std::vector<String> searchPaths;  searchPaths.push_BACk("images"); FileUtils::geTinstance()->setSearchPaths(searchPaths);  // 3. add sprite frame SpriteFrameCache::addSpriteFramesWithFile("hero.plist", "hero.png"); "images.plist",152)!important">"images.png"); 
  1. I’m a iPhone user,thus I fix the resolution to320 x 480for simplicity
  2. In this case,I put the sprite sheet underimagesdirectory,thus I add the search pathimagestoFileUtils
  3. Add sprite sheet to cache

Create a sprite object (bird in this casE)

I just name itHero. EditHero.h

#ifndef __HERO_SCENE_H__ #define __HERO_SCENE_H__  #include "cocos2d.h"  class Hero : @H_167_262@public cocos2d::Sprite { @H_167_262@public:  Hero();  @H_167_262@private:  RepeatForever *@H_806_70@moving(); };  #endif // __HERO_SCENE_H__ 

andHero.cpp

#include "Hero.h" #include "GameScene.h"  USING_NS_CC;  Hero::Hero() {  // 1. load a default image  initWithSpriteFramename("hero_01.png");   // 2. run the move action  @H_167_262@this->runAction(@H_806_70@moving()); }  RepeatForever* @H_806_70@moving() {  // 3. repeat the frame  int numFrame = 3;   Vector<SpriteFrame *> frames;  SpriteFrameCache *frameCache = geTinstance();   char file[100] = {0};   @H_167_262@for (i = 0; i < numFrame; i++) {  sprintf(file,152)!important">"hero_%02d.png",210)!important">i+1);  SpriteFrame *frame = frameCache->getSpriteFrameByName(file);  frames.pushBACk(frame);  }   Animation *animation = Animation::createWithSpriteFrames(frames, 0.3);  Animate *animate = Animate::create(animation);   repeat = RepeatForever::animate);  @H_167_262@return repeat; } 
  1. Load an image for the Hero(a.k.a bird)by default during object instantiation
  2. @H_347_208@make the bird fly by running the move action
  3. As we know that there are 3 images onhero.plist,thus fix it to 3. Then add all these 3 images(get from sprite frame cachE)to an array,and then repeat it forever.

Add hero to GameScene

EditGameScene.cpp,under theinit()@H_425_28@method,add the code below right beforereturnstatement

Hero *hero = @H_167_262@new Hero(); hero->setPosition(Point(visibleSize.width / 2,210)!important">height / 2)); addChild(hero); 

This is to instantiate aHeroobject,set the position to center of screen then add to current scene.

Run it and you will get the animated bird,see screenshot below.

Done.

大佬总结

以上是大佬教程为你收集整理的Cocos2d-x 使用 TexturePacker制作一个英雄,老外写的全部内容,希望文章能够帮你解决Cocos2d-x 使用 TexturePacker制作一个英雄,老外写的所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。