- 2022.03.28 Monday
- スポンサーサイト
-
一定期間更新がないため広告を表示しています
- | スポンサードリンク | - | | - | - | pookmark |
- 2014.04.16 Wednesday
- cocos2d-x-3.0rc0 ,c++ ,cpp でTexturePacker で 吐き出した、plist と png でパラパラアニメーションのコード例
-
メモ:Cocos2d-x ,c++ ,cpp
TexturePacker で 吐き出した、plist と png でパラパラアニメーションのコード例
http://nyakagawan.hatenablog.com/entry/2014/03/22/112513
さんを参考にさせてもらったよ。
HelloWorldScene.cpp
#include "HelloWorldScene.h" USING_NS_CC; Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } //------------------------------------ /// // //テクスチャアトラスを読み込む auto cache = SpriteFrameCache::getInstance(); cache->addSpriteFramesWithFile("img_ship_00.plist", "img_ship_00.png"); //その画像でCCSpriteBachNodeを作成 /// アニメーションフレームに画像を追加 /// [例] /// img_ship_00.png /// img_ship_01.png /// img_ship_02.png /// img_ship_03.png /// という画像があるとします ////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////http://nyakagawan.hatenablog.com/entry/2014/03/22/112513 ////////////////////////////////////////////////////////////////////////////////////////////// auto _sprite1 = Sprite::createWithSpriteFrameName("img_ship_01.png"); _sprite1->setPosition( Point( 300, 300) ); Vector<SpriteFrame*> animFrames(5); for( int i=1; i<5; i++ ) { char filename[128] = {0}; //sprintf(filename,"hoge_%02d.png",i); //の、%02dという部分ですが、これは、例えば i が 1 の時、“hoge_01.png”となります。 //もう一つ例を上げておくと //sprintf(filename,"hoge_%01d.png",i); //の時、同じように i が 1 の場合、“hoge_1.png”となります。 sprintf(filename,"ループ回数:%2d",i); puts(filename); sprintf(filename,"img_ship_%02d.png",i); puts(filename); //Sprite* spr = Sprite::createWithSpriteFrameName(filename); auto frame = cache->getSpriteFrameByName( filename ); animFrames.pushBack(frame); } auto animation = Animation::createWithSpriteFrames(animFrames, 0.01f); _sprite1->runAction( RepeatForever::create( Animate::create(animation) ) ); this->addChild(_sprite1); /* /// アニメーションを切り替える速度を決める /// ※1.0fで一秒になります pAnimation->setDelayPerUnit( 0.1f ); /// 無限ループさせる /// [例] /// 1 ... 一回ループさせる /// 2 ... 二回ループさせる /// -1 ... 無限ループさせる pAnimation->setLoops(-1); /// アニメーションの生成 //CCAnimate* pAnimate = CCAnimate::create(pAnimation); Animate* pAnimate = Animate::create(pAnimation); /// スプライトの生成 //CCSprite* pSprite = CCSprite::create(); Sprite* pSprite = Sprite::create(); /// レイヤーにスプライトを追加 this->addChild(pSprite); /// スプライト座標を設定する //pSprite->setPosition(CCPointMake(0.0f,0.0f)); pSprite->setPosition(300,300); /// アニメーションを実行する pSprite->runAction(pAnimate); */ //////////////////////////////////////////////////////////////////// return true; } void HelloWorld::menuCloseCallback(Ref* pSender) { Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }
HelloWorldScene.h はそのままだよ#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class HelloWorld : public cocos2d::Layer { public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // a selector callback void menuCloseCallback(cocos2d::Ref* pSender); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
- | whaison | cocos2d-x | 20:34 | comments(1) | - | pookmark |
- Comment
-
-
関連するブログ
【cocos2d】テクスチャアトラスを使ってみる
http://tf.hateblo.jp/entry/2013/04/10/230505
【cocos2d】CCAnimationでスプライトをアニメーションしてみた
http://tf.hateblo.jp/entry/2013/04/23/230000 - whaison, 2014/04/17 1:33 PM
-
関連するブログ