This series of learning tutorials uses cocos2d-x-2.1.4 (the latest version is cocos2d-x-2.1.5)
The blogger found that the previous two series of learning courses were severely plagiarized. Here, he appealed to everyone to respect the developers' labor achievements,
Please be sure to indicate the source when reprint: http://blog.csdn.net/yangyu20121224/article/details/12067629
I. Create a class
Class, the. h header file.
# Ifndef _ GAME_SCENE_H __# define _ GAME_SCENE_H __# include "cocos2d. h "class GameLayer: public cocos2d: CCLayer {public: // initialization method virtual bool init (); // creation scenario static cocos2d: CCScene * scene (); CREATE_FUNC (GameLayer); // initialize the bool setupViews () ;}; # endif
. Cpp file, which has detailed comments.
# Include "GameScene. h "# include" StaticData. h "USING_NS_CC; // create a game scenario CCScene * GameLayer: scene () {CCScene * scene = CCScene: create (); GameLayer * layer = GameLayer: create (); scene-> addChild (layer); return scene;} // initialization method bool GameLayer: init () {bool isRet = false; do {CC_BREAK_IF (! This-> setupViews (); isRet = true;} while (0); return true;} // initialize the control and set bool GameLayer: setupViews () {bool isRet = false; do {CCLOG ("games initialization... "); // obtain the window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // create a background image sprite * background = CCSprite :: create (STATIC_DATA_STRING ("game_background"); // set the sprite location background-> setPosition (CCPointMake (winSize. width * 0.5, winSize. height * 0.5); // Add the sprite to the layer this-> addChild (background); // Add the plist file to the cache CCSpriteFrameCache: sharedSpriteFrameCache () -> outputs (STATIC_DATA_STRING ("GameLayer_plist"); // read from the cache and create the genie CCSprite * game_box01 = CCSprite: outputs (STATIC_DATA_STRING ("game_ui_box_01 ")); // set the anchorpoint (ccp (0.5, 1); // set the game_box01-> setPosition (CCPointMake (winSize. width * 0.5, winSize. height); // Add the genie to the layer this-> addChild (game_box01); // read from the cache and create the genie CCSprite * game_box02 = CCSprite :: createWithSpriteFrameName (STATIC_DATA_STRING ("game_ui_box_02"); // set the anchorpoint game_box02-> setAnchorPoint (ccp (0.5, 0 )); // set the game_box02-> setPosition (CCPointMake (winSize. width * 0.5, 0); // Add the genie to the layer this-> addChild (game_box02, 3); // create the background image genie CCSprite * ui_2p = CCSprite :: create (STATIC_DATA_STRING ("game_ui_2p"); // set the sprite location ui_2p-> setPosition (CCPointMake (winSize. width * 0.5, 41); // Add the sprite to the layer this-> addChild (ui_2p, 2); isRet = true;} while (0); return isRet ;}
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>default_gold</key><string>200</string><key>title</key><string>title.png</string><key>background</key><string>background.png</string><key>StartScene_Texture</key><string>StartScene.plist</string><key>start_normal</key><string>ui_button_box02_02.png</string><key>start_selected</key><string>ui_button_box02_01.png</string><key>scene_normal</key><string>ui_button_box01_02.png</string><key>scene_selected</key><string>ui_button_box01_01.png</string><key>Button_Texture</key><string>Button.plist</string><key>start</key><string>ui_2p_010.png</string><key>scene</key><string>button_other_014.png</string><key>loading_title</key><string>loading_title.png</string><key>loading_1_1</key><string>loading_1_1.png</string><key>loading_1_2</key><string>loading_1_2.png</string><key>loading_2_1</key><string>loading_2_1.png</string><key>loading_2_2</key><string>loading_2_2.png</string><key>game_background</key><string>game_background.png</string><key>cannon_plist</key><string>cannon.plist</string><key>cannon</key><string>cannon.png</string><key>cannon10_plist</key><string>cannon10.plist</string><key>cannon10</key><string>cannon10.png</string><key>increase_button</key><string>increase_button.png</string><key>reduce_button</key><string>reduce_button.png</string><key>GameLayer_plist</key><string>GameLayer.plist</string><key>game_ui_2p</key><string>game_ui_2p.png</string><key>game_ui_box_01</key><string>ui_box_01.png</string><key>game_ui_box_02</key><string>ui_box_02.png</string></dict></plist>
// Click the callback void StartLayer: start_callback (CCObject * pSender) {CCLOG ("start game"); CCDirector: shareddire () -> replaceScene (CCTransitionFade: create (1.0f, GameLayer: scene ()));}