开发者

convertToNodeSpace not working properly

开发者 https://www.devze.com 2023-03-28 06:29 出处:网络
Using this code (tweaked a little) i can\'t get a constant Y value. it keeps changing. X works perfect but Y is really skewed.

Using this code (tweaked a little) i can't get a constant Y value. it keeps changing. X works perfect but Y is really skewed.

Im making tiles about the size of a screen. and when i grab and drag it works开发者_运维问答 fine for x. so if i click a space on screen, say

Touches Ended x : 459.000000 y : 705.000000

and then drag a bit away i get this

Touches Ended x : 459.000000 y : 823.000000

So the x value stay's which is what i want, but the Y value changes not presenting the proper value

My code for printing out looks like this

NSArray *touchArray = [touches allObjects];        
UITouch * fingerOne = [touchArray objectAtIndex:0];    
CGPoint newTouchLocation = [fingerOne locationInView:[fingerOne view]];
CGPoint mapLoc = [self convertToNodeSpace: newTouchLocation];
NSLog(@"Touches Ended x : %f y : %f", mapLoc.x, mapLoc.y);

and the majority comes from this website...

http://firsttimecocos2d.blogspot.com/2011/07/how-to-scroll-and-zoom-inout-of-map.html

    - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//finds the difference in the original map and the scaled map
int differenceX = mapWidth - (mapWidth*mapScale);
int differenceY = mapHeight - (mapHeight*mapScale);    

// This method is passed an NSSet of touches called (of course) "touches"
// "allObjects" returns an NSArray of all the objects in the set
NSArray *touchArray = [touches allObjects];
//Remove Multi Touch
if ([touchArray count] ==1){

    UITouch * fingerOne = [touchArray objectAtIndex:0];

    CGPoint newTouchLocation = [fingerOne locationInView:[fingerOne view]];
    newTouchLocation = [[CCDirector sharedDirector] convertToGL:newTouchLocation];

    CGPoint oldTouchLocation = [fingerOne previousLocationInView:fingerOne.view];
    oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];

    //get the difference in the finger touches when the player was dragging
    CGPoint difference = ccpSub(newTouchLocation, oldTouchLocation);

    //adds this on to the layers current position, effectively moving it
    CGPoint newPosition = ccpAdd(mapLayer.position, difference);

    CGPoint bottomLeft = newPosition;

    //check to see if the map edges of the map are showing in the screen, if so bringing them back on the view so no black space can be seen
    if (bottomLeft.x - differenceX/2 > 0 - (mapWidth * (1-mapScale)) + (1-mapScale)*33) {
        bottomLeft.x = differenceX/2- (mapWidth * (1-mapScale))+(1-mapScale)*33;
    }

    if (bottomLeft.y - differenceY/2 > 0 -(mapHeight * (1-mapScale))) {
        bottomLeft.y = differenceY/2-(mapHeight * (1-mapScale));

    }

    if (bottomLeft.x + mapWidth*mapScale +differenceX/2 < 440+ (1-mapScale)*33) {
        bottomLeft.x = -differenceX/2 - mapWidth*mapScale + 440 + (1-mapScale)*33;

    }
    if (bottomLeft.y + mapHeight*mapScale +differenceY/2 < 320) {
        bottomLeft.y =  -differenceY/2 - mapHeight*mapScale + 320;

    }

    mapLayer.position = bottomLeft;
  }
}

Anything? Thanks!


The UIView fingerOne uses might have changed as you moved your finger, which might skew the CGPoint.

Try this:

CCDirector* director = [CCDirector sharedDirector];
CGPoint newTouchLocation = [fingerOne locationInView:[director openGLView]];

100% of the time, the openGLView doesn't move or change unless you have specifically done so.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号