2015-02-24 84 views
1

我有一個SKSpriteNode用圖像初始化。我正在使用-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event方法來確定用戶觸摸是否在精靈範圍內。出於某種原因,即使我點擊精靈內部時,座標也不相似。他們似乎在不同的座標系統上。UITouch座標和SKSpriteNode座標是不同的

#import "GameScene.h" 

SKSpriteNode *card; 

@implementation GameScene 

-(void)didMoveToView:(SKView *)view { 

     /* Setup your scene here */ 
     self.backgroundColor = [SKColor whiteColor]; 
     card = [SKSpriteNode spriteNodeWithImageNamed:@"card"]; 
     card.position = CGPointMake(500, 500); 

    [self addChild:card]; 


} 

-(void)update:(CFTimeInterval)currentTime { 
    /* Called before each frame is rendered */ 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 
    int cardX = card.frame.origin.x; 
    int cardwidth = card.frame.size.width; 
    int cardY = card.frame.origin.y; 
    int cardHeight = card.frame.size.height; 


    if(touchLocation.x >= card.frame.origin.x && 
     touchLocation.x <= (card.frame.origin.x + card.frame.size.width) && 
     touchLocation.y <= card.frame.origin.y && 
     touchLocation.y >= (card.frame.origin.y + card.frame.size.height)) 
    { 
     self.backgroundColor = [SKColor blackColor]; 
    } 
    else{ 
     self.backgroundColor = [SKColor whiteColor]; 
    } 
} 

@end 
+0

您可以檢查一個點使用相交的節點SKNode(SKScene)的便利方法: nodeAtPoint和nodesAtPoint – 2015-02-25 09:15:22

回答

1

NodeView有不同的座標系。 如果你需要知道在節點抽頭點的位置座標,你可能需要更換線路:

CGPoint touchLocation = [touch locationInView:self.view]; 

由:

CGPoint touchLocation = [touch locationInNode:self];