Cocos Creator | Flying Smash Bros. development tutorial series (three)

Preview

Specific contents

■ this period, mainly on the course of the game by touching the slide, the direction of motion control characters, as well as during the game, control the character movement area. Below, the slip control by the character motion direction, i.e. which direction the slide, which go out into the direction of the moving person. Limited only in the lower figure of FIG blue boundary movement.

Implement border

■ sports game using a background image region, sprite pattern TILED modified, if necessary, can modify the size of the background, the change region of the game, using a boundary grain boundary, using the same TILED mode, flip, modify the size, adjustment position, in order to achieve the above-described interface effects.

■ All the player's character, they are placed on the root node of a parent covered under the background. The reason is not directly on the background node, in order to facilitate the management characters of all players.

■ figure maximum and minimum vertical and horizontal boundaries, can drag the character at the interface, as best seen in acquired position information in the script, record, behind the character motion control, is used as a threshold.

 

Touch Event Listeners

■ during the game, the characters are in the game area, full map can be moved, so listen for events, you need to put on a big map, as in this case, you can hang on to the root background or character, I choose It is mounted onto the background node.

■ need to listen TOUCH_START, TOUCH_MOVE, TOUCH_END, TOUCH_CANCEL four events, taking into account the movement, the size of the range of movement will affect the gaming experience, I have here a change in control value, when the last change to the next value exceeds 5, It was calculated in accordance with the direction of movement of the shift amount.

■ above, it will retain a position of a variable store, when the movement reaches a preset offset value of 5, to modify the direction of movement of the character, the next updated initial variables. The direction of movement of the character, to calculate the change in the angle between the vector and (0,1) vector.

 

■ the use Vec2 signAngle can easily calculate the angle of the two vectors (rad), the direction of movement of the character is controlled based on this arc.

 

Control the direction of movement

■ course of the game, the finger hold on the screen, the characters in the attack state, release your finger, the characters on the defensive, defending people against the state, it can not be moved. In calculating the characters move position, we need to figure in the state under attack before they can take effect. The following figure is a top defensive state, below is offensive state.

 

防守状态
进攻状态

■ 位置的计算,也是根据上一步中设置的变化角度,计算下一步的运动位置,在人物的 update 函数内,动态更新人物的下一个位置。假设变化的偏移量是 offset,变化的角度是 angle(弧度表示),节点当前的位置是(x,y),下一步的位置(next_x,next_y)计算就是:

next_x = x + offset * sin(angle);

next_y = y + offset * cos(angle);

■ 再判断 next_x 和 next_y 的合法性,如果验证通过,人物的下一个位置就是(next_x, next_y)。

边界的控制

■ 上一步中,计算完人物的下个位置,需要判断是否超过上下左右的边界,在超过时,调整位置,最后直接更新人物位置即可。

■ 游戏一开始,根据配置,加载多个人物到人物的根节点上,记录下玩家自己的节点,人物的运动逻辑,按照上述步骤控制,就可以实现屏幕中多个人物同时游戏的效果。

Guess you like

Origin www.cnblogs.com/caizj/p/12179174.html