0
\$\begingroup\$

How can I set the radius or the hit range of a node initialized with a texture? Of course I can do that with increasing the size of it, but actually I want to do this without resizing the picture itself. This node is moving around the screen with a small speed. This is my code:

func addMonster() {

let monster = SKSpriteNode(imageNamed: "MosquitoDrawed")

monster.name = predatorNodeName

monster.zPosition = 1

// monster.physicsBody = SKPhysicsBody(circleOfRadius: 50.0)

monster.position = CGPoint(x: random(-10.0,max: -30.0), y: random(10.0, max:130.0))

addChild(monster)

let actualDuration = 7.0

let actionMoveTo = SKAction.moveTo(CGPoint(x: random(500, max:800),y: random(1, max:800) ), duration: NSTimeInterval(actualDuration))

monster.runAction(actionMoveTo,withKey: "move")

}
\$\endgroup\$
1
\$\begingroup\$

Have you tried to initialize your SKPhysicsBody with a rectangle of the size you want?

var node = SKSpriteNode(imageNamed: "node.png")
node.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 50, height: 50))

That way you can increase the size of the bounding box of your node without using a larger image.

More details in the docs.

Hope it helps!

\$\endgroup\$
  • \$\begingroup\$ Great ! I did change the PhysicsBody , but the hit range is still not affected. Also there's another bug that appeared after after this, the node goes up and down fast while moving. \$\endgroup\$ – xdevx32 Mar 9 '16 at 16:27
  • \$\begingroup\$ Without seeing any code, it's a bit hard to help you. You should add it to your question ;) \$\endgroup\$ – Heyfara Mar 9 '16 at 16:31
  • \$\begingroup\$ Yes, you're right. I just updated my post. \$\endgroup\$ – xdevx32 Mar 9 '16 at 16:33
  • \$\begingroup\$ You are talking about "hit range", but I can't see collision detection in your code. How did you test that the "hit range" was not affected? I suggest you to read the [docs about simulating physics](You should read [the docs] (developer.apple.com/library/ios/documentation/GraphicsAnimation/…) about simulating physics. ). It should give you pointers on what's missing. \$\endgroup\$ – Heyfara Mar 9 '16 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.