Mahdi Amrollahi
Mahdi Amrollahi

Reputation: 3021

How detect contact on a line in SpriteKit

i have a transparent line at top of my scene in SpriteKit . nodes can pass over it with no collision (but contact occurs) . i create a thin rectangle instead of line , but when a node contact with this rectangle the behavior of node changes completely(it is strange) . where is the problem ?

CGRect rectangle = CGRectMake(0, 0, self.frame.size.width, 2);

            SKShapeNode *yourline = [[SKShapeNode alloc] init];   



            yourline.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rectangle.size];
            yourline.fillColor =[SKColor redColor];
            yourline.physicsBody.categoryBitMask = topEdgeCategory;

            yourline.physicsBody.dynamic = YES;
            yourline.position = CGPointMake(self.frame.size.width/2,self.frame.size.height-3);
            yourline.physicsBody.friction = 0;
            yourline.physicsBody.linearDamping = 0;
            yourline.physicsBody.restitution = 1.0f;               

            [self addChild:yourline];


    self.physicsBody.categoryBitMask = edgeCategory;        

    // change gravity settings of the physics world
    self.physicsWorld.gravity = CGVectorMake(0, 0);
    self.physicsWorld.contactDelegate = self;

you can suppose that i want to create a linear laser that when a node contact to it , its health decreases . other question : how can i coloring the shape ? in my scene it is transparent , though the color is RED .

Thanks a lot

Upvotes: 1

Views: 308

Answers (1)

user4616250
user4616250

Reputation: 221

I don't have enough info to answer the first question, but I can answer the second. You need to change:

SKShapeNode *yourline = [[SKShapeNode alloc] init];  

to:

SKShapeNode *yourline = [SKShapeNode shapeNodeWithRectOfSize:rectangle.size];

Now you will be able to see the red line. The problem before was that you never laid out the size of "yourline" you only initialized it. The actual node and its physics body can be different sizes, so they must both be specified. You only stated the size of the physics body. Hope this helps!

Upvotes: 1

Related Questions