IOS cocos2d cocos2d-x game do not step on the white
Finally:
Game scenarios
/// whitesquarescene. h // 01_cocos2d-x // created by beyond on 14-10-7. /// # ifndef ___ begin cocos2d_x _ whitesquarescene __# define ___ begin cocos2d_x _ whitesquarescene __# include "Square. H "# include" endline. H "class endline; using_ns_cc; Class whitesquarescene: Public cocos2d: layer {PRIVATE: // screen size winsize; // temporarily accumulate the total number of rows added currently (when 50 rows are added, add the end row) int temptotalline; // whether the end row bool isendlineshowing is displayed; // timer label * timerlabel; // wrap all the items in a node to facilitate overall control of node * _ gamelayer; // mark whether bool istimerrunning is being timed; // The timestamp long starttime when the game starts; // The endline * currentendline of the end line; public: // call init create_func (whitesquarescene) in the static create () method ); virtual bool Init (); // for external calls (Director) Static cocos2d: Scene * createscene (); // Add the start line void addstartline (); // Add the end line void addendline (); // Add a normal game row. The bottom line is the start line, the row number of a normal row starts from 1, 2, 3, and so on. // The reason for adding a normal row is that the row number must be input as the parameter because, the row number determines the Y value of the normal row on the screen void addnormalline (INT lineindex); // you can add a listener to listen to the current layer. If you click the first line of the normal game line, start the game void addlayertouchhandler (); // The game control game initializes void initgame (); // the game layer moves down void gamelayermovedown (); void starttimer (); void stoptimer (); virtual void Update (float DT) ;};# endif/* defined (___ define cocos2d_x _ whitesquarescene _) */
/// whitesquarescene. CPP // 01_cocos2d-x // created by beyond on 14-10-7. //// # include "whitesquarescene. H "# define ksquarewidth winsize. width/4 # define ksquareheight winsize. height/4using_ns_cc; SCENE * whitesquarescene: createscene () {auto scene = scene: Create (); // The init method auto layer = whitesquarescene is called inside the create method :: create (); scene-> addchild (layer); Return scene ;}# Pragma mark-Add rows (start row, normal game row, end Rows) // Add the start row to occupy 1/4 of the bottom of the screen. Do not display the text void whitesquarescene: addstartline () {auto B = square: createwithargs (color3b: yellow, size (winsize. width, ksquareheight), "", 20, color4b: Black); // Add it to the game layer to facilitate management of _ gamelayer-> addchild (B ); // bind the row where it is located; the start row is 0; the normal row is 1, 2, 3 B-> setlineindex (0 );} // Add black and white rows (only one black row exists in this row and appears randomly) // The reason why the row number is used as a parameter when a normal row is added is that, the row number determines the value of void whitesquarescene: addnormalline (INT lineind Ex) {// every time you add a game row, use the member variable to remember that when 50 rows are reached, add the end row temptotalline ++; Square * B; // there is only one black row in the normal row, and INT blackindex = rand () % 4 is displayed at random; // create four small squares for (INT I = 0; I <4; I ++) {color3b color = blackindex = I? Color3b: Black: color3b: White; // the width and height are both 1/4 of the screen, minus 1 to have a gap B = square: createwithargs (color, size (ksquarewidth-1, ksquareheight-1), "", 20, color4b: Black); // Add it to the game layer to facilitate management of _ gamelayer-> addchild (B ); // important ~~~ The bottom line is the starting row. The row number of the normal row starts from 1, and B-> setposition (I * ksquarewidth, lineindex * ksquareheight ); // bind the row number. // remember the normal row where each square is located, because the row number determines the Y value of square B-> setlineindex (lineindex );}} // Add a green box with the end row filled with the full screen, and display the text void whitesquarescene: addendline () {auto B = endline: createwithcontext (this ); // bind the row where it is located; the start row is 0; the normal row is 1, 2, 3; the end row is 4 B-> setlineindex (4 ); b-> setpositiony (B-> getlineindex () * ksquareheight ); // Add it to the game layer to facilitate management of _ gamelayer-> addchild (B); currentendline = B ;}# Pragma mark-Initialize // initialize bool whitesquarescene: Init () {// initialization of the parent class if (! Layer: Init () return false; // important ~~~~ Use the current timestamp as the seed srand (Time (null) of the random number; // screen size winsize = Director: getinstance ()-> getvisiblesize (); // use a node to package all the stuff for easy overall control _ gamelayer = node: Create (); addchild (_ gamelayer ); // Add a timer label timerlabel = label: Create (); timerlabel-> settextcolor (color4b: Blue); timerlabel-> setsystemfontsize (48 ); timerlabel-> setposition (winsize. width/2, winsize. height-100); addchild (timerlabel); // game initialization initgame (); // Add a listener for the current layer. If you click the first line of the normal game line, start the game addlayertouchhandler (); Return true ;} # pragma mark-layer touch listener // Add listener for the current layer. If you click the first line of the normal game line, start the game void whitesquarescene: addlayertouchhandler () {// single-touch auto listener = eventlistenertouchonebyone: Create (); listener-> ontouchbegan = [this] (touch * t, event * E) {auto squarearr = square :: getsquarearr (); Square * s; // traverses an array of all square objects and retrieves each square for (Au To it = squarearr-> begin (); it! = Squarearr-> end (); It ++) {// extract each square S = * it; // If the row number of the square is 1, it indicates it is the first row in the normal row; // and a square in row 1st of the normal row is touched // and it is black; start Timing If (S-> getlineindex () = 1 & S-> getboundingbox (). containspoint (t-> getlocation () {// black if (S-> getcolor () = color3b: Black) {// 1st times click the black block, if (! Istimerrunning) {This-> starttimer () ;}// after the black icon is clicked, it turns gray S-> setcolor (color3b: Gray ); // execute the overall move-down animation this-> gamelayermovedown ();} else if (S-> getcolor () = color3b: Green) {// click green, that is, The endline // moves down and stops timing this-> gamelayermovedown (); this-> stoptimer ();} else {// turns to the red alert S-> setcolor (color3b :: red); // in other cases, the white block is accidentally clicked. The MessageBox ("You clicked wrong", "clicked wrong") is displayed. This-> initgame ();} // important ~~~ Jump loop, no need to traverse squarearr break;} return false;}; // register the listener to the event distributor and listen on the entire layer Director: getinstance () -> geteventdispatcher ()-> addeventlistenerwithscenegraphpriority (listener, this);} # pragma mark-game control // game control start Game Void whitesquarescene: initgame () {// init stoptimer (); // The variable temptotalline used to record the number of current game rows is cleared at the beginning = 0; // whether the end row is being displayed // isendlineshowing = false; istimerrunning = false; Cu Rrentendline = NULL; timerlabel-> setstring ("0.000000"); // each time you restart the game, clear the square object array square: removeallsquares (); // Add the start line addstartline (); // Add the normal game line addnormalline (1); addnormalline (2); addnormalline (3 );} // void whitesquarescene: gamelayermovedown () {// temporarily accumulate the total number of rows added currently (when 50 rows are added, add the end row) if (temptotalline <50) {// Add the first row of the normal row; because the starting row number is 0; the normal row number starts from 1; therefore, 4 indicates addnormalline (4) at the top of the screen );} else if (! Isendlineshowing) {// whether the end row is being displayed // display (only once) addendline (); isendlineshowing = true ;} // traverse all squares so that all squares can be moved down. Auto squarearr = square: getsquarearr (); // use the iterator to traverse the object array for (Auto it = squarearr-> begin (); it! = Squarearr-> end (); It ++) {// obtain each square by unreferencing it. Let the square perform the movedown operation (* It)-> movedown ();} if (currentendline! = NULL) {// when the row number of the last row is 1, move it down if (currentendline-> getlineindex () = 1) {// game end // overall move down gamelayermovedown (); // stop the timer stoptimer () ;}}# Pragma mark-clock method // time control void whitesquarescene :: update (float DT) {// get the time continuously. During calculation, long timeinterval = clock ()-starttime; // set the timer tag to convert the second to the sixth Power STD :: string STR = stringutils: Format ("% G", (double) timeinterval)/1000000); timerlabel-> setstring (STR);} // start timing void whites Quarescene: starttimer () {If (! Istimerrunning) {scheduleupdate (); // the start time of the game. starttime = clock (); istimerrunning = true ;}}// stop the timer void whitesquarescene: stoptimer () {If (istimerrunning) {unscheduleupdate (); istimerrunning = false ;}
Square
// square. h // 01_cocos2d-x // created by beyond on 14-10-7. /// square # ifndef ___ character cocos2d_x _ square __# define ___ character cocos2d_x _ square __# include using_ns_cc; Class square: Public sprite {PRIVATE: // static array. Each time a square object is created, it is added to the array. Static vector * squarearr; int lineindex; public: // creation parameters: color, size, text displayed, font size, and font color of the square. Static square * createwithargs (color3b color, size, STD: String label, float fontsize, color4b textcolor); // initialization parameter: block color, size, displayed text, font size, font color virtual bool initwithargs (color3b color, size, STD :: string label, float fontsize, color4b textcolor); // obtains the static vector of the object array * getsquarearr (); // traverses the object array, starting from the last object in the array, remove from parent container (layer); and remove static void removeallsquares () from array; // remove (parent container and array) void removesquare (); // remember the normal row where each square is located, because the row number determines the Y value of square int getlineindex (); // remember the normal row where each square is located, because the row number determines the Y value of square void setlineindex (INT lineindex ); // perform the void movedown () ;};# endif/* defined (___ define cocos2d_x _ square _) for each cube to move to a row */
// square. CPP // 01_cocos2d-x // created by beyond on 14-10-7. //// # include "Square. H "# define kwinsize Director: getinstance ()-> getvisiblesize () # define ksquarewidth kwinsize. width/4 # define ksquareheight kwinsize. height/4 // static array. Each time a square object is created, it is added to the array. Vector * Square: squarearr = new vector (); # pragma mark-lifecycle method // parameter: Square color, size, displayed text, font size, font color square * Square: createwithargs (color 3b color, size, STD: String label, float fontsize, color4b textcolor) {auto B = New Square (); // call the init method to initialize B-> initwithargs (color, size, label, fontsize, textcolor); B-> autorelease (); // Add squarearr-> pushback (B) to the object array for every square object created ); return B;} // parameter: color, size, text displayed, font size, font color bool square: initwithargs (color3b color, size, STD :: string label, float fontsize, color4b textcolor) {// init sprite: init of the parent class (); // Lineindex = 0; // setcontentsize (size); // setanchorpoint (point: zero) in the lower left corner of the anchpoint; // settexturerect (rect (0, 0, size. width, size. height); setcolor (color); // The text is displayed in the center of the square. Auto L = label: Create (); L-> setstring (Label ); l-> setsystemfontsize (fontsize); L-> settextcolor (textcolor); addchild (l); L-> setposition (size. width/2, size. height/2); Return true ;}# Pragma mark-getter method vecto for external calls // object Array R * Square: getsquarearr () {return square: squarearr;} // traverses the object array, starting from the last object in the array, from the parent container (layer) remove; and remove void square: removeallsquares () {While (getsquarearr ()-> size () {// traverses the object array, removes the last object from the array from the parent container (layer) and getsquarearr ()-> back ()-> removefromparent (); getsquarearr () -> popback () ;}/// remove (in the parent container and array) void square: removesquare () {// auto c = getcolor (); // log ("Remove square, color I S (% d, % d, % d) ", C. r, C. g, C. b); // remove removefromparent () from the parent container (layer); // remove squarearr-> eraseobject (this) from the object array );} // remember the normal row where each square is located, because the row number determines the Y value of square void square: setlineindex (int I) {This-> lineindex = I;} // each square remembers its own normal row, because the row number determines the Y value of square int square: getlineindex () {return this-> lineindex;} // perform the void square: movedown () {// Since the square moves one row by itself, then the row number is reduced by this-> lineindex --; I F (getnumberofrunningactions ()! = 0) {stopallactions ();} // move the move-down action to the specified coordinate (you can also use moveBy) moveTo * To = moveTo: Create (0.1f, point (getpositionx (), lineindex * ksquareheight); // The callback action callfunc * func = callfunc: Create ([this] () {// If the screen is displayed, directly call the remove box (which is removed from both the parent container and array internally) if (lineindex <0) {This-> removesquare ();}}); // sequence * s = sequence: Create (to, func, null); // execute the action runaction (s) ;}
Encapsulated end line endline
/// endline. h // 01_cocos2d-x // created by beyond on 14-10-7. /// # ifndef ___ export cocos2d_x _ endline __# define ___ export cocos2d_x _ endline __# include "Square. H "# include" whitesquarescene. H "// The Declaration uses the main scenario class whitesquarescene of the game. // the end line inherits from the class endline: Public Square {PRIVATE: size _ winsize; whitesquarescene * _ context; public: // The main scenario whitesquarescene must be used for static method creation. The init method static endline * createwithcontext (whitesquarescene * context) is called internally. // In the init method, implement real initialization of bool initwithcontext (whitesquarescene * context) ;};# endif/* defined (___ define cocos2d_x _ endline _) */
/// endline. CPP // 01_cocos2d-x // created by beyond on 14-10-7. //// # include "endline. H "// The main scenario whitesquarescene needs to be used when creating static methods; Internally calling the init method endline * endline: createwithcontext (whitesquarescene * context) {auto El = new endline (); // In the init method, implement the true initialization El-> initwithcontext (context); El-> autorelease (); // Add the square object to the static object array: Square: getsquarearr ()-> pushback (EL); Return El;} // In the init method, implement real initialization bool endline: initwithcontext (whitesquarescene * context) {This-> _ context = context; _ winsize = Director: getinstance ()-> getvisiblesize (); // full screen Green Square: initwithargs (color3b: Green, _ winsize, "Game end", 50, color4b: Black); Auto label = label: Create (); label-> setstring ("play again"); label-> setsystemfontsize (50); label-> setposition (_ winsize. width/2, label-> getcontentsize (). height/2 + 50); // text Red Label-> settextcolor (color4b: Red); addchild (Label); // click Label [play again ], restart the game auto listener = eventlistenertouchonebyone: Create (); listener-> ontouchbegan = [this] (touch * t, event * E) {// click label if (e-> getcurrenttarget ()-> getboundingbox (). containspoint (t-> getlocation ()-E-> getcurrenttarget ()-> getparent ()-> getposition ())) {// start the game this-> _ context-> initgame ();} return false ;}; // register the listener Director: getinstance ()-> geteventdispatcher () -> addeventlistenerwithscenegraphpriority (listener, label); Return true ;}
cocos2d_x_07 _ game _ do not step on the white block