0

I'm programming a little Game. For this I need some Walls. Therefor I have used:

    Wall[w] = [[SKShapeNode alloc] init];
    Wall[w].path = WallPos[w];
    Wall[w].lineWidth = 4;
    Wall[w].strokeColor = [UIColor whiteColor];
    Wall[w].zPosition = 3;

    [self addChild: Wall[w]];

Wall is an Array of SKShapeNodes and is set in @interface, so I can use it in every method. WallPos contains CGMutablePathRefs. In TouchesBegan and TouchesMoved I'm calling a method which should check if you have touched one of the walls. I have also some SKShapeNodes which are Rectangles, and to check if they are touched, I have used

if ([SomeShape containsPoint: Position] {
//Do some stuff
}

But with a line it's not working. Sometimes I'm touching on the line and nothing happens. Then I've seen this: Detecting Touch on SKShapeNode that is a Line and I have tried to do it on that way:

    for (int i = 0; i < NrWalls; i++) {
       if (CGRectContainsPoint(Wall[i].frame, Position)) {
           [self GameOver];
       }
    }

But now every Point I touch sets a "Game Over" to me!!

Has anyone an Idea, how could I check if the line is touched?

Thanks for your help!

DXXL

Community
  • 1
  • 1

1 Answers1

1

Not sure why you want to use SKShapeNodes for walls and rectangles. To answer your question, you can attach a physics body to your shape node and use the contact methods to check for possible contacts. However, assigning a physics body to a shape node could be a tricky undertaking due to the anchor points and getting a desired alignment.

Seeing that you are really only drawing rectangles for your walls, I suggest you use a SKSpriteNode with a physics body instead. Something like this:

SKSpriteNode *myNode = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(5, 100)];
myNode.position = CGPointMake(100, 100);
myNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:myNode.size];
myNode.physicsBody.dynamic = NO;
myNode.physicsBody.categoryBitMask = CategoryWall;
[self addObject:myNode];

If you need, you can read up on SKPhysicsBody here.

sangony
  • 11,636
  • 4
  • 39
  • 55