coco2d-x 使用Action中的Animate实现动画效果

命令行新建一个工程

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

运行效果,显示为动画,截取某个图片。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值