1
\$\begingroup\$

Scenario: I have a game with several game objects on screen. Each can have different score and hitpoints. When I use collision detection for cannonball vs. ship (example) I need to know which ship is hit and how much hitpoints it has.

Problem: I had several approaches from extending SKSpriteNode or SKPhysicsBody but all failed. SKSpriteNode doesn't work, because Swifts collision detection only returns physicsBody and since you can't extend physicsBody, it didn't work to save there ships attributes as well.

Question: What would be a good approach to save such data to my game objects? I do not need code - because I think I can write it myself - but I would be happy to read a good suggestion. But if you have already some code written and a good solution for this problem, I would not be angry to read it ;)

\$\endgroup\$
  • \$\begingroup\$ Because I try to avoid change core classes. And I am not in a hurry - so I might find a better solution. Otherwise this would be a good way thought. But I have still hope that there is a more 'common' solution in Swift for my problem. How do all the other game developer it? \$\endgroup\$ – Jurik Aug 5 '15 at 23:34
  • \$\begingroup\$ From SKPhysicsBody.h it seems that there is a reference to SKNode, how come that didn't work for you ? This answer definitely seems correct. I suppose you have to cast the physicsBody.node instance to Orange (as long as Orange extends SKNode). \$\endgroup\$ – dimitris93 Aug 6 '15 at 0:04
  • \$\begingroup\$ Hmm... physicsBody is attached to SKSpriteNode.physicsBody. But physicsBody returns just SKNode as parent tought. \$\endgroup\$ – Jurik Aug 6 '15 at 7:00
  • \$\begingroup\$ You need to type cast it. \$\endgroup\$ – dimitris93 Aug 6 '15 at 12:30
1
\$\begingroup\$

Use the SKPhysicsBody instances as keys to a dictionary of game objects. When you want to process a collision, retrieve the game object associated with the physics body from the dictionary. Not as efficient as the direct link you sought, but more than sufficient for most purposes.

\$\endgroup\$
-1
\$\begingroup\$

Try Using

NSUserDefaults it has the full CRUD functionality and you can save each ship type with their specific hit points and cannonball

More description about NSUserDefaults is Here

\$\endgroup\$

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.