Cocos2d-x study note 05-Scene and scene animation, action

Source: Internet
Author: User
Tags addchild

Scene Switching function:

    • Director->getinstance ()->replacescene (scene*);
    • Director->getinstance ()->runwithscene (scene*);

The difference between the two is that if a scene exists, use the first one, the first run the scene, and the second one.

The following is a key code for switching scenes. Click the text to jump to the next scene. The scene to switch to Imagescene inherits from layer, and defines 3 member function Init,createscene,create_func.

ImageScene.h as follows:

1 #ifndef _imagescene_h_2 #define_imagescene_h_3 4#include <iostream>5#include <cocos2d.h>6 using_ns_cc;7 8 classImagescene: Publiclayer{9  Public:Ten     Virtual BOOLinit (); One     Staticscene*Createscene (); A Create_func (imagescene); - }; - #endif
View Code

ImageScene.cpp as follows:

1#include"ImageScene.h"2 3scene*Imagescene::createscene () {4Scene *scene =scene::create ();5Imagescene *layer =imagescene::create ();6Scene->addChild (layer);7 8     returnscene;9 }Ten BOOLImagescene::init () { One     if(!Layer::init ()) { A         return false; -     } -  theSize size = Director::getinstance ()getvisiblesize (); -  -Sprite *s = Sprite::create ("Helloworld.png"); -S->setposition (size.width/2, size.height/2); + AddChild (s); -  +     return true; A}
View Code

Key code:

1 BOOLHelloworld::init ()2 {3     //////////////////////////////4     //1. Super init First5     if( !layer::init ())6     {7         return false;8     }9Size visiblesize = Director::getinstance ()getvisiblesize ();TenPoint origin = Director::getinstance ()Getvisibleorigin (); One  ALabelttf *label = Labelttf::create ("Show Next Scene","Courier", $); - addChild (label); -  theLabel->setposition (visiblesize.width/2, visiblesize.height/2); -  -Eventlistenertouchonebyone *listener =eventlistenertouchonebyone::create (); -  +Listener->ontouchbegan = [Label] (Touch *t, Event *e) { -         if(Label->getboundingbox (). Containspoint (t->getLocation ())) { +Director::getinstance ()Replacescene (Imagescene::createscene ()); A             return true; at         } -         return false; -     }; -Director::getinstance ()->geteventdispatcher ()addeventlistenerwithscenegraphpriority (listener, label); -     return true; -}

Operating effect:

The scene switching effect can make the scene switch not appear abrupt. The scene switching effect is mainly implemented by Transitionscene subclasses.

Modify the 21st line of the critical code

Director::getinstance ()->replacescene (transitionfade::create (3.0f, Imagescene::createscene ()));

Toggle Effect:

Action actions

Primary inheritance diagram for a class

Key code:

    • Label->runaction (Moveto::create (1.0f, point (100, 100)));
    • Label->runaction (Moveby::create (1.0f, point (-10,-10)));
    • Label->runaction (Moveby::create (1.0f, point))->reverse ());//The effect is the same as the previous one, and reverse is the reverse execution.

The effect is different, the effect of running:

The detailed code is as follows:

BOOLHelloworld::init () {//////////////////////////////    //1. Super init First    if( !Layer::init ()) {        return false; } Size visiblesize= Director::getinstance ()getvisiblesize (); Labelttf*label = Labelttf::create ("Hello, Cocos.","Courier", -); Label->setposition (Visiblesize.width/2, Visiblesize.height/2);        AddChild (label); Eventlistenertouchonebyone*listener =eventlistenertouchonebyone::create (); Listener->ontouchbegan = [Label] (touch* t,event*e) {        if(Label->getboundingbox (). Containspoint (t->getLocation ())) {Label->runaction (Moveto::create (1.0f, Point ( -, -))); //label->runaction (Moveby::create (1.0f, point ( -10,-10)));        }        return false;    }; Director::getinstance ()->geteventdispatcher ()addeventlistenerwithscenegraphpriority (listener, label); return true;}
View Code

Repeat/repeatforever:

Complete 180 degree rotations in 1 seconds and repeat 3 times.

Label->runaction (Repeat::create (Rotateby::create (1.0f, 180), 3));

Complete the 180 degree rotation in 1 seconds and repeat.
Label->runaction (Repeatforever::create (Rotateby::create (1.0f, 180)));

Operation Result:

Action Mix: Spawn class

The label rotates while moving.

At the end, write a null that identifies the end of the array.
Label->runaction (Spawn::create (moveby::create (1, point), Rotateby::create (1, N), NULL));

Operating effect:

Action sequence: Sequence class

Label moves first, then rotates

Write a null at the end to identify the end of the array

Label->runaction (Sequence::create (moveby::create (1, point), Rotateby::create (1,));

Operating effect:

Monitoring of actions:

The Callfunc class inherits from the Finitetimeaction, so you can use this class to implement the listening effect.

The label moves first, then rotates, and after the rotation is finished, a pop-up box pops up.

Label->runaction (sequence::create (                moveby::create (1, point ( ) ),                 rotateby::create (1),                 callfunc::create ([] () {                MessageBox (  "ActionComplete", "Complete");            }) , NULL));

Operation Result:

Enclose all the source code in this section:

BOOLHelloworld::init () {//////////////////////////////    //1. Super init First    if( !Layer::init ()) {        return false; } Size visiblesize= Director::getinstance ()getvisiblesize (); Labelttf*label = Labelttf::create ("Hello, Cocos.","Courier", -); Label->setposition (Visiblesize.width/2, Visiblesize.height/2);        AddChild (label); Eventlistenertouchonebyone*listener =eventlistenertouchonebyone::create (); Listener->ontouchbegan = [Label] (touch* t,event*e) {        if(Label->getboundingbox (). Containspoint (t->getLocation ())) {            //label->runaction (Moveto::create (1.0f, point (100, 100))); //label->runaction (Moveby::create (1.0f, point ( -10,-10))); //label->runaction (Moveby::create (1.0f, point (Ten))->reverse ());//the effect is the same as the previous sentence//label->runaction (Repeat::create (Rotateby::create (1.0f, 180), 3)); //label->runaction (Repeatforever::create (Rotateby::create (1.0f, 180))); //at the end, write a null that identifies the end of the array. //label->runaction (Spawn::create (moveby::create (1, point), Rotateby::create (1, N), NULL)); //label->runaction (Sequence::create (moveby::create (1, point), Rotateby::create (1, N), NULL));Label->runaction (Sequence::create (Moveby::create (1, Point ( -, -)), Rotateby::create (1, the), Callfunc::create ([] () {MessageBox ("Action Complete"," Complete");        }), (NULL)); }        return false;    }; Director::getinstance ()->geteventdispatcher ()addeventlistenerwithscenegraphpriority (listener, label); return true;}

Cocos2d-x study note 05-Scene and scene animation, action

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.