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.