Super Mary 2

Source: Internet
Author: User
Zookeeper

Stage 2:

Objective: To enable Mario to accept the control of left and right movement and jump, run the left and right walking animation and maintain the air status, and design the Mario state machine. There is no collision detection during this process.

1: This is a horizontal scroll control category game. Although the touch mechanism on the simulated mobile device is clicked with the mouse, it is still not convenient for the trial. For example, when two buttons are pressed at the same time, you can't do it with a single mouse touch. Therefore, in order to achieve the convenience of testing at the early stage of development, we need to slightly modify the engine so that the game can receive messages from the keyboard and respond. This is not difficult to do. The link here is very clear, and it will be OK. [Http://blog.csdn.net/a7833756/article/details/7772115]

Ccgamelayer: voidprocesswin32keypress (uintmessage, wparam, lparam); this method can capture messages from the keyboard and process different buttons in the function, this is consistent with Win32 programming.

2: The simulation physical environment is not used in the game. The left and right movements and jumps in the game are implemented through the change of the forward value. In the ccgamelayer class, there are two variables about left and right movement, floatccmoveoffset; it is a fixed value initialized in the constructor, floatmoveoffset; it is convenient for unified external updates, during the game, if you press the left button, the moveoffset will be attached with-ccmoveoffset. If you press the right button, the moveoffset will be attached with ccmoveoffset.

This is also for the convenience of subsequent changes. If there are numbers everywhere, it will be troublesome to change. In this way, if you want to change it, you only need to change the ccmoveoffset value.

3: The following describes the core idea of Maio's walking and jumping operations in the game. In the update (floatdt); of the ccgamelayer class, this is a function that controls the update of Mario. The core code of this function is:

This function runs once every frame. Two bool values are maintained in the ccgamelayer class, indicating whether the Left or Right bool values are pressed.

These four buttons are controlled by four buttons on the UI control handle. When the left button is pressed, isleftkeydown is true and moveoffset is set to-ccmoveoffset. The same applies when the right-click is pressed.

4: let Mario run the walking animation while moving left and right. We need to talk about cocos2dx about animation. Frame Animation is used here, which means to make an animation with the same size, and stored in the engine's own animation cache. The animation production, storage, and calling are all in the animationmanager class:

This is a singleton. Singleton is a common design mode. In principle, the game is executed and only one object class can be designed as a singleton. There are also a lot of classes in the engine designed as a Singleton, such as the director class ccdirector, as well as various cache classes cctexturecache, ccanimationcache and so on. In the Mario class, animation is called through enumeration. In the gameenum. h file:

This file contains all the enumerations used in the game, such as animations in the game, types of monsters, and various collision detection elements on the map. On the basis of the previous paragraph, when you press the left button, Mario plays the animation that is walking left at the same time, and finally adjusts the playing interval and moving speed of the animation, which looks more natural, the values involved here are not directly written in the code, but in the form of arguments, which facilitates debugging and modification.

5: Skip implementation. This is different from the previous concept of left-side walking. The Skip does not need to maintain a bool value. When the hop button is pressed, It is vacated, in addition, the texture of Mario is also changed to a vacant image. This involves a bool logo for whether Mario is in the air. Since the jump button cannot be ineffective when Mario is vacant, it is only when Mario is on the ground or on the bricks of the map, capable of responding to jump keys. When Mario falls onto the ground, Mario's texture is selected based on the current orientation of Mario. Since Mario received the response from both the left and right buttons in the air, when Mario jumps to the right, he suddenly presses the left button, mario's turn will change. Here, how can we determine whether or not Mario is in the air status involves collision detection in the vertical direction, that is, collision detection with some of the standing bricks on the ground and the map. The following is the logic of the jump button after it is pressed:

The last line in the ccgamelayer: collistionv () function:

In this way, we can achieve a gradual deceleration effect during the upward jump process of Mario until jumpoffset is reduced to 0. At this time, Mario also reached the highest jump, and continued to run the subtraction above, mario will be on the ground again, and the running logic is as follows:

6: maintenance of the Mario state machine. The performance of Mario in the game is that a state machine is switching freely. How can we refine the status of Mario in the game? One aspect is from the shape of Mario, which refers to the shape of before and after eating mushrooms, and to the bullets. The other aspect is from the motion of Mario, for example, standing left and right, walking left and right, and left and right. All the logic in the state machine of Mario is handled in this way. One point to mention after the group is that the state of Mario is also set for every frame, which brings about a problem, assuming that the current State is the same as the previous state, it is not necessary to update the state of Mario. At this time, a current state and the previous state are introduced. The state machine function starts with an assumption:

Assume that the current status is the same as that of the slave data transmission. If the current status is different, it means that the status of Mario has changed and then all the actions of Mario are stopped, matches and updates based on the new status.

7: the implementation of the control handle is neither technically difficult nor hard to understand. It is to respond to the Win32 Keyboard Message and set the control variable of Mario Based on the message.

8: tracking implementation of maps and implementation that Mario cannot go back. This may be hard to understand, and there are a lot of questions on the network. Before solving the problem, we must first have a deep understanding of the concept of the anchor and the relationship between the local coordinate systems, these two do not understand that this requirement is very difficult to implement.

Conclusion: The results of this phase: Mario can accept and process messages from the keyboard. Mario can walk freely on the map, and there is a real walking animation that can jump and land, can Jump to some bricks on the map,

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.