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:
- #ifndef Player_H
- #define Player_H
- #include "cocos2d.h"
- USING_NS_CC;
- class Player : public Node
- {
- public:
- Player();
- ~Player();
- CREATE_FUNC(Player);
- virtual bool init();
- void logic(float dt);
- private:
- CC_SYNTHESIZE(int, m_iHP, iHP);
- };
- #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:
- # Include "Player. h"
- Player: Player ()
- {
- M_iHP = 100;
- }
- Player ::~ Player ()
- {
- }
- Bool Player: init ()
- {
- Auto playerSp = Sprite: create ("player.png ");
- This-> addChild (playerSp );
- Auto body = PhysicsBody: createCircle (playerSp-> getContentSize (). width * 0.4f );
- Body-> getShape (0)-> setFriction (0 );
- Body-> getShape (0)-> setRestitution (1.0f );
- Body-> setCategoryBitmask (1); // 0001
- Body-> setCollisionBitmask (1); // 0001
- Body-> setContactTestBitmask (1); // 0001
- This-> setPhysicsBody (body );
- /* Add thrust to the role */
- This-> getPhysicsBody ()-> applyImpulse (Vect (0,-40000 ));
- Return true;
- }
- Void Player: logic (float dt)
- {
- }
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:
- Bool toll1_scene: init ()
- {
- If (! Layer: init ())
- {
- Return false;
- }
- /* Create the main character */
- Size visibleSize = Director: getInstance ()-> getVisibleSize ();
- M_player = Player: create ();
- M_player-> setPosition (Point (visibleSize. width * 0.5f, visibleSize. height * 0.85f ));
- This-> addChild (m_player, 5 );
- This-> schedule (schedule_selector (toll1_scene: logic ));
- Return true;
- }
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:
- #ifndef Player_H
- #define Player_H
- #include "cocos2d.h"
- USING_NS_CC;
- class Player : public Node
- {
- public:
- Player();
- ~Player();
- CREATE_FUNC(Player);
- virtual bool init();
- void moveToRight();
- void moveToLeft();
- void quickMove();
- void logic(float dt);
- private:
- CC_SYNTHESIZE(int, m_iHP, iHP);
- };
- #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:
- void Player::moveToRight()
- {
- this->getPhysicsBody()->applyImpulse(Vect(50000, 0));
- }
- void Player::moveToLeft()
- {
- this->getPhysicsBody()->applyImpulse(Vect(-50000, 0));
- }
- void Player::quickMove()
- {
- this->getPhysicsBody()->applyImpulse(Vect(0, -200000));
- }
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.