How to Use cocos2d to create a slide picture game Tutorial: Part 2 (end)

Source: Internet
Author: User

Original article link: http://www.iphonegametutorials.com/2011/03/17/cocos2d-game-tutorial-%E2%80%93-building-a-slide-image-game-part-2-with-solution/

Tutorial:

 

Hello everyone! We will go back to the slide picture game tutorial again and add some new features to it. We will add a judgment to determine whether the player has successfully solved the problem. This task is very clear. Let's start it!

The complete source code of this tutorial is provided here.

 

All the information in this tutorial is based on the previous tutorial. Therefore, before continuing to read this tutorial, please read the first tutorial.

Now, it's time to teach you how to implement it!

First, in tile. H, we need to add a new variable originalvalue to save the initial position of tile. In this way, when you disrupt the tile, we can use this position to compare it with the new position to determine whether the player has completed the puzzle.

 @ Interface tile: nsobject {
Int X, Y, value, originalvalue;
Ccsprite * Sprite;
}

- (ID) initwithx :( Int ) Posx Y :( Int ) Posy;
@ Property (nonatomic, Readonly ) Int X, Y;
@ Property (nonatomic) Int Value;
@ Property (nonatomic) Int Originalvalue;
@ Property (nonatomic, retain) ccsprite * Sprite;
- (Bool) neartile: (Tile * ) Othertile;
- ( Void ) Trade :( Tile * ) Othertile;
- (Cgpoint) pixposition;
@ End

 

Next, open box. m, and at the bottom of the check function, make the following changes:

Desttile. Value=(7*Desttile. X)+Desttile. Y;
Desttile. originalvalue=Desttile. value;
Desttile. Sprite=Sprite;

If you are directly from the first projectSource codeThen, the originalCodeAs follows:

 
Desttile. Value=Imgvalue;
Desttile. Sprite=Sprite;

After you complete these changes, we need to add a new function in box. m to determine the game status.

 -  (Bool) checksolution {

Bool issolved = True ;

For ( Int X = 0 ; X < Size. width; x ++ ){
For ( Int Y = 0 ; Y < Size. height; y ++ ){
Tile * Tile = [Self objectatx: x y: Y];

If (Tile. originalvalue ! = Tile. Value ){
Issolved = False ;
}
}
}

If (Issolved ){
Nslog ( @" The sliding image is solved " );
Return True ;
} Else {
Nslog ( @" The sliding image is not solved " );
Return False ;
}

Return True ;
}

 

Here we traverse all the tiles and compare the values of value and originalvalue. If all the values are equal, it indicates that the solution is successful. Otherwise, it indicates that the solution fails.

Finally, in playlayer. m, we add the following code in the changetilewitha method:

 Nslog (  @" Check Solution  "  );

Bool issolved = [Box checksolution];

Cclabel * Solvedlabel = (Cclabel * ) [Self getchildbytag: ksolvedtext];

If (Issolved ){
[Solvedlabel setstring: @" The puzzle is solved " ];
} Else {
[Solvedlabel setstring: @" The puzzle is not solved " ];
}

 

In this way, every time you exchange two small pictures, you will judge the status of the game and display it on the screen with the cclabel.

If you have any questions, please leave a message.

See the next tutorial!

copyright statement: this article is translated by http://www.cnblogs.com/andyque . Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!

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.