convertToNodeSpace无法正常工作

时间:2011-08-16 16:46:30

标签: ios cocos2d-iphone ccsprite

使用此代码(稍微调整一下)我无法获得恒定的Y值。它不断变化。 X工作得很完美,但Y确实是偏斜的。

我制作的瓷砖大小与屏幕大小相当。当我抓住并拖动时,它适用于x。所以,如果我点击屏幕上的空格,请说

触及结束x:459.000000 y:705.000000

然后拖一点我得到这个

触及结束x:459.000000 y:823.000000

所以x值保持不变,这是我想要的,但Y值变化没有呈现正确的值

我的打印代码看起来像这样

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);

并且大多数来自这个网站......

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;
  }
}

什么?谢谢!

1 个答案:

答案 0 :(得分:1)

当您移动手指时,UIView fingerOne使用可能已经改变,这可能会使CGPoint倾斜。

试试这个:

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

100%的时间,除非你特意这样做,否则openGLView不会移动或改变。<​​/ p>