7

In my game I want to call a function every few seconds that creates random objects from an array. Currently I'm moving this objects by SKAction like this:

func createRandmoObject() {
    let random = arc4random_uniform(4)
    randomObject = randomObjects[random]

    randomObject.runAction(SKAction.moveTo(CGPoint(x: randomObject.position.x, y: 0 - randomObject.size.height), duration: 3))

    addChild(randomObject)

}

and when the object hits the player it gets removed, then the function gets called again. The problem with this method is I found that the motion of the object isn't always smooth, when the framerate drops for some reason, stutterness occurs. Now I know how to calculate delta time in the update method, but I'm not sure how to apply it to an object when that object keeps changing and not always on the screen, and there are some other objects like it(let's think of them as enemies, and there are another function that creates another enemy) So how can I make those random objects move according to deltatime instead of SKAction?

Stefan
  • 5,203
  • 8
  • 27
  • 51
Abdou023
  • 1,654
  • 2
  • 24
  • 45
  • SpriteKit uses a variable time step for its physics. Unfortunately what this means is objects will appear to "jump" if the delta time was high. If you don't want this to happen (i.e. keep motion independent of frame rate) you will need to manually calculate the physics of nodes yourself, such as incrementing the position of your nodes in the update method. You would essentially need to write your own physics engine. – Epic Byte Nov 23 '15 at 03:32
  • I can increment the position in "update" manually, but how to do it when the node isn't always there, it takes few seconds between spawns. – Abdou023 Nov 23 '15 at 17:50
  • And sometimes new nodes get spawned when the previous ones are still there. – Abdou023 Nov 23 '15 at 18:01
  • 1
    You would need to keep a list of all active nodes and increment the position by the velocity. You would need to use a custom velocity property though because you would need to avoid using Sprite Kit Physics. Like I said you would essentially need to create your own physics engine which is not a task that would be easy depending on how many features you need such as collisions. It's really not worth it imo. Instead use the Sprite Kit Physics engine and optimize for 60fps so you won't see any "jumpy" behavior. Figure out why your game drops below 60 fps, or use a different game engine. – Epic Byte Nov 23 '15 at 18:30
  • The frame drops below 60 when there are data being loaded in the background such as ads, or when the app becomes active. – Abdou023 Nov 23 '15 at 21:44
  • 1
    I think the solution in your case is to tackle your performance issues. Try to figure out what you can do asynchronously etc. – Simon Nov 25 '15 at 11:24
  • Are you using the simulator? – Simone Pistecchia May 17 '16 at 09:38

1 Answers1

0

While you can use SKAction in spritekit projects, if your game is based on physics, you should utilize the update method for all of your sprites movements. SKactions are ok for simple games but if your game involves collisions and random movements, update method is the choice to go for.

GeneCode
  • 7,545
  • 8
  • 50
  • 85