Cocos2d-x mobile game
Helloworld is the main menu scenario in the game. The helloworld. h file code is as follows:
# DEFINE _ helloworld_scene_h __# include "cocos2d. H "# include" simpleaudioengine. H "① # include" settingscene. H "class helloworld: Public cocos2d: layer {public: static cocos2d: Scene * createscene (); Virtual bool Init (); ② virtual void onenter (); virtual void onentertransitiondidfinish (); Virtual void onexit (); Virtual void onexittransitiondidstart (); Virtual void cleanup (); ③ void menuitemhelpcallback (cocos2d: Re F * psender); void menuitemstartcallback (cocos2d: ref * psender); void encode (cocos2d: ref * psender); create_func (helloworld);}; bool helloworld :: init () {If (! Layer: Init () {return false;} log ("helloworld init"); size visiblesize = Director: getinstance ()-> getvisiblesize (); point origin = Director:: getinstance ()-> getvisibleorigin (); sprite * BG = sprite: Create ("background.png"); BG-> setposition (point (origin. X + visiblesize. width/2, origin. Y + visiblesize. height/2); this-> addchild (BG); // start sprite * startspritenormal = sprite: Create ("start-up.png"); sprite * startspriteselected = sprite :: create ("start-down.png"); menuitemsprite * startmenuitem = menuitemsprite: Create (sequence, startspriteselected, cc_callback_1 (helloworld: sequence, this); startmenuitem-> setposition (ctor :: getinstance ()-> converttogl (point (700,170); // set the image menu menuitemimage * settingmenuitem = menuitemimage: Create ("setting-up.png", "setting-down.png", cc_callback_1 (helloworld:: menuitemsettingcallback, this); settingmenuitem-> setposition (Director: getinstance ()-> converttogl (point (480,400); // help Image menu menuitemimage * helpmenuitem = menuitemimage:: Create ("help-up.png", "help-down.png", cc_callback_1 (helloworld: menuitemhelpcallback, this); helpmenuitem-> setposition (Director: getinstance () -> converttogl (point (860,480); menu * mu = menu: Create (startmenuitem, settingmenuitem, helpmenuitem, null); Mu-> setposition (point: zero ); this-> addchild (MU); Return true;} void helloworld: menuitemsettingcallback (ref * psender) {auto SC = Setting: createscene (); Auto rescene = transitionjumpzoom :: create (1.0f, SC); ctor: getinstance ()-> pushscene (rescene); simpleaudioengine: getinstance ()-> playeffect ("sound/blip.wav "); ②} void helloworld: menuitemhelpcallback (ref * psender) {menuitem * Item = (menuitem *) psender; log ("Touch help % P", item); simpleaudioengine :: getinstance ()-> playeffect ("sound/blip.wav"); ③} void helloworld: menuitemstartcallback (ref * psender) {menuitem * Item = (menuitem *) psender; log ("Touch start % P", item); simpleaudioengine: getinstance ()-> playeffect ("sound/blip.wav"); ④} void helloworld: onenter () {layer: onenter (); log ("helloworld onenter");} void helloworld: onentertransitiondidfinish () {layer: onentertransitiondidfinish (); log ("helloworld onentertransitiondidfinish "); // play simpleaudioengine: getinstance ()-> playbackgroundmusic ("sound/jazzloud", true); ⑤} void helloworld: onexit () {layer: onexit (); log ("helloworld onexit");} void helloworld: onexittransitiondidstart () {layer: reset (); log ("helloworld onexittransitiondidstart");} void helloworld: cleanup () {layer: cleanup (); log ("helloworld cleanup"); // stop simpleaudioengine: getinstance ()-> stopbackgroundmusic ("sound/jazzaudio"); ⑥}
The first line of the above code, using namespace cocosdenshion, uses the namespace cocosdenshion, which is required by the cocosdenshion engine. Lines ②, ③, and ④ simpleaudioengine: getinstance ()-> playeffect ("sound/blip.wav") Play the sound when you click the menu. The fifth line of code is to play background music in the onentertransitiondidfinish () function. The sixth line of code is to stop playing background music in the cleanup () function.
More content please pay attention to the Cocos2d-x series of books "Cocos2d-x practice (Volume I): c ++ development" book exchange discussion site: http://www.cOcoagame.netWelcome to cocos2d-x Technology Discussion Group: 257760386, 327403678