Animation of Cocos2d-x game development

Source: Internet
Author: User
Tags addchild

To declare an animation function in MyGame.h:

Class Mygame:public Cocos2d::layer
{
Public
Static scene* Createscene ();
void UpdateTime (float t);
virtual BOOL init ();
void logic (float DT);

........
cocos2d::animate* createAnimate1 ();
Create_func (Mygame);

}

MyGame.cpp:

#include "MyGame.h"

#include "cocostudio/cocostudio.h"

#include "ui/cocosgui.h"

#include "Cocos2d.h"

#include <iostream>

using namespace Std;

USING_NS_CC;

using namespace cocos2d;

using namespace Cocostudio::timeline;

cocos2d::animate* mygame::createanimate1 () {

Auto animation = Animation::create ();

for (int i = 1; I <= 4; i++) {//four pictures (for composing animations)

Animation->addspriteframewithfile (Stringutils::format ("Run%d.png", I));

}

Animation->setdelayperunit (3.0f/15.0f);

Back to original state

Animation->setrestoreoriginalframe (TRUE);

Auto animate = animate::create (animation);

return animate;

}

scene* Mygame::createscene () {

' Scene ' is an Autorelease object

Auto scene = Scene::create ();

' Layer ' is an Autorelease object

Auto layer = Mygame::create ();

Add layer as a child to scene

Scene->addchild (layer);

Return the scene return scene; }

BOOL Mygame::init () {

1. Super Init first

if (! Layer::init ())

{

return false;

}

Ball = Sprite::create ("Run1.png");
Ball->setposition (VEC2 (VISIBLESIZE.WIDTH/2, VISIBLESIZE.HEIGHT/2));
This->addchild (ball,2);

Click Btn0 to run the hit baseball animation

Btn0->addclickeventlistener ([&] (ref* psender) {
Ball->runaction (CreateAnimate1 ());
});

return true;

}

Note: If the button event contains a toggle scene event, you need to add the schedule function in the button event, otherwise after switching the scene, the animation is too late to generate, click the button to start the button event again, the animation will not be executed, that is, no response

The specific code is as follows:

Add touch events to a button
Btn0->addtoucheventlistener ([&, Btn0, BTN1, btn2] (ref* psender, Widget::toucheventtype type) {
Switch (type)
{
Case Widget::toucheventtype::began:
Btn0->setscale (1.02);
Break
Case widget::toucheventtype::ended:
Btn0->setscale (1);
Btn1->settouchenabled (FALSE);
Btn2->settouchenabled (FALSE);
.......


if (r[0] = = 3 | | delegate1->restartnum = = 0)
{
..........

You cannot simply write a code director::getinstance ()->replacescene (Gameover::createscene ()) when switching scenes.

Instead, write a schedule function to buffer the time.


Schedule ([&] (float ft) {
Unschedule ("Gameover");
Director::getinstance ()->replacescene (Gameover::createscene ());
},1,0,0, "Gameover");
}
Else
Schedule ([&] (float ft) {
Unschedule ("Newgame");
Director::getinstance ()->replacescene (Mygame::createscene ());
},1, 0, 0, "Newgame");
}
});

Animation of Cocos2d-x game development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.