3Animation动画的创建,CCSpeed,CCFollow
動畫,不同于動作,動畫并非屬性的改變。而是對幀的播放。
2 方法一
| CCSprite * sp = CCSprite::create(“animation/p_2_01.png”); sp->setPosition(ccp(240,160)); //注意:這里的CCRectMake中的參數都是相對自己來說的,前兩個參數定義了一個點,80,80表示的是矩形的長,寬。 CCSpriteFrame * frame1 = CCSpriteFrame::create("animation/p_2_01.png",CCRectMake(0,0,80,80)) ; CCSpriteFrame * frame2 = CCSpriteFrame::create("animation/p_2_02.png",CCRectMake(0,0,80,80)) ; CCSpriteFrame * frame3 = CCSpriteFrame::create("animation/p_2_03.png",CCRectMake(0,0,80,80)) ; CCSpriteFrame * frame4 = CCSpriteFrame::create("animation/p_2_04.png",CCRectMake(0,0,80,80)) ; CCSpriteFrame * frame5 = CCSpriteFrame::create("animation/p_2_05.png",CCRectMake(0,0,80,80)) ; CCSpriteFrame * frame6 = CCSpriteFrame::create("animation/p_2_06.png",CCRectMake(0,0,80,80)) ; CCSpriteFrame * frame7 = CCSpriteFrame::create("animation/p_2_07.png",CCRectMake(0,0,80,80)) ; CCSpriteFrame * frame8 = CCSpriteFrame::create("animation/p_2_08.png",CCRectMake(0,0,80,80)) ; ? CCAnimation * animation = CCAnimation::create(); animation->addSpriteFrame(frame1); animation->addSpriteFrame(frame2); animation->addSpriteFrame(frame3); animation->addSpriteFrame(frame4); animation->addSpriteFrame(frame5); animation->addSpriteFrame(frame6); animation->addSpriteFrame(frame7); animation->addSpriteFrame(frame8); ? animation->setDelayPerUnit(0.1f); animation->setLoops(kCCRepeatForever); ? CCAnimate *animate = CCAnimate::create(animation); sp->runAction(animate); ? addChild(sp); |
3 方法二(對一的改進)
| CCSprite * sp = CCSprite::create(“animation/p_2_01.png”); sp->setPosition(ccp(240,160)); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFrameWithFile(“animation/plant.plist”); char bufname[100]; CCArray * array = CCArray::create(); for(int I = 1;i<= 8;i++) { memset(bufname,”p_2_0%d.png”,i); CCSpriteFrame * frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(bufname); array->addObject(frame); } CCAnimation * animation = CCAnimation::createWithSpriteFrames(array,0.2f); animation->setLoops(kCCRepeatForever); ? CCAnimate * animate = CCAnimate::create(animation); addChild(sp); |
4 更簡單的
| CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(“animation/plant.plist”); char bufname[100]; CCAnimation * animation = CCAnimation::create(); for(int i = 1;i<=8;i++){ memset(bufname,sizeof(bufname),0); sprint(bufname,”p_2_0%d.png”,i); animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(bufname)); } animation->setDelayPerUnit(0.2f); animation->setLoops(kCCRepeatForever); |
5 案例說明:
| T14Animation.h |
| #ifndef __T14Animation_H__ #define __T14Animation_H__ ? #include "cocos2d.h" #include "TBack.h" USING_NS_CC; class T14Animation :public TBack { public: ??? static CCScene * scene(); ??? CREATE_FUNC(T14Animation); ??? bool init(); ? ??? CCSprite *spr; ??? void onEnter(); ??? void onEnterTransitionDidFinish(); }; ? #endif |
| T14Animation.cpp |
| #include "T14Animation.h" #include "AppMacros.h" ? CCScene *T14Animation::scene() { ??? CCScene * scene = CCScene::create(); ??? T14Animation * layer = T14Animation::create(); ??? scene->addChild(layer); ??? return scene; } ? bool T14Animation::init() { ??? TBack::init(); ??? return true; } ? //在進入場景的時候做以下操作 void T14Animation::onEnter() { ??? TBack::onEnter(); ??? //以圖片的方式創建一個精靈 ??? spr = CCSprite::create("animation/p_2_01.png"); ??? //設置精靈的顯示位置 ??? spr->setPosition(ccp(winSize.width / 2, winSize.height / 2)); ??? addChild(spr); } ? void T14Animation::onEnterTransitionDidFinish() { ??? TBack::onEnterTransitionDidFinish(); ??? //plist中是圖片信息 ??? CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/plant.plist"); ? ??? //創建動畫 ??? CCAnimation * animation = CCAnimation::create(); ??? //這個用于存儲圖片的名字 ??? char? nameBuf[100]; ??? for (int i = 0; i < 8; i++) ??? { ??????? memset(nameBuf, 0, sizeof(nameBuf)); ??????? sprintf(nameBuf, "p_2_0%d.png", i + 1); ??? ??? animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf)); ??? } ??? //設置每次動畫執行的時候的延時 ??? animation->setDelayPerUnit(0.1f); ??? //這只循環兩次 ??? animation->setLoops(2); ? ??? CCAnimate * animate = CCAnimate::create(animation); ??? spr->runAction(animate); } |
| 動畫效果(2秒鐘內循環執行了20次):
|
6 CCSpeed?CCFollow
案例:
| T15Speed.h |
| #ifndef _T15Speed_H__ #define _T15CCSpeed_H__ ? #include "cocos2d.h" #include "TBack.h" USING_NS_CC; class T15Speed :public TBack { public: ??? static CCScene * scene(); ??? CREATE_FUNC(T15Speed); ??? bool init(); ? ??? //英雄這個精靈 ??? CCSprite * hero; ??? //食物這個精靈 ??? CCSprite * food; ? ??? CCMoveBy * by; ??? void update(float dt); }; ? #endif |
| T15Speed.cpp |
| #include "T15Speed.h" #include "AppMacros.h" ? CCScene *T15Speed::scene() { ??? CCScene * scene = CCScene::create(); ??? T15Speed * layer = T15Speed::create(); ??? scene->addChild(layer); ??? return scene; } ? bool T15Speed::init() { ??? TBack::init(); ? ??? hero = CCSprite::create("animation/hero.png"); ??? hero->setPosition(ccp(100, 160)); ??? addChild(hero); ? ??? by = CCMoveBy::create(10, ccp(300, 0)); ??? hero->runAction(by); ? ??? food = CCSprite::create("animation/food.png"); ??? food->setPosition(ccp(200, 160)); ??? addChild(food); ??? ??? //因為小人跑動的姿勢都是一幅幅的動畫效果組成,所以下面通過 ??? //CCSpriteFrameCache的方式加載一系列的圖片 ??? CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/run.plist"); ??? //創建動畫效果 ??? CCAnimation * animation = CCAnimation::create(); ? ??? char nameBuf[100]; ??? for (int i = 0; i < 15;i++) ??? { ??????? memset(nameBuf, 0, sizeof(nameBuf)); ??????? //取出圖片等信息 ??????? sprintf(nameBuf, "run%d.png", i + 1); ??? ??? animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf)); ??? } ??? //圖片切換的時候的時間延遲為0.1f ??? animation->setDelayPerUnit(0.1f); ??? //下面的方式表示永久循環 ??? animation->setLoops(-1); ??? CCAnimate * animate = CCAnimate::create(animation); ??? hero->runAction(animate); ? ??? //開啟定時器 ??? scheduleUpdate(); ??? return true; } ? ? void T15Speed::update(float dt) { ??? //將boundingBox()縮放 ??? CCRect heroRect = CCRect(hero->boundingBox().origin.x + 50, ??????? hero->boundingBox().origin.y, ??????? hero->boundingBox().size.width - 100, ??????? hero->boundingBox().size.height); ??? //通過下面的方式實現:當人碰到豌豆了的時候移除豌豆 ??? if (food&&heroRect.intersectsRect(food->boundingBox())) ??? { ??????? //通過下面這句實現將food從主窗口中清除 ??????? food->removeFromParentAndCleanup(true); ??????? food = NULL; ? ??????? //通過CCSpeed將原來的速度增加5倍 ??????? CCSpeed * speed = CCSpeed::create(by, 5); ??????? hero->runAction(speed); ??? } } |
| 運行結果:
吃豆之后:
|
CCFollow
| T16CCFollow.h |
| #include "T16CCFollow.h" #include "AppMacros.h" ? CCScene *T16CCFollow::scene() { ??? CCScene * scene = CCScene::create(); ??? T16CCFollow * layer = T16CCFollow::create(); ??? scene->addChild(layer); ??? return scene; } ? bool T16CCFollow::init() { ??? TBack::init(); ??? //背景 ??? CCSprite * bg = CCSprite::create("map.png"); ??? //設置錨點 ??? bg->setAnchorPoint(ccp(0, 0)); ??? addChild(bg); ? ??? //創建一個英雄的精靈 ??? hero = CCSprite::create("animation/hero.png"); ??? hero->setPosition(ccp(240, 160)); ??? addChild(hero); ? ??? //下面的代碼是添加動畫的代碼 ??? CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/run.plist"); ??? CCAnimation * animation = CCAnimation::create(); ? ??? char nameBuf[100]; ??? for (int i = 0; i < 15; i++) ??? { ??????? memset(nameBuf, 0, sizeof(nameBuf)); ??????? sprintf(nameBuf, "run%d.png", i + 1); ??????? animation->addSpriteFrame( ??????????? CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf)); ??? } ??? animation->setDelayPerUnit(0.1f); ??? animation->setLoops(-1); ??? CCAnimate * animate = CCAnimate::create(animation); ??? hero->runAction(animate); ? ??? by = CCMoveBy::create(10, ccp(400, 0));; ? ??? //添加假動作 ??? CCCallFuncN * func = CCCallFuncN::create(this, callfuncN_selector(T16CCFollow::funcNCallBack)); ? ??? CCSequence * seq = CCSequence::create(by, func, NULL); ??? hero->runAction(seq); ? ??? //使用CCFollow的方式創建英雄 ??? CCFollow * follow = CCFollow::create(hero); ??? this->runAction(follow); ??? return true; } ? void T16CCFollow::funcNCallBack(CCNode * node) { ??? CCSprite * spr = (CCSprite *)node; ??? CCLog("x = %g, y = %g", spr->getPositionX(), spr->getPositionY()); ? ??? CCPoint world = this->convertToWorldSpace(spr->getPosition()); ? ??? CCLog("x = %g, y = %g", world.x, world.y); } |
| T16CCFollow.cpp |
| #include "T16CCFollow.h" #include "AppMacros.h" ? CCScene *T16CCFollow::scene() { ??? CCScene * scene = CCScene::create(); ??? T16CCFollow * layer = T16CCFollow::create(); ??? scene->addChild(layer); ??? return scene; } ? bool T16CCFollow::init() { ??? TBack::init(); ??? //背景 ??? CCSprite * bg = CCSprite::create("map.png"); ??? //設置錨點 ??? bg->setAnchorPoint(ccp(0, 0)); ??? addChild(bg); ? ??? //創建一個英雄的精靈 ??? hero = CCSprite::create("animation/hero.png"); ??? hero->setPosition(ccp(240, 160)); ??? addChild(hero); ? ??? //下面的代碼是添加動畫的代碼 ??? CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/run.plist"); ??? CCAnimation * animation = CCAnimation::create(); ? ??? char nameBuf[100]; ??? for (int i = 0; i < 15; i++) ??? { ??????? memset(nameBuf, 0, sizeof(nameBuf)); ??????? sprintf(nameBuf, "run%d.png", i + 1); ??????? animation->addSpriteFrame( ??????????? CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf)); ??? } ??? animation->setDelayPerUnit(0.1f); ??? animation->setLoops(-1); ??? CCAnimate * animate = CCAnimate::create(animation); ??? hero->runAction(animate); ? ??? by = CCMoveBy::create(10, ccp(400, 0));; ? ??? //添加假動作 ??? CCCallFuncN * func = CCCallFuncN::create(this, callfuncN_selector(T16CCFollow::funcNCallBack)); ? ??? CCSequence * seq = CCSequence::create(by, func, NULL); ??? hero->runAction(seq); ? ??? //使用CCFollow的方式創建英雄 ??? CCFollow * follow = CCFollow::create(hero); ??? this->runAction(follow); ??? return true; } ? void T16CCFollow::funcNCallBack(CCNode * node) { ??? CCSprite * spr = (CCSprite *)node; ??? CCLog("x = %g, y = %g", spr->getPositionX(), spr->getPositionY()); ? ??? CCPoint world = this->convertToWorldSpace(spr->getPosition()); ? ??? CCLog("x = %g, y = %g", world.x, world.y); } |
| 運行結果:
|
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的3Animation动画的创建,CCSpeed,CCFollow的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 徐工xg700e成槽机柴油油箱多大容量?
- 下一篇: 购车时怎样选择一辆适合自己的车?