intersectsNode不工作?

时间:2015-06-24 19:33:45

标签: sprite-kit

我正在尝试检测一个节点何时与另一个节点重叠。我正在教自己sprite kit,并且无法获得intersectsNodes来提供任何输出。我已经加载了2个可以在屏幕上移动的节点,然后在更新部分添加了intersectsNodes。

-(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

1 个答案:

答案 0 :(得分:0)

首先,您需要为每个节点设置一个位置:

playerShip1.position = CGPointMake(0,0);

其次,您需要将每个节点添加到视图中:

[self addChild:playerShip1];

此外,您正在更新方法中添加节点,这是错误的位置。正如您的代码现在一样,您将每秒连续添加60个节点。

我看到你添加了额外的代码。您正在init方法中创建3个SKSpriteNodes,这很好并且有效。但是,您没有保留对这3个节点的引用。这意味着一旦创建它们,您就无法再次访问它们。要解决此问题,您应该创建3个SKSpriteNode类属性或将所有3个节点添加到数组中(数组的类属性)。如果你不知道我的意思,那没关系,只是意味着你有办法学习。

我建议你做一些教程,因为这将教会一些额外的基础知识,并防止将来大量的“为什么这不起作用”的挫折。