Development of Cocos2d-X Implementation of the starting scene of Chinese chess three

Source: Internet
Author: User

Development of Cocos2d-X Implementation of the starting scene of Chinese chess three

The functions and engineering files of Chinese chess are introduced in the previous two blogs. This blog will introduce the implementation of the starting scene of Chinese chess.

Before writing code, clarify the idea of implementing the start scenario:

1. Open the game and enter the start scene. The scene shows a red handsome and black pair, and the two pieces are in the same straight line (the y coordinates of the two pieces are the same)

2. When the player clicks the red handsome one, the red handsome one moves to the right and one side rotates clockwise. The black one moves the other side to the left and rotates in the clockwise direction.

3. When the player clicks the black button, the red handsome side moves to the right side of the rotation in clockwise direction, and the black side moves to the left side of the rotation in a clockwise direction

4. When two pieces collide, they enter the game scene (the distance between the two pieces is smaller than or equal to the diameter of the pieces)

5. If the player clicks the red handsome player, the player's pawn is red.

6. If a player clicks a black icon, the player's pawn is black.


SceneStart. h and SceneStart. cpp are used to implement the initial scenario.

Implementation Code:

Code in SceneStart. h

# Ifndef _ SceneStart_H _ # define _ SceneStart_H _ # include "cocos2d. h "USING_NS_CC; class SceneStart: public CCLayer {public: static CCScene * scene (); bool init (); CREATE_FUNC (SceneStart); bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent); void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent); CCSprite * _ red; CCSprite * _ black; void update (float ); // mark whether the red pawn bool _ selected;}; # endif


Code in SceneStart. cpp

# Include "SceneStart. h "# include" SceneGame. h "CCScene * SceneStart: scene () {CCScene * scene = CCScene: create (); SceneStart * layer = SceneStart: create (); scene-> addChild (layer); return scene;} bool SceneStart: init () {CCLayer: init (); CCSize winSize = CCDirector: shareddire () -> getWinSize (); CCSprite * bkr = CCSprite: create ("bkg2.png"); addChild (bkr); CCSprite * bkb = CCSprite: create ("bkg1.png "); addChild (bkb); bkr-> setPosition (ccp (winSize. width/2-100, winSize. height/2); bkb-> setPosition (ccp (winSize. width/2 + 100, winSize. height/2); _ red = bkr; _ black = bkb; // register the touch event setTouchEnabled (true); setTouchMode (kCCTouchesOneByOne); return true;} bool SceneStart :: success (CCTouch * pTouch, CCEvent * pEvent) {return true;} void SceneStart: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent) {CCSize winSize = CCDirector: shareddire () -> getWinSize (); // obtain the position (coordinate) of the touch point. CCPoint ptClick = pTouch-> getLocation (); // It is used to determine whether the pawns are in the bool bClickStone = false position; // if (_ red-> boundingBox (). containsPoint (ptClick) {// indicates the red pawn this-> _ selected = true; // indicates that the pawns bClickStone = true ;} // when a black pawn is in the position (The position of the touch point is within the range of the black Pawn) else if (_ black-> boundingBox (). containsPoint (ptClick) {// The Red pawn in the missing position this-> _ selected = false; // The pawn is in the position bClickStone = true ;} // if (bClickStone) {// move the pawnpiece CCMoveTo * moveTo1 = CCMoveTo: create (1, ccp (winSize. width/2, winSize. height/2); CCMoveTo * moveTo2 = CCMoveTo: create (1, ccp (winSize. width/2, winSize. height/2); // rotate the chess piece CCRotateBy * rotate1 = CCRotateBy: create (1,360); CCRotateBy * rotate2 = CCRotateBy: create (1,-360 ); // execute CCSpawn * spawn1 = CCSpawn: create (moveTo1, rotate1, NULL) when rotating and moving; CCSpawn * spawn2 = CCSpawn: create (moveTo2, rotate2, NULL ); // execute the row Action _ red-> runAction (spawn1); _ black-> runAction (spawn2); // start the timer scheduleUpdate () ;}} void SceneStart :: update (float) {// obtain the x coordinate float x1 = _ red-> getPositionX (); float x2 = _ black-> getPositionX (); // when the red and black pawns collide, // The distance between the two pawns is smaller than or equal to the diameter of the pawns. // getContentSize (). width: Obtain the width of the pawn (the diameter of the pawn) if (abs (x1-x2) <= _ red-> getContentSize (). width) {// enter the CCDirector: sharedDirector ()-> replaceScene (SceneGame: scene (this-> _ selected ));}}

Execution result:

Zookeeper

Related Article

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.