In cctouchesbegan, how does one make the obtained coordinates relative to the coordinates of the entire screen, instead of the coordinates of the current view?

Source: Internet
Author: User
In cctouchesbegan, how does one make the obtained coordinates relative to the coordinates of the entire screen, instead of the coordinates of the current view? In cocos2d, when we handle similar Touch events such as cctouchesbegan in cclayer, we usually use the followingCodeTo obtain the current user click position:
 
Uitouch * Touch = [touches anyobject]; cgpoint touchlocation = [Touch locationinview: [Touch view]; cgpoint gllocation = [[ccdirector shareddire] converttogl: touchlocation]; gllocation = [self converttonodespace: gllocation];

It is generally used here

[Touch locationinview: [Touch view] This sentence means to obtain the relative coordinates of touch's location in the current view. There is a problem at this time, if we only want to process this event on the current layer, if the view clicked by the user is not the view we want to process, there will be a problem at this time, this may cause problems in determining whether the user touch is in the desired region. For example, the following judgment is based on the above Code:

 
// Determine whether touch is in the ruler range if (gllocation. y> =-selbarsprite. contentsize. height * 0.5f & gllocation. Y <= selbarsprite. contentsize. height * 0.5f & gllocation. x> =-selbarsprite. contentsize. width * 0.5f & gllocation. x <= selbarsprite. contentsize. width * 0.5f) {btouchinsideblinderrpoint = true; touchbeginpoint = gllocation; toucholdpoint = gllocation ;}

Today, we finally found a good solution, that is, to use egalview, that is, to obtain the coordinates of touch relative to egalview. In this way, the obtained coordinates are based on the winsize coordinates, and there is no problem in determining the region.

The modified code is as follows:

 
Uitouch * Touch = [touches anyobject]; cgpoint touchlocation = [Touch locationinview: [[ccdirector shareddire] view]; cgpoint gllocation = [[ccdirector shareddirector] converttogl: touchlocation]; gllocation = [self converttonodespace: gllocation];

The intermediate change is:

Cgpoint touchlocation = [Touch locationinview: [Touch view];

Replaced:

CgpointTouchlocation = [Touch
Locationinview:[[Ccdirector Shareddire]
View];

The obtained coordinates are based on the egalview.

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.