2014-07-01 22 views
0

我有兩個spriteNodes相交,但在不同的位置。兩個精靈都是長方形的。我得到一些奇怪的行爲,它只檢測節點之間的交集,如果它們相交於其中一個節點的左半部分(該節點始終是相同的並且始終是固定節點)。那麼會導致這種行爲的是什麼?intersectsNode沒有正確的工作只有一半

我確實有一些我可以顯示的代碼,但它看起來非常直截了當,所以如果有人認爲它可以幫助只是讓我知道?

更新:它用不同的精靈在這裏工作是爲他們每個人的代碼:

if (!weldRUHit && [weldRU intersectsNode:[barriers objectAtIndex:i]]) { 

    SKSpriteNode *nodeA = (SKSpriteNode*)[barriers objectAtIndex:i]; 
    SKPhysicsBody *firstBody = nodeA.physicsBody; 
    SKSpriteNode *nodeB = weldRU; 
    SKPhysicsBody *secondBody = nodeB.physicsBody; 

    weldRUHit = YES; 
    [self beginIntersectionWithBodyA:firstBody bodyB:secondBody]; 
} 
    if (!weldLDHit && [weldLD intersectsNode:[barriers objectAtIndex:i]]) { 

    SKSpriteNode *nodeA = (SKSpriteNode*)[barriers objectAtIndex:i]; 
    SKPhysicsBody *firstBody = nodeA.physicsBody; 
    SKSpriteNode *nodeB = weldLD; 
    SKPhysicsBody *secondBody = nodeB.physicsBody; 

    weldLDHit = YES; 
    [self beginIntersectionWithBodyA:firstBody bodyB:secondBody]; 
} 

它適用於weldLD而不是weldRU。

+0

你改變了anchorPoint嗎? – LearnCocos2D

+0

@ LearnCocos2D沒有錨點已被更改。也看到我的更新。 – user3794885

回答

0

我遇到了與SKNode的intersectsNode:方法相同的問題。我認爲它與錨點和兩個不是同一父節點的子節點的組合有關。 frame屬性用於檢測節點交集,並且Apple將SKNode的框架屬性定義爲「父級的座標系中的矩形...」。因此,對於不同的父級,您有不同的座標系。無論哪種方式,我的解決方案是使用此功能。

bool CGRectIntersectsRect (
    CGRect rect1, 
    CGRect rect2 
); 

而只是確保你有正確的座標系統。提示:使用SKNode的convertPoint:FromNode:方法。