1

使用此代码(稍作调整)我无法获得恒定的 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;
  }
}

任何事物?谢谢!

4

1 回答 1

1

当您移动手指时,使用的 UIView fingerOne 可能会发生变化,这可能会使 CGPoint 倾斜。

试试这个:

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

100% 的时间,openGLView 不会移动或更改,除非您特别这样做。

于 2011-08-16T22:12:17.450 回答