Cocos2d-X to develop a simple little game

Source: Internet
Author: User
Tags addchild

After learning so long Cocos2d-X, today finally can make a simple game, the game is very simple, control the genie movement through the menu item

Learn a new concept before playing games

Sched ):

The Cocos2d-x scheduler provides scheduled events and scheduled calls for games. All node objects know how to schedule and cancel scheduling events. Using the scheduler has several advantages:

  1. The scheduler stops whenever the node is no longer visible or removed from the scenario.
  2. The scheduler also stops when the Cocos2d-x is paused. The scheduler also continues automatically when the Cocos2d-x restarts.
  3. The Cocos2d-x encapsulates a scheduler for various platforms, which you don't need to care about and track the destruction and stop of the scheduled object you set, as well as the risk of crash.


Some functions used when using the Scheduler:

// Call this-> Update (float DT) function cyclically For frames
// Scheduleupdate ();

// Let the frame loop call the specified function. The time is still 1/60 seconds.
// Schedule (schedule_selector (t19update: myschedulefunc ));

// Timer, call the t19update: myschedulefunc function every 2 seconds
// Schedule (schedule_selector (t19update: myschedulefunc), 2.0f );

// A timer with a limited number of times. When the number of times is defined very large, it is also considered to be infinite. The actual number of times is set to + 1.
Schedule (schedule_selector (t19update: myschedulefunc), 1.0f, 3, 2.0f );

// A timer that is only scheduled once
// Scheduleonce (schedule_selector (t19update: myschedulefunc), 5.0f );

// Stop a timer with parameters as the callback function
Unschedule (schedule_selector (t19update: myschedulefunc ));

// Stop update scheduling
Unscheduleupdate ();

// Stop all scheduling tasks
Unscheduleallselectors ();


Now that we are familiar with the concept of scheduler, we start to play a simple game today.

First, add a small ball image to the resource file under the project directory.


Then define an update class

Add the following code to the update. H class:

# Ifndef _ update_h _ # DEFINE _ update_h _ # include "cocos2d. H "using_ns_cc; Class update: Public cclayer {public: static ccscene * scene (); create_func (update); bool Init (); void Update (float DT ); ccsprite * _ sprite; void handle (ccobject * sender); // indicates the direction int _ direction; // The window size ccsize winsize;}; # endif

Add the following code to update. cpp:

# Include "Update. H "ccscene * update: Scene () {ccscene * s = ccscene: Create (); Update * layer = Update: Create (); s-> addchild (layer); Return s;} bool update: Init () {// initialize the parent class cclayer: Init (); // obtain the window size winsize = ccdirector: shareddire()-> getwinsize (); // set the coordinates ccpoint center = CCP (winsize. width/2, winsize. height/2); // Let the frame call this-> Update (float DT) function scheduleupdate () cyclically; // create sprite _ sprite = ccsprite :: create ("green_edit.png"); addchild (_ sprite); // set the sprite location _ sprite-> setposition (center); // create a menu item ccmenuitemfont * up = ccmenuitemfont :: create ("up", this, menu_selector (Update: handle); ccmenuitemfont * down = ccmenuitemfont: Create ("down", this, menu_selector (Update :: handle); ccmenuitemfont * Left = ccmenuitemfont: Create ("Left", this, menu_selector (Update: handle); ccmenuitemfont * Right = ccmenuitemfont :: create ("right", this, menu_selector (Update: handle); ccmenuitemfont * Stop = ccmenuitemfont: Create ("stop", this, menu_selector (Update :: handle); // create menu ccmenu * menu = ccmenu: Create (Up, down, left, right, stop, null); addchild (menu ); // align the menu item menu-> alignitemsvertically (); // set the menu item ID up-> settag (1); down-> settag (2 ); left-> settag (3); Right-> settag (4); stop-> settag (0); // mark direction _ direction =-1; return true ;} void update: handle (ccobject * sender) {ccnode * node = (ccnode *) sender; // obtain the menu item ID, ID corresponds to the direction of the genie movement _ direction = node-> gettag ();} void update: Update (float DT) {// _ direction = 1 indicates that the genie moves up if (1 = _ direction) {// The genie moves up. // 50 indicates moving 50 pixels in one second _ sprite-> setpositiony (_ sprite-> getpositiony () + dt * 100 );} // _ direction = 2 indicates that the genie moves down if (2 = _ direction) {// move the genie downward. // 50 indicates moving 50 pixels per second _ sprite-> setpositiony (_ sprite-> getpositiony ()-dT * 100 );} // _ direction = 3 indicates that the genie moves to the left if (3 = _ direction) {// move the genie to the left. // 50 indicates moving 50 pixels in one second _ sprite-> setpositionx (_ sprite-> getpositionx ()-dT * 100 );} // _ direction = 4 indicates that the genie moves to the right if (4 = _ direction) {// move the genie to the right. // 50 indicates moving 50 pixels in one second _ sprite-> setpositionx (_ sprite-> getpositionx () + dt * 100 );} // _ direction = 4 indicates that the genie stops moving if (0 = _ direction ){}}

Execution result:



Reference: http://cn.cocos2d-x.org/article/index? Type = cocos2d-x & url =/doc/Cocos-docs-master/manual/framework/native/V3/Scheduler/Zh. md zookeeper

Cocos2d-X to develop a simple little game

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.