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

指を動かしたときに fingerOne が使用する UIView が変更され、CGPoint が歪んでいる可能性があります。

これを試して:

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

100% の確率で、openGLView は特別に動かさない限り移動したり変更したりしません。

于 2011-08-16T22:12:17.450 に答える