2011-08-16 9 views
1

En utilisant ce code (légèrement modifié) je ne peux pas obtenir une valeur Y constante. ça ne cesse de changer. X fonctionne parfaitement mais Y est vraiment asymétrique.convertToNodeSpace ne fonctionne pas correctement

Je fais des carreaux de la taille d'un écran. et quand je prends et fais glisser, ça marche bien pour x. donc si je clique sur un espace à l'écran, disent

Touches terminées x: 459,000000 y: 705,000000

puis faites glisser un peu loin je reçois ce

Touches terminées x: 459,000000 y: 823,000000

Ainsi, la valeur de x séjour qui est ce que je veux, mais les changements de valeur Y ne présentant pas la valeur correcte

Mon code pour l'impression ressemble à ceci

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

et la majorité vient de ce site ...

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

Tout? Merci!

Répondre

1

Les utilisations UIView fingerOne ont peut-être changé lorsque vous avez déplacé votre doigt, ce qui peut fausser le CGPoint.

Essayez ceci:

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

100% du temps, la openGLView ne bouge pas ou changement à moins que vous avez spécifiquement fait.