命令行新建一个工程
cocos new ActionGame -p com.MyCompany.ActionGame -l cpp -d ./MyCompany
新建一个名叫ActionGame的项目,目录在当前目录的MyCompany目录下
使用gedit打开HelloWorldScene.cpp
去掉一些不要的代码,比如显示hello world的字符串等,下面的双斜线的注释为去掉的代码。
准备图片,来源于网络,这些图片规定每个鱼儿的位置,所以应该是顺序排列。
源码:
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
// auto closeItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
// closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
// auto menu = Menu::create(closeItem, NULL);
// menu->setPosition(Vec2::ZERO);
// this->addChild(menu, 1);
/
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
// auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
// position the label on the center of the screen
// label->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height));
// add the label as a child to this layer
// this->addChild(label, 1);
// add "HelloWorld" splash screen"
// auto sprite = Sprite::create("HelloWorld.png");
// position the sprite on the center of the screen
// sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
// this->addChild(sprite, 0);
auto mySprite = Sprite::create("mysprite.png");
this->addChild(mySprite);
mySprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// now lets animate the sprite we moved
Vector<SpriteFrame *> animFrames;
animFrames.reserve(12);
animFrames.pushBack(SpriteFrame::create("Blue_Front1.png", Rect(0, 0, 100, 100)));
animFrames.pushBack(SpriteFrame::create("Blue_Front2.png", Rect(0, 0, 100, 100)));
animFrames.pushBack(SpriteFrame::create("Blue_Front3.png", Rect(0, 0, 100, 100)));
animFrames.pushBack(SpriteFrame::create("Blue_Left1.png", Rect(0, 0, 100, 100)));
animFrames.pushBack(SpriteFrame::create("Blue_Left2.png", Rect(0, 0, 100, 100)));
animFrames.pushBack(SpriteFrame::create("Blue_Left3.png", Rect(0, 0, 100, 100)));
animFrames.pushBack(SpriteFrame::create("Blue_Back1.png", Rect(0, 0, 100, 100)));
animFrames.pushBack(SpriteFrame::create("Blue_Back2.png", Rect(0, 0, 100, 100)));
animFrames.pushBack(SpriteFrame::create("Blue_Back3.png", Rect(0, 0, 100, 100)));
animFrames.pushBack(SpriteFrame::create("Blue_Right1.png", Rect(0, 0, 100, 100)));
animFrames.pushBack(SpriteFrame::create("Blue_Right2.png", Rect(0, 0, 100, 100)));
animFrames.pushBack(SpriteFrame::create("Blue_Right3.png", Rect(0, 0, 100, 100)));
// create the animation out of the frames
Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.1f);
Animate* animate = Animate::create(animation);
mySprite->runAction(RepeatForever::create(animate));
return true;
}
编译生成apk
cocos compile -s ./MyCompany/ActionGame -p android -m debug -o ./MyCompany/ActionGame/bin
安装进入手机
adb install MyCompany/ActionGame/bin/ActionGame-debug.apk
运行效果,显示为动画,截取某个图片。