Cocos2d-x game instances (11)-mobile tracking with Touch Screen

Source: Internet
Author: User

Small man (Bill man) Personal Original, welcome to reprint, reprint please indicate the address, small man (Bill man) column address http://blog.csdn.net/bill_man

Recently, I was playing the spy mouse game. One of the touch-screen operations of the main character moves, that is, the track of the finger touch the screen will be recorded, and then the main character moves along the track, in fact, this operation is applied in many games and is suitable for touch screen operations. This article will write a simple example of similar implementation in cocos2d-x.

The implementation is simple. First, you need to rewrite the following functions to record the touch screen track:

Virtual void cctouchesbegan (ccset * ptouches, ccevent * pevent );

Virtual void cctouchesmoved (ccset * ptouches, ccevent * pevent );

Virtual void cctouchesended (ccset * ptouches, ccevent * pevent );

In addition, we need a ccmutablearray to record our trajectory, which is defined as follows:

Ccmutablearray <mypoint *> * path;

Mypoint is a ccpoint defined by ourselves. Why do we need to define it? Because ccmutablearray requires that the type must have a retain function, we define a mypoint to let it inherit the retain function. The definition of mypoint is as follows:


It is very simple, that is, there is X, Y, and it can inherit from ccnode.

The following describes the cctouchesbegan function:

First, we get coordinate points. There is no need to talk about a series of transformations. Then we want to get the main character's position. Because I still use the astar algorithm, the main character belongs to the map, therefore, to obtain the absolute coordinates of the protagonist, add the map coordinates directly to the absolute coordinates of the protagonist on the screen. Then, we can determine whether the starting drag point is within the rectangle of the protagonist, if it is within the range of the main character, first clear the path array, add this vertex to the path, and set our index value stepindex to-5, which indicates that the subsequent dragging is effective, the initial value of stepindex is-1,-1 indicates that no command is received, and a value greater than 0 indicates that the main character moves

Let's look at the cctouchesmoved function again:

If the stepindex is-5, it indicates that the subsequent dragging is valid. We will continue to add vertices to our path. Check whether the following vertices are out of the screen range, in addition, we also need to check whether a point is closed and different points are recorded.

Let's look at the cctouchesended function again:

When the touch screen ends and stepindex is-5, it indicates that the subsequent dragging is valid, stepindex is set to 0, indicating that the main character can be moved.

Update the main character position in each frame in the update process, that is, traverse the path. Remember to set the relative coordinates of the main character and the map. When Path Traversal is complete, set stepindex to-1, in this way, you can record the touch-screen track and move the protagonist following this track.

If there are any errors, I hope you can correct them more.

Next continue game instance

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.