2012-02-11 43 views
2

我使用的Cocos2D開發一個迷你iPhone遊戲..我想檢測精靈的觸摸。要做到這一點,我決定不子類CCSprite類,而是使用層類的觸摸事件:如何訪問UITouch locationInWindow座標

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CCLOG(@"touch began..."); 
    CCSprite *particularSprite = [self getChildByTag:artSprite]; 
    CCNode *nodeClass = (CCNode*) particularSprite; 
    CGRect DesiredSprite = CGRectMake(nodeClass.positionInPixels.x, nodeClass.positionInPixels.y,particularSprite.contentSize.width , particularSprite.contentSize.height); 

    for (UITouch *myTouch in touches) { 
     CGPoint touchPosition = [myTouch locationInView: [myTouch view]]; 
     if(CGRectContainsPoint(DesiredSprite ,touchPosition)) 
     { 
      CCLOG(@"Sprite touched"); 
     } 
    } 
} 

不幸的是,座標是錯誤的。 locationInView翻譯它不同。我正在使用landscapeleft視圖(kCCDeviceOrientationLandscapeLeft)。

添加的功能,斷點,看的myTouch變量,然後我看到,它有一個名爲locationInWindow成員變量,它反映了實際觸摸位置(這是我想要的)。

我試圖訪問到locationInWindow但它沒有getter方法。我該怎麼做?

非常感謝和誠摯的問候

回答

5

窗口是一個UIWindow這是一個UIView子類。此外UITouchwindow屬性。所以你可以嘗試:

CGPoint touchPosition = [myTouch locationInView:myTouch.window]; 

視圖的變換數字到計算中;所以你可能也想嘗試self.superview作爲參數。

+0

您好,感謝,這也幫助..我不得不轉換位置交換X到Y的景觀視野的..想知道是否有一個自動化的方式來做到這一點。我沒有使用boundingBox的矩形比較針對如如先前創建完成一個矩形不是精確(它不與x中的原點和y作爲SPRIE創建)。謝謝 :)! – mm24 2012-02-11 18:37:04

+2

同時,文檔指出,你可以通過'nil'獲得窗口座標。 – 2013-02-12 23:13:17