2012-05-24 41 views
0

我正在開發一款遊戲,其中我試圖分別在單擊和雙擊時分別觸發兩種不同類型的子彈。NSDictionary到CGPoint

繼承人我在做什麼,我觸摸開始方法:

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for(UITouch *touch in touches) 
    { 

     CGPoint location = [touch locationInView: [touch view]]; 
     location = [[CCDirector sharedDirector] convertToGL: location]; 

     NSLog(@"TOUCH LOCATION IN TOUCH BEGAN = (%f , %f)", location.x , location.y); 

     NSUInteger tapCount = [touch tapCount]; 

     switch (tapCount) 
     { 
      case 1: 
      { 
       NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithCGPoint:location] forKey:@"location"]; 
       [self performSelector:@selector(startTimer:) withObject:touchloc afterDelay:3]; 
       break; 
      } 
      case 2: 
      { 
       [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startTimer) object:nil]; 
       [self performSelector:@selector(removeBall) withObject:nil afterDelay:1]; 
       break; 
      } 

      default: 
      { 
       break; 
      } 
     } 
    } 

現在在我的perform selector(startTimer:)我得到點的座標,我在NSPoint觸摸(由於我使用NSDictionary) 所有我想要的知道是..我們如何將這些點轉換爲CGPoints ..?

任何想法我該怎麼做?

任何幫助將得到真正的讚賞。

回答

14

如果你正在使用NSValue您可以從使用CGPointValue得到CGPoint回來。

例如

NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithCGPoint:location] forKey:@"location"]; 
    CGPoint points = [[touchloc valueForKey:@"location"] CGPointValue]; 
+0

一個賈達夫:謝謝老兄:)它的工作就像魅力 – Shailesh

+0

對於我來說,正確的功能保存並返回到cgpoint是NSDictionary的是: 的NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWi thPoint:cgPointPositionTouchPoint] forKey:@「location」]; CGPoint points = [[touchloc valueForKey:@「location」] pointValue]; –

1

CGPointCreateDictionaryRepresentationCGPointMakeWithDictionaryRepresentation

0

的文檔說NSPoint是:

typedef struct _NSPoint { 
    CGFloat x; 
    CGFloat y; 
} NSPoint; 

和CGPoint是:

struct CGPoint { 
    CGFloat x; 
    CGFloat y; 
}; 
typedef struct CGPoint CGPoint; 

所以他們相當的結構,你應該能夠簡單地轉換爲從轉換一個到另一個。既不是對象類型,也不能直接存儲在字典中,而必須將它們放在NSValue中才能將它們轉換爲對象。你可能從你的字典越來越NSValue對象,NSValue有兩種類型的訪問器,這樣你就可以得到你需要直接沒有投一個:

CGPoint a = [somePoint CGPointValue]; 
NSPoint b = [somePoint pointValue];  
+0

:非常感謝,更加清楚的解釋。其實..作爲Objective C的新手..這是我第一次使用NSDictionary,所以有點困惑。 – Shailesh

0

對我來說,爲保存並返回cgpoint正確的函數到NSDictionary中是如下:

NSDictionary * touchloc = [NSDictionary dictionaryWithObject:[NSValue valueWithPoint:cgPointPositionTouchPoint] forKey:@"location"]; 

CGPoint points = [[touchloc valueForKey:@"location"] pointValue];