0

I have code which generates random background images, but for some reason when the code is called my game glitches for a split of a second, which is very noticeable. After hours of commenting out my code, I've narrowed it down to one of my random number generates. I have two random number generators, one that generates a number between 0 and 2 and one that generates a number between 0 and 162. The thing that has me stumped is, the generator which generates a number between 0 and 2 is the one creating the glitch???

Here is my code below, can anybody see something I'm missing?

    randomLandscape = CGFloat(arc4random_uniform(UInt32(3)))
    randomDistanceLandscape = CGFloat(arc4random_uniform(UInt32(sizePreviousLandscapeWidth2)))

    nodeLandscapeA = SKSpriteNode(imageNamed: "A\(Int(countWorld))2\(Int(randomLandscape))")
    nodeLandscapeA.name = "landscapeA"
    nodeLandscapeA.zPosition = CGFloat(arrayLandscapeZPosition[Int(countWorld)] + 3)
    nodeLandscapeA.position.y = nodeLandscapeA.size.height/2
    nodeLandscapeA.position.x = positionPreviousLandscapeX2 + sizePreviousLandscapeWidth2/2 + nodeLandscapeA.size.width/2 + randomDistanceLandscape

    nodeLandscapeB = SKSpriteNode(imageNamed: "B\(Int(countWorld))2\(Int(randomLandscape))")
    nodeLandscapeB.name = "landscapeB"
    nodeLandscapeB.zPosition = CGFloat(arrayLandscapeZPosition[Int(countWorld)] + 2)
    nodeLandscapeB.position.y = nodeLandscapeA.position.y
    nodeLandscapeB.position.x = nodeLandscapeA.position.x

Note, if I enter randomLandscape = 1, the glitch disappears.

sangony
  • 11,636
  • 4
  • 39
  • 55
Jarron
  • 1,049
  • 2
  • 13
  • 29
  • See http://stackoverflow.com/questions/2794201/whats-the-difference-between-arc4random-and-random, especially the comment about comparative performance. – Phillip Mills Jun 12 '15 at 18:11
  • What happens if you do randomLandscape=0 or randomLandscape=2? What happens if you call it repeatedly? Does it only have a delay the first time arc4random_uniform is called? – KirkSpaziani Jun 12 '15 at 18:17
  • @PhillipMills trying to find information on the net to get random() to work, but have't really found much, everything so far is for arc4random. I'll keep looking and hopefully I can find something. From what I've found on the net however, the random() create predicable numbers, which I can work with in this instance. – Jarron Jun 12 '15 at 18:36
  • @KirkSpaziani when randomLandscape = 0 or 2, it runs with no glitch. When used repeatedly, I do notice some glitches, but some are not present. Can't tell if there is a delay the first time it is called, but the second time (about 10 seconds in), is when I see the glitch. The code is only called two times at the moment in my code, but will increase – Jarron Jun 12 '15 at 18:39
  • There is a discussion here (http://stackoverflow.com/questions/23685920/performance-of-concurrent-code-using-dispatch-group-async-is-much-slower-than-si) on arc4random_uniform performance issues. – picciano Jun 12 '15 at 19:37
  • What kind of "Glitch" is this? I've never had any problems with arc4random_uniform, maybe the problem lies elsewhere... – Jake Crastly Jun 13 '15 at 02:47
  • @picciano thanks, for the link, they mention something about the random generate not being able to keep up with the rest of the code, and as a result, the random number generate would slow the rest of the code down. I'm thinking this might be the case with my code, because has previously mentioned, once the randomLandscape is assigned a specific value, the code run fine. – Jarron Jun 13 '15 at 05:47
  • @hamecanecha I have several moving nodes on my app, when the above code is ran, all the nodes stop for a split of a second and then resume. I was hoping that it wasn't the random number generator causing the problem, because I can't really get around that, but after testing and commenting out code, the random number generator was the only thing creating the glitch. I'm still looking over and over my code to see if there is something I'm missing, to try and rule out the random number generator, but have not found anything else that is effecting the performance. – Jarron Jun 13 '15 at 05:51
  • 1
    The delay is likely due to loading the image not the random number generator. Try preloading the images, as `SKTextures`, into an array and access the textures via a random index value. – 0x141E Jun 13 '15 at 09:38
  • @0x141E thanks for this. I changed my code so that the images generated at 1, 2, 3 and then back to 1 again (not using the random generate), and my program still glitched. So, I think your right in saying that its the image loading which is causing the problem. Also, when the same image was used to populate all landscape images, the glitch disappeared, which make sense, because it is not having to reload images. I need to amend a lot of my code to see if using textures works, but I think it will. Thanks – Jarron Jun 13 '15 at 13:58
  • @0x141E just finished implementing textures into all of my code. This has completely removed the glitch!!! Thanks a lot for your advice! – Jarron Jun 13 '15 at 14:37

0 Answers0