Cocos2d-x3.0 game instance of "Don't save me" Article 4-chaos into the main character

Source: Internet
Author: User

 

Well, I 've talked so much nonsense before, and finally I'm going to get into the right question. (wait, are you playing with us before you dare ?)

 

What do you mean? Flowers? No, it's your heart ~

Reprinted please note, original address: http://www.benmutou.com/blog/archives/872

Source: dummies and Game Development

 

It is too easy to add a protagonist. The protagonist must have the following super powers:

1. Cute Look

2. Not fly

3. will drop down

4. Wall hitting will rebound

5. Move left and right

6. The killer is: fart

(Xiao RuO: I don't know if it's an illusion. I always feel that these abilities are personal)

(Wood: Oh, oh, narration, don't be stupid. You won't be able to own the first one)

 

This tutorial to use the image here download: http://yunpan.cn/QNbNZ9JG4Uape access password 8301

 

Show the main character

As mentioned above, the super power is very complicated. Let's start with a simple process and let the main character first come into the scene.

Create a Player class. The header file is as follows:

 
 
  1. #ifndef Player_H
  2. #define Player_H
  3. #include "cocos2d.h"
  4. USING_NS_CC;
  5. class Player : public Node
  6. {
  7. public:
  8.     Player();
  9.     ~Player();
  10.     CREATE_FUNC(Player);
  11.     virtual bool init();
  12.     void logic(float dt);
  13. private:
  14.     CC_SYNTHESIZE(int, m_iHP, iHP);
  15. };
  16. #endif

OK. Obviously, the main character also has a logic function. Leave nothing blank first, and then the main character has an HP attribute.

 

Look at the CPP file:

 
 
  1. # Include "Player. h"
  2. Player: Player ()
  3. {
  4. M_iHP = 100;
  5. }
  6. Player ::~ Player ()
  7. {
  8. }
  9. Bool Player: init ()
  10. {
  11. Auto playerSp = Sprite: create ("player.png ");
  12. This-> addChild (playerSp );
  13. Auto body = PhysicsBody: createCircle (playerSp-> getContentSize (). width * 0.4f );
  14. Body-> getShape (0)-> setFriction (0 );
  15. Body-> getShape (0)-> setRestitution (1.0f );
  16. Body-> setCategoryBitmask (1); // 0001
  17. Body-> setCollisionBitmask (1); // 0001
  18. Body-> setContactTestBitmask (1); // 0001
  19. This-> setPhysicsBody (body );
  20. /* Add thrust to the role */
  21. This-> getPhysicsBody ()-> applyImpulse (Vect (0,-40000 ));
  22. Return true;
  23. }
  24. Void Player: logic (float dt)
  25. {
  26. }

I have already discussed how to create a physical object. You should be familiar with it. Use createCircle to create a circular rigid body. Why is the circle not a square? Because the pictures of the main character are not very square, it will be better to use a circle in a collision.

 

Then, get the skin of the rigid body through getShape (0), setFriction sets the friction, and setRestitution sets the elasticity.

 

Finally, give the rigid body an initial downward thrust (applyImpulse), so that the main character will fall down. Yes, the main character of this game is to keep falling down and then hit something to play ~

 

OK. Test it. Add the Player Object to the init function of toll1_scene:

 
 
  1. Bool toll1_scene: init ()
  2. {
  3. If (! Layer: init ())
  4. {
  5. Return false;
  6. }
  7. /* Create the main character */
  8. Size visibleSize = Director: getInstance ()-> getVisibleSize ();
  9. M_player = Player: create ();
  10. M_player-> setPosition (Point (visibleSize. width * 0.5f, visibleSize. height * 0.85f ));
  11. This-> addChild (m_player, 5 );
  12. This-> schedule (schedule_selector (toll1_scene: logic ));
  13. Return true;
  14. }

The initial position of the main character is in the middle of the screen.

Okay, run it. effect:

 

You should see the protagonist falling down from the beginning, and then bounce when it encounters the following sawtooth ~

Prove that our physical world is already working ~

 

Left and right Injection

Well, now the main character will only fall down. What else should I play? Let's give the protagonist three types of operations: To the left, to the right, fart. (Xiao RuO: Wait! What is Fart ?)

 

Farting means pushing down, um.

 

If you still remember how to add the initial thrust to the main character, these three operations are simple. Let's look at the code. First, add several function declarations. The current Player. h file is as follows:

 
 
  1. #ifndef Player_H
  2. #define Player_H
  3. #include "cocos2d.h"
  4. USING_NS_CC;
  5. class Player : public Node
  6. {
  7. public:
  8.     Player();
  9.     ~Player();
  10.     CREATE_FUNC(Player);
  11.     virtual bool init();
  12.     void moveToRight();
  13.     void moveToLeft();
  14.     void quickMove();
  15.     void logic(float dt);
  16. private:
  17.     CC_SYNTHESIZE(int, m_iHP, iHP);
  18. };
  19. #endif

Why is the fart function named quickMove? That's because farting is a big trick, so I chose this special name. (Xiao RuO: You don't know how to write fart in English)

 

O_O, let's leave it alone, continue ....

 

Let's take a look at the implementation of the three functions:

 
 
  1. void Player::moveToRight()
  2. {
  3.     this->getPhysicsBody()->applyImpulse(Vect(50000, 0));
  4. }
  5. void Player::moveToLeft()
  6. {
  7.     this->getPhysicsBody()->applyImpulse(Vect(-50000, 0));
  8. }
  9. void Player::quickMove()
  10. {
  11.     this->getPhysicsBody()->applyImpulse(Vect(0, -200000));
  12. }

How is it? Is it easy to see the quickMove function? The thrust it adds to the main character is very large ~

 

But how can we test it? How to call these functions? No buttons...

No buttons, so we have to think about it again...

 

Next, we will briefly introduce Cocostudio's UI editor. We will use it to compile several buttons.

 

 

 

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.