Cocos2d-x 3.0 game instance learning notes the fourth step of "parkour"-MAP cycle & amp; Add the protagonist action, cocos2d-x "parkour"

Source: Internet
Author: User

Cocos2d-x 3.0 game instance learning notes the fourth step of "parkour" -- MAP cycle & add the protagonist action, cocos2d-x "parkour"

(Here is the reference: Xiaofeng residual month predecessors blog, he is the teiran network of the parkour tutorial, with the cocos2d-x 2.X version of rewriting, I am learning cocos2d-X3.0 so I will use cocos2d-X 3.0 rewrite, take notes

In this step, we mainly complete the following functions:

1. Infinite scrolling of the map --- make the protagonist look really running

2. Add the Jump and crouch actions to the main character

First, let's Scroll the background and add the following in PlayScene. h:

// Initialize the background void initBG (); // use the update function to scroll the map to virtual void update (float dt); // The background genie cocos2d: Sprite * bgSprite1; cocos2d :: sprite * bgSprite2; cocos2d: Sprite * groundSprite1; cocos2d: Sprite * groundSprite2;
Then, in the. Cpp file:

Void PlayScene: initBG () {auto visibleSize = Director: getInstance ()-> getVisibleSize (); // background 1bgSprite1 = Sprite: create ("Map00.png "); bgSprite1-> setPosition (visibleSize. width/2, visibleSize. height/2); this-> addChild (bgSprite1); // ground 1groundSprite1 = Sprite: create ("Ground00.png"); groundSprite1-> setPosition (visibleSize. width/2, groundSprite1-> getContentSize (). height/2); this-> addChild (groundSprite1); // background 2bgSprite2 = Sprite: create ("Map01.png"); bgSprite2-> setPosition (bgSprite1-> getContentSize (). width + visibleSize. width/2, visibleSize. height/2); this-> addChild (bgSprite2); // ground 2groundSprite2 = Sprite: create ("Ground01.png"); groundSprite2-> setPosition (bgSprite1-> getContentSize (). width + visibleSize. width/2, groundSprite2-> getContentSize (). height/2); this-> addChild (groundSprite2);} void PlayScene: update (float dt) {int posX1 = bgSprite1-> getPositionX (); int posX2 = bgSprite2-> getPositionX (); posX1-= 2; posX2-= 2; auto mapSize = bgSprite1-> getContentSize (); if (posX1 <-mapSize. width/2) {posX1 = mapSize. width + mapSize. width/2; posX2 = mapSize. width/2;} if (posX2 <-mapSize. width/2) {posX2 = mapSize. width + mapSize. width/2; posX1 = mapSize. width/2;} bgSprite1-> setPositionX (posX1); bgSprite2-> setPositionX (posX2); groundSprite1-> setPositionX (posX1); groundSprite2-> setPositionX (posX2 );}
To accomplish this, we will use the init function in PlayScene:

Bool PlayScene: init () {if (! Layer: init () {return false;} SimpleAudioEngine: getInstance ()-> playBackgroundMusic ("background#", true); initPhysicWorld (); initBG (); // enable updatethis-> scheduleUpdate (); m_runner = Runner: create (); m_runner-> setPosition (runner_posX, ground_hight + m_runner-> getRunJumpSize (). height/2); m_runner-> Run (); this-> addChild (m_runner); return true ;}
Note: we should place the initialization of the background before the creation of the main character, because the first creation of the main character is equivalent to drawing the main character first on the screen, and then painting the background, then the main character will be blocked. Of course, you can also set the painting level here, so the operation is as follows:



This is not dynamic, so I cannot see the effects of map scrolling. Please provide guidance...


Next we will add other actions for the main character:

In the initActionSet function of Runner, we first add another Frame Animation:

Void Runer: initActionSet (SpriteFrameCache * frameCache) {SpriteFrame * frame = NULL; // use vector instead of ArrayVector <SpriteFrame *> frameVector in 3.0;/* 1. ---------------- load the running Animation --------------- */for (int I = 0; I <= 7; I ++) {// load the genie from the cache pool to Vectorframe = frameCache-> spriteFrameByName (String: createWithFormat ("runner+d.png", I)-> getCString (); frameVector. pushBack (frame);} // use the SpriteFrame list in the vector to create an Animation and set some parameters: auto run_animation = Animation: createWithSpriteFrames (frameVector, 0.1f,-1 ); // name the running Animation as running AnimationCache: getInstance ()-> addAnimation (run_animation, "running "); /* 4 ------------------ load the rising animation during the jumping process ------------------------- */frameVector. clear (); for (int I = 0; I <= 3; I ++) {frame = frameCache-> spriteFrameByName (String: createWithFormat ("runnerjumpup1_d.png", I) -> getCString (); frameVector. pushBack (frame);} auto jumpUp_animation = Animation: round (frameVector, 0.2); // do not set an infinite loop AnimationCache: getInstance ()-> addAnimation (jumpUp_animation, "jumpUp");/* ------------------ load the animation that falls in the jumping process ------------------------ */frameVector. clear (); for (int I = 0; I <= 1; I ++) {frame = frameCache-> spriteFrameByName (String: createWithFormat ("runnerjumpdown1_d.png", I) -> getCString (); frameVector. pushBack (frame);} auto jumpDown_animation = Animation: trim (frameVector, 0.3); AnimationCache: getInstance ()-> addAnimation (jumpDown_animation, "jumpDown "); /* ------------------ load and squat --------------------------------------------- */frameVector. clear (); frame = frameCache-> spriteFrameByName ("runnerCrouch0.png"); frameVector. pushBack (frame); auto crouch_animation = Animation: createWithSpriteFrames (frameVector, 0.3); // do not set infinite loop AnimationCache: getInstance ()-> addAnimation (crouch_animation, "crouch ");}
Add the Jump, Crouch, and update functions:

void Jump();void Crouch();virtual void update(float dt);
Implementation:

Void Runner: Jump () {// you can only take off when running. if (m_state = running) {m_state = jumpUp; auto mass = this-> getPhysicsBody () -> getMass () * 150; // this-> getPhysicsBody ()-> applyImpulse (Vect (0, mass); m_runner-> stopAllActions (); doAction ("jumpUp") ;}} void Runner: update (float dt) {auto vel = this-> getPhysicsBody ()-> getVelocity (); if (m_state = jumpUp) {if (vel. y <0.1) {m_state = jumpDown; m_runner-> stopAllActions (); doAction ("jumpDown") ;}} if (m_state = jumpDown) {CCLOG ("% f ", vel. y); // It should not be equal to 0if (vel. y> 0) {m_state = running; m_runner-> stopAllActions (); doAction ("running") ;}} void Runner: Crouch () {// you can only squat if (m_state = running) {m_state = crouch; m_runner-> stopAllActions (); initBody (); doAction ("crouch ");}}

HereJumpWe will give the protagonist an upward force, and this force is a instantaneous force, so that the protagonist will rise, but the acceleration will slowly reach the highest point and then fall due to gravity;

Here UpdateThe function is used to do this: when the main character jumps up to the highest point, it should fall, switch to the falling state, and execute the animation of the falling frame. This is a reference for the predecessors. First, in the JumpUp state, if the speed of y is less than 0.1, we think it is the highest point and switch to the drop. In the JumpDown State, if the y speed is greater than 0, we think it has reached the ground and switched the running state. The predecessor's judgment is vel. I debugged the y = 0 process, because the speed is float, = 0 is not accurate, then, my main character is always in the action of falling .....

Now, we have prepared all the actions for the main character. Next, we will add a button to control the main character to execute these actions !!


Personal ignorance, welcome to correction and discussion (it seems that no one will discuss with me, Ah)


Cocos2d-x 30 game engine, what language to edit the game

Supports c ++, lua, and javascript. The underlying layer is implemented in c ++, and the upper layer can be bound to two other scripting languages.
 
What are the best places to study Cocos2d-x game training school? It is also responsible for employment

Learning Cocos2d-x game professional good Oh, looking for large formal, the tuition is cheap but can't learn anything don't money is useless

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.