1

I used

SKAction.customActionWithDuration(1, actionBlock: { (node: SKNode!, elapsedTime: CGFloat) -> Void in print("Hello")})

and it only ran for 5 times in 1 second. When I switched to 0.5 seconds it only ran for 3 times.

I want to run it for around 100-200 times in 3-4 seconds.

Any Solution ??

Isabel Inc
  • 1,871
  • 2
  • 21
  • 28

3 Answers3

1

customActionWithDuration gets updated every frame, if it is running 5 times in 1 second, that means your updates are happening at 200ms, you have other issues going on. Ideally you want to get it to call every 16.6ms, or 60 frames per second, so investigate your update phase, and find out what is causing slowdown. If you are running on simulator, make sure the simulator is running at 60fps.

If you need something consistent, then customActionWithDuration is not for you.

You may need to use repeatAction(action:,count:) to achieve what you want.

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
  • Yeah you are right, I have aroung 20-25 fps...and I think its because of something called debug and release schemes...I want 60 fps, what should I implement? – Ayush Rajput Jul 19 '16 at 17:34
0

I'm not sure what the limitations of an SKAction's speeds are but you could try this:

let waitAction = SKAction.waitForDuration(0.03) //0.03 is 100 times in 3 seconds
let blockAction = SKAction.runBlock({(node: SKNode!, elapsedTime: CGFloat) -> Void in print("Hello")}) 

let actionSequence = SKAction.sequence([waitAction, blockAction])

self.runAction(actionsSequence)
claassenApps
  • 1,137
  • 7
  • 14
  • Thanx for your answer @claassenApps, but my main concern is regarding the customaction. Please if u can try to answer this – Ayush Rajput Jul 19 '16 at 16:25
0

According with the sources:

/** Creates an action that executes a block over a duration
     @param duration The duration of the animation
     @param actionBlock The block to run. The block takes the following parameters:
     node The node on which the action is running.
     elapsedTime The amount of time that has passed in the animation.
     */
    public class func customActionWithDuration(seconds: NSTimeInterval, actionBlock block: (SKNode, CGFloat) -> Void) -> SKAction

You can try to do:

let duration:NSTimeInterval = 3.0
let action = SKAction.customActionWithDuration(duration, actionBlock: { (node: SKNode!, elapsedTime: CGFloat) -> Void in
    let t = Int((elapsedTime/CGFloat(duration))*100)
    print("\(t)hello")
})
self.runAction(action)

Output:

enter image description here

the counter continue until reach 100

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • no, that wont do it, you are only printing a percentage of time passed, and because you set it to 3, that is why you are reaching 100% at a interval of 1 (At some point you should see the same number printed twice) – Knight0fDragon Jul 19 '16 at 17:38
  • A little imprecision is an acceptable factor as well as the same customActionWithDuration method does not guarantee accuracy.OP don't required an exact number, not to mention all the factors that could slow down time. – Alessandro Ornano Jul 19 '16 at 17:49
  • Yes, but you do not provide an answer, what if the device runs at 1fps – Knight0fDragon Jul 19 '16 at 17:51