Game Design and Implementation of "One cannot die"

Source: Internet
Author: User

Disclaimer: This article is my work made after watching the video on the Internet. It is not true or original (the animation is implemented by myself and summarized by myself). Thank you for sharing your ideas, your contribution is also the motivation to inspire us to move forward. Thank you !!! Congratulations! (haha, it's a little fun to get things in series X !)

Start the text below:


Design and description of the game "one cannot die:


Gamescen: inherited from Layer

Create a gamecontroller and upload the current layer and Y coordinates.
Create a set. The GCS stores all created gamecontroller objects for collision detection.


Set two listening events:


1. Collision listening
Auto Listen = eventlistenerphysicscontact: Create ();
Listen-> oncontactbegin = [=] (physicscontact & contact ){
Director: getinstance ()-> replacescene (gameover: cretescene ());
Log ("Wow !!, I hit Ah ");
Return true;
};
// Dispatch events
Director: getinstance ()-> geteventdispatcher ()-> addeventlistenerwithscenegraphpriority (listen, this );


2. Touch listening
// Add touch events
Auto listener = eventlistenertouchonebyone: Create ();
Listener-> ontouchbegan = [=] (touch * t, event * E ){
For (Auto it = GCS. Begin (); it! = GCS. End (); It ++ ){
If (* It)-> hittestpoint (t-> getlocation ())){
(* It)-> onusertouch ();
Break;
}
}
Return true;
};
Director: getinstance ()-> geteventdispatcher ()-> addeventlistenerwithscenegraphpriority (listener, this );


Set a timer:
Void gamescene: Update (float t ){
For (Auto it = GCS. Begin (); it! = GCS. End (); It ++ ){
(* It)-> onupdate (t );
}
}



Block: inherits from sprite obstacles
Set random width and height, and delete from the screen
Set the color. settexturerect () is used to specify the region.
Set contentsize ();
Setphysicsbody (physicsbody: createbox (size); // The specified physical body must be !!!
Getphysicsbody ()-> setcontacttestbitmask (1); // sets the collision ID. By default, the collision detection is not detected. // sets the collision detection.
Getphysicsbody ()-> setdynamic (false); // It is a dynamic object by default. It is affected by gravity. If it is not set, the villain will be blocked !!
The callback function keeps the block going to the left and clears it from the screen.


Edge: inherits from node to add a border box to prevent the villain from dropping out of the screen

Size size = Director: getinstance ()-> getvisiblesize ();
// Create a border box
Setphysicsbody (physicsbody: createedgebox (size ));
Setcontentsize (size );


Gameover

20 lines of code are omitted here ...... Haha




Gamecontroller: Inheriting from ref is the key to the main design, because you need to create an uncertain number of people and pass the layer and position to gamecontroller in gamescene.
So here we need to override the create and init parameters (Layer * layer, float postion) method.


Here we mainly create:
Border
Hero
Floor:
Auto ground = sprite: Create (); // This play is an genie, hacker
Set the color // personal prefers yellow ...... 3b Woo
Settexturerect setting area
Location



Hero: inherited from Sprite. Because the villain wants to exercise, a plist file must be included during creation in gamecontroller and created through the following statement.
Override the create method:
Hero * hero: Create (STD: String filename ){
Hero * hero = new hero ();
If (hero & hero-> Init (filename )){
Hero-> autoreal.pdf ();
Return hero;
}
Cc_safe_delete (HERO );
Hero = NULL;
Return hero;
}


The override init method:
Hero * hero: Init (STD: String filename ){
If (! Sprite: Init ()){
Return false;
}
// Create an animation based on the uploaded file
Spriteframecache: getinstance ()-> addspriteframeswithfile (filename );

Animation * ani = Animation: Create ();


For (INT I = 0; I <4; I ++ ){
STD: String strname = stringutily: formate ("majinajie+d.png", I );
Spriteframe * sp = spriteframecache: getinstance ()-> addspriteframebyname (strname );
Ani-> addspriteframe (SP );
}
Ani-> setdelayperunit (0.1 );
Ani-> setloops (-1 );
Auto AMT = Animate: Create (ANI );


This-> runaction (AMT );

Return true


}

Getphysicsbody ()-> setcontacttestbitmask (1); // sets the collision ID, which is not detected by default.


The above is my summary, there is not perfect place, due to time reasons, just out of a model, there will be time to improve, the following together with the source code: http://pan.baidu.com/s/1eQFzNj0




Game Design and Implementation of "One cannot die"

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.