| 版权声明:本文为博主原创文章,未经博主允许不得转载。
这节来实现GameUnit类中的一些函数方法,其实这个类一般是一个边写边完善的过程,因为一般很难一次性想全所有的能够供多个类共用的方法。下面贴上代码:
GameUnit.h
#ifndef _GAME_UNIT_H_ #define _GAME_UNIT_H_ ////////////////////////////////////////////////////////////////// //此文件中主要描述的是游戏经常使用到的一些变量和函数的集合 #include "cocos2d.h" USING_NS_CC; class unit : public cocos2d::Layer { private: cocos2d::Size visible; cocos2d::Vec2 origin; cocos2d::Animation* animation; public: void displayNowScore(float); cocos2d::Size winSize(); cocos2d::Vec2 winOrigin(); cocos2d::Animation* gameAnimate(int); float scaleX(cocos2d::Sprite*, cocos2d::Size); float scaleY(cocos2d::Sprite*, cocos2d::Size); void goNullptr(cocos2d::Ref* pSender); //cocos2d::Sprite* createSprite(int); }; #endif // _GAME_UNIT_H_
GameUnit.cpp
#include "GameUnit.h" //取得当前屏幕的大小 cocos2d::Size unit::winSize() { visible = cocos2d::Director::getInstance()->getVisibleSize(); return visible; } //取得当前的屏幕的原点坐标 cocos2d::Vec2 unit::winOrigin() { origin = cocos2d::Director::getInstance()->getVisibleOrigin(); return origin; } //计算适合屏幕的缩放值 float unit::scaleX(cocos2d::Sprite* sprite, cocos2d::Size visible) { return visible.width / sprite->getContentSize().width; } float unit::scaleY(cocos2d::Sprite* sprite, cocos2d::Size visible) { return visible.height / sprite->getContentSize().height; } //合成游戏中的精灵的动画效果 cocos2d::Animation* unit::gameAnimate(int types) { int i = 0; if (types <= 3 && types >= 1) //通过 .plist 文件来创建,取得动画创建的对象 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("bird/bird.plist"); else SpriteFrameCache::getInstance()->addSpriteFramesWithFile("bird/number.plist"); //使用一个Vector容器来保存这些对象. Vector<SpriteFrame*> frames; //将plist中的文件依次的去处来 switch (types) { case 1: //红色小鸟 for (i = 0; i < 3; i++) { //循环取出每只小鸟 String* frameName = String::createWithFormat("bird0_%d.png", i); //取得每只小鸟对应的名字 SpriteFrame* frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(frameName->getCString()); //将每只小鸟压入栈中 frames.pushBack(frame); } animation = Animation::createWithSpriteFrames(frames, 1.0f / 3.0f); break; case 2: //蓝色小鸟 for (i = 0; i < 3; i++) { String* frameName = String::createWithFormat("bird1_%d.png", i); SpriteFrame* frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(frameName->getCString()); frames.pushBack(frame); } animation = Animation::createWithSpriteFrames(frames, 1.0f / 3.0f); break; case 3: //黄色小鸟 for (i = 0; i < 3; i++) { String* frameName = String::createWithFormat("bird2_%d.png", i); SpriteFrame* frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(frameName->getCString()); frames.pushBack(frame); } animation = Animation::createWithSpriteFrames(frames, 1.0f / 3.0f); break; case 4: //黄色小鸟 for (i = 1; i <= 3; i++) { String* frameName = String::createWithFormat("n%d.png", i); SpriteFrame* frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(frameName->getCString()); frames.pushBack(frame); } animation = Animation::createWithSpriteFrames(frames, 1.0f / 1.0f); break; default: CCLOG("Error, The create animation occur error!"); break; } return animation; } cocos2d::Sprite* createSprite(int type) { Sprite* sprite4 = Sprite::create("logo/medals1.png"); Sprite* sprite3 = Sprite::create("logo/medals2.png"); Sprite* sprite2 = Sprite::create("logo/medals3.png"); Sprite* sprite1 = Sprite::create("logo/medals4.png"); switch (type) { //award = NULL; //铜牌 case 5:CCLOG("5"); return sprite1; break; //白银 case 10: CCLOG("10"); return sprite2; break; //黄金 case 20:CCLOG("20"); return sprite3; break; //白金 case 50:CCLOG("50"); return sprite4; break; } return nullptr; } void displayNowScore(float time) { /* int number = 0; int i = 0; //分离数字 while (number) { switch (number%10) { case 0: MenuItemImage* zero = NULL; zero = MenuItemImage::create( "number/zero.png", "number/zero.png", CC_CALLBACK_1(unit::goNullptr, this) ); break; case 1: MenuItemImage* one = NULL; one = MenuItemImage::create( "number/one.png", "number/one.png", CC_CALLBACK_1(unit::goNullptr, this) ); break; case 2: MenuItemImage* two = NULL; two = MenuItemImage::create( "number/two.png", "number/two.png", CC_CALLBACK_1(unit::goNullptr) ); break; case 3: Sprite* three = NULL; three = Sprite::create("number/three.png"); break; case 4: Sprite* frou = NULL; frou = Sprite::create("number/frou.png"); break; case 5: Sprite* five = NULL; five = Sprite::create("number/five.png"); break; case 6: Sprite* six = NULL; six = Sprite::create("number/six.png"); break; case 7: Sprite* seven = NULL; seven = Sprite::create("number/seven.png"); break; case 8: Sprite* eight = NULL; eight = Sprite::create("number/eight.png"); break; case 9: Sprite* nine = NULL; nine = Sprite::create("number/nine.png"); break; } number = number / 10; } */ } void unit::goNullptr(cocos2d::Ref* pSender) {}
每个函数的功能见:Cocos2d之FlyBird开发---简介