-1

I am new to StackOverflow and my first question here is, how do I collide with objects in a game, removed them from scene and add 1 to the score .

To help you understand this question more here is an example: If the player collides with a coin, the coin will get removed from the scene and it will add 1 to the score

The only code I have is generating the diamonds and that't but I am not sure how to get round this question, I think I need to write the code in beginContact or something similar. I would be really glad if someone could help me with this issue. Thanks!

Swift101
  • 81
  • 10

1 Answers1

1

Object collision can be done in several ways depending on the complexity of the game.

  1. The simplest method is to track the entire field in a 2d or 3d matrix and if the user moves into the same coordinates as an obtainable object remove the object and increment the score. This has obvious issues when it comes to large maps or complex systems that would run the hardware out of memory. So this works for something like a chess/checkers board but not a driving simulation.

  2. The second method is to keep a linked list of objects visible on the field with it's central coordinates and it drawing directions. The objects might look like coords (1007.2053, 489.2111) shape (box). Where box is a function that generates all the border coordinates. Then detect collisions by checking if primary target overlaps any of the object in the list. You'll probably have to write a collision function for each shape. The simpler the shape the easier the collision function. For more complex object it's often easier just to simplify the shape to a box no mater what it looks like. This is why 3d games often have clipping errors and why you could shoot the edge of an object in a fps and still not be considered to hit it.

Your question is too broad for a better answer but Here is a very basic article that discusses the second method.

More Info. The game is apparently a 2d endless runner.

So you could set up a 2d matrix that acts as a queue. Say your board is 3 high so your character can walk, jump, or high jump.


highjump

jump

walk

This would be a simple matrix like board 10 X 3 (x, y) With each frame the board removes the front column and adds a new column to the back. Think front of matix is left side, back is right side. When adding to back you randomly decide which box to put the coin in. In each frame the user must be in one of the 3 positions. if his position is the same as the object collect it and gain points. Or if the object is undesirable lose a life and start game over, etc.

more links

Information specific to endless runners here and another stack overflow question similar to yours here

Community
  • 1
  • 1
danielson317
  • 3,121
  • 3
  • 28
  • 43
  • my game is a 2d endless runner, with the character jumping up and down to collect coins – Swift101 Jun 24 '15 at 22:15
  • @Swift101 I updated my answer to go into the matrix idea further. – danielson317 Jun 24 '15 at 22:32
  • That's a helpful answer I will use in the future, but the thing I need to know is how do I make my player pick up the coin. I got the coin generating and my hero working and stuff like that, I just need help with the collision. – Swift101 Jun 24 '15 at 22:44
  • Your going to have to give more info on your architecture. You "pick up a coin" by removing it from your view and taking a corresponding action on your player such as adding points. You "detect collision" by checking if the players coordinates match the coins coordinates using one of the methods I just described. – danielson317 Jun 24 '15 at 22:46
  • Ok maybe these 2 sources may help you understand my question. [Source 1](http://stackoverflow.com/questions/28881480/how-would-i-save-coins-in-my-game-in-swift-spritekit) [Source 2](http://stackoverflow.com/questions/30857900/how-do-i-increment-the-score-by-10-when-a-user-collects-a-coin-in-swift) – Swift101 Jun 24 '15 at 23:00