最近上课的作业就是研究cocos2dx,所以之后的大部分题目可能都会和cocos2dx有关,这一次来讲一讲cocos2dx的网格动作。就是这个样子的动作
直接开始吧,首先是创建helloworld程序(我默认你们的环境都搭好了,是在mac上的),
创建完之后编译运行一下,在mac上运行(不是ios模拟器),初次编译过程有点长,可以先去准备一下素材。
这里我们将素材包拷贝到项目目录下的resources文件夹下
编译好了后,看看效果
我们改一改HelloworldScene.h如下
#include "cocos2d.h"
typedef enum//添加了存储各种动作标记的结构体
{
kFlipX3D=100,
kLens3D,
kLiquid,
kPageTurn3D,
kRipple3D,
kShaky3D,
kTwirl,
kWaves,
kWave3D,
kFadeOutTRTiles,
kJumpTiles3D,
kShakyTiles3D,
kShatteredTiles3D,
kShuffleTiles,
kSplitCols
} ActionTypes;
class HelloWorld : public cocos2d::Scene
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
void OnclickMenu(cocos2d::Ref *pSender);//添加了菜单点击方法
CREATE_FUNC(HelloWorld);
};
修改helloworld.cpp如下
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
return HelloWorld::create();
}
static void problemLoading(const char* filename)
{
printf("Error while loading: %s\n", filename);
printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
}
bool HelloWorld::init()
{
if ( !Scene::init() )
{
return false;
}
cocos2d::Size visibleSize = cocos2d::Director::getInstance()->getVisibleSize();
cocos2d::Vec2 origin = cocos2d::Director::getInstance()->getVisibleOrigin();
auto bg = cocos2d::Sprite::create("HelloWorld.png");
if (bg == nullptr)
{
problemLoading("'HelloWorld.png'");
}
else
{
bg->setPosition(Vec2(origin.x+visibleSize.width/2,origin.y+visibleSize.height/2));
this->addChild(bg);
}
auto FlipX3DLable = cocos2d::Label::createWithBMFont("fonts/fnt2.fnt", "FlipX3D");
auto FlipX3DMenu = cocos2d::MenuItemLabel::create(FlipX3DLable, CC_CALLBACK_1(HelloWorld::OnclickMenu, this));
FlipX3DMenu->setTag(kFlipX3D);
auto kLens3Dlabel = cocos2d::Label::createWithBMFont("fonts/fnt2.fnt", "Lens3D");
auto kLens3Dmenu = cocos2d::MenuItemLabel::create(kLens3Dlabel,
CC_CALLBACK_1(HelloWorld::OnclickMenu, this));
kLens3Dmenu->setTag(kLens3D);
auto Twirllabel = cocos2d::Label::createWithBMFont("fonts/fnt2.fnt", "Twirl");
auto Twirlmenu = cocos2d::MenuItemLabel::create(Twirllabel,
CC_CALLBACK_1(HelloWorld::OnclickMenu, this));
Twirlmenu->setTag(kTwirl);
auto FadeOutTRTilesLabel = cocos2d::Label::createWithBMFont("fonts/fnt2.fnt", "FadeOutTRTiles");
auto FadeOutTRTilesMenu = cocos2d::MenuItemLabel::create(FadeOutTRTilesLabel,
CC_CALLBACK_1(HelloWorld::OnclickMenu, this));;
FadeOutTRTilesMenu->setTag(kFadeOutTRTiles);
auto ShatteredTiles3DLabel = cocos2d::Label::createWithBMFont("fonts/fnt2.fnt", "ShatteredTiles3D");
auto ShatteredTiles3DMenu = cocos2d::MenuItemLabel::create(ShatteredTiles3DLabel,
CC_CALLBACK_1(HelloWorld::OnclickMenu, this));
ShatteredTiles3DMenu->setTag(kShatteredTiles3D);
auto mn = cocos2d::Menu::create(FlipX3DMenu, kLens3Dmenu, Twirlmenu, FadeOutTRTilesMenu, ShatteredTiles3DMenu, NULL);
mn->alignItemsVertically();
mn->setPosition(Vec2(origin.x+visibleSize.width/4, origin.y+visibleSize.height/2));
this->addChild(mn);
auto kLiquidLable = cocos2d::Label::createWithBMFont("fonts/fnt2.fnt", "Liquid");
auto kLiquidMenu = cocos2d::MenuItemLabel::create(kLiquidLable, CC_CALLBACK_1(HelloWorld::OnclickMenu, this));
kLiquidMenu->setTag(kLi