2015-06-24 44 views
1

我試圖檢測何時一個節點與另一個節點重疊。我正在教自己的sprite工具包,並且無法獲取交叉節點以提供任何輸出。我已經加載了2個節點,可以在屏幕上四處移動,然後在更新部分添加intersectsNodes。intersectsNode不工作?

-(void)update:(CFTimeInterval)currentTime { 
SKSpriteNode* playerShip1; 
SKSpriteNode* playerShip2; 

if ([playerShip1 intersectsNode: playerShip2]) { 
      NSLog(@"your ships overlap"); 

    } 

有關爲什麼找不到交點的想法?

這裏是整個m文件。

#import "MyScene.h" 

@implementation MyScene 

-(id)initWithSize:(CGSize)size { 
if (self = [super initWithSize:size]) { 
     SKSpriteNode* playerShip1 = [SKSpriteNode spriteNodeWithImageNamed:@"player"]; 
     playerShip1.position = CGPointMake(self.frame.size.width/2, playerShip1.frame.size.height); 
     playerShip1.name = @"PLAYER_SHIP_1"; 
     [self addChild:playerShip1]; 

     SKSpriteNode* playerShip2 = [SKSpriteNode spriteNodeWithImageNamed:@"player"]; 
     playerShip2.position = CGPointMake(self.frame.size.width/2, self.frame.size.height-playerShip2.frame.size.height); 
     playerShip2.name = @"PLAYER_SHIP_2"; 
     [self addChild:playerShip2]; 


     SKSpriteNode* playerShip3 = [SKSpriteNode spriteNodeWithImageNamed:@"player"]; 
     playerShip3.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); 
     playerShip3.name = @"PLAYER_SHIP_3"; 
     [self addChild:playerShip3]; 

     activeDragNode = nil; 
} 
    return self; 
} 

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    // Identify where this touch is on the scene 
    CGPoint scenePosition = [touch locationInNode:self]; 
    // Find the child node that contains this touch 
    SKNode* checkNode = [self nodeAtPoint:scenePosition]; 
    // Make sure it is a player ship and not another node like the parent SKScene 
    if (checkNode && [checkNode.name hasPrefix:@"PLAYER_SHIP"]) { 
     activeDragNode = (SKSpriteNode*)checkNode; 
     SKAction *zoomAction = [SKAction scaleTo:1.5 duration:.2]; 
     [checkNode runAction:zoomAction]; 
     //NSLog(@"your ship has been hit!"); 

    } 
} 

    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    // Remove reference to which object we are dragging 
    activeDragNode=nil; 
    UITouch *touch = [touches anyObject]; 
    CGPoint scenePosition = [touch locationInNode:self]; 
    SKNode* checkNode = [self nodeAtPoint:scenePosition]; 
    SKAction *shrinkAction = [SKAction scaleTo:1.0 duration:.2]; 
    [checkNode runAction:shrinkAction]; 

    } 
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    // Be sure we have a reference to an object to drag 
    if (activeDragNode==nil) return; 
    UITouch *touch = [touches anyObject]; 
    CGPoint scenePosition = [touch locationInNode:self]; 
    CGPoint lastPosition = [touch previousLocationInNode:self]; 


    // Calculate the new location of the dragged node 
    CGPoint newLoc = CGPointMake(activeDragNode.position.x + (scenePosition.x  - lastPosition.x), activeDragNode.position.y + (scenePosition.y -  lastPosition.y)); 
    // Update this location 
     activeDragNode.position = newLoc; 
    } 
    -(void)update:(CFTimeInterval)currentTime { 
    SKSpriteNode* playerShip1; 
    SKSpriteNode* playerShip2; 

    if ([playerShip1 intersectsNode: playerShip2]) { 
      NSLog(@"your ships overlap"); 

    } 
     /* Called before each frame is rendered */ 
    } 

    @end 

回答

0

首先,你需要爲每個節點的位置:你需要在每個節點添加到視圖

playerShip1.position = CGPointMake(0,0); 

二:

[self addChild:playerShip1]; 

此外,要添加的節點在你的更新方法中是錯誤的地方。當你的代碼現在,你會連續添加每秒60次的節點。


我看到你添加額外的代碼。你在你的init方法中創建了3個SKSpriteNodes,這很好,並且可以工作。但是,您並未保留對這3個節點的引用。這意味着一旦創建它們,您就無法再訪問它們。爲了解決這個問題,你應該創建3個SKSpriteNode類屬性或者將所有3個節點添加到一個數組中(數組的類屬性)。如果你不知道我的意思,沒關係,這只是說你有一種學習方式。

我建議你做幾個教程,因爲這將教授一些額外的基礎知識,並防止未來大量的「爲什麼不工作」的挫折。

+0

嗨,我在開始時添加節點。我再次給他們打電話的唯一原因是我得到了「未聲明的標識符」錯誤。我將添加整個m文件。它基於我正在添加的一個教程。 – Swick

+0

也謝謝你看我的問題。 – Swick

+0

@Swick - 更新 – sangony