action does not wait till function call ends












2















I have 2 actions that i put in a sequence. In the first action I am calling a method to calculate the new waiting time for the next action. The next action is just a wait for this duration, but the second action always executes straight away, so the time must be 0. I debugged it and in the method spawnFlowers i get the time returned as 3.5 seconds.



these are my 2 actions



let spawnFlowerAction = SKAction.run {
self.WaitTime = self.calculateWaitingTime()
}
let waitForNewFlower = SKAction.wait(forDuration: self.WaitTime)


I execute it this way:



        let spawnSeq = SKAction.sequence([spawnFlowerAction, waitForNewFlower])
let spawnRepeat = SKAction.repeat(spawnSeq, count: 4)
self.run(spawnRepeat)


Result: 4 times spawned without waiting, printing 4 different calculated times in the console from the calculateWaitingTime function (in which the spawning happens)



What is a good way to fix this?










share|improve this question





























    2















    I have 2 actions that i put in a sequence. In the first action I am calling a method to calculate the new waiting time for the next action. The next action is just a wait for this duration, but the second action always executes straight away, so the time must be 0. I debugged it and in the method spawnFlowers i get the time returned as 3.5 seconds.



    these are my 2 actions



    let spawnFlowerAction = SKAction.run {
    self.WaitTime = self.calculateWaitingTime()
    }
    let waitForNewFlower = SKAction.wait(forDuration: self.WaitTime)


    I execute it this way:



            let spawnSeq = SKAction.sequence([spawnFlowerAction, waitForNewFlower])
    let spawnRepeat = SKAction.repeat(spawnSeq, count: 4)
    self.run(spawnRepeat)


    Result: 4 times spawned without waiting, printing 4 different calculated times in the console from the calculateWaitingTime function (in which the spawning happens)



    What is a good way to fix this?










    share|improve this question



























      2












      2








      2








      I have 2 actions that i put in a sequence. In the first action I am calling a method to calculate the new waiting time for the next action. The next action is just a wait for this duration, but the second action always executes straight away, so the time must be 0. I debugged it and in the method spawnFlowers i get the time returned as 3.5 seconds.



      these are my 2 actions



      let spawnFlowerAction = SKAction.run {
      self.WaitTime = self.calculateWaitingTime()
      }
      let waitForNewFlower = SKAction.wait(forDuration: self.WaitTime)


      I execute it this way:



              let spawnSeq = SKAction.sequence([spawnFlowerAction, waitForNewFlower])
      let spawnRepeat = SKAction.repeat(spawnSeq, count: 4)
      self.run(spawnRepeat)


      Result: 4 times spawned without waiting, printing 4 different calculated times in the console from the calculateWaitingTime function (in which the spawning happens)



      What is a good way to fix this?










      share|improve this question
















      I have 2 actions that i put in a sequence. In the first action I am calling a method to calculate the new waiting time for the next action. The next action is just a wait for this duration, but the second action always executes straight away, so the time must be 0. I debugged it and in the method spawnFlowers i get the time returned as 3.5 seconds.



      these are my 2 actions



      let spawnFlowerAction = SKAction.run {
      self.WaitTime = self.calculateWaitingTime()
      }
      let waitForNewFlower = SKAction.wait(forDuration: self.WaitTime)


      I execute it this way:



              let spawnSeq = SKAction.sequence([spawnFlowerAction, waitForNewFlower])
      let spawnRepeat = SKAction.repeat(spawnSeq, count: 4)
      self.run(spawnRepeat)


      Result: 4 times spawned without waiting, printing 4 different calculated times in the console from the calculateWaitingTime function (in which the spawning happens)



      What is a good way to fix this?







      sprite-kit skaction






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 14:08







      Beginner

















      asked Nov 23 '18 at 13:49









      BeginnerBeginner

      408




      408
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The problem is trying to dynamically change the values used within SKActions after the action has been created. For example when your WaitTime variable changes while running the spawnFlowerAction, the waitForNewFlower Action's wait time won't change dynamically because it doesn't reference WaitTime. Instead its wait value is whatever your variable WaitTime was when you declared let waitForNewFlower = SKAction.wait(forDuration: self.WaitTime) (Which I'm guessing was initially 0). Same concept goes with your other two spawn actions.



          I usually use the dispatch Queue for things like these, but to use SKActions here's a function. Just call it once and input the number of times you want it to repeat.



          func spawnRepeat(count: Int) {

          //Put whatever code to spawn flower here
          print("SPAWN FLOWER")

          if count > 1 {
          //Recalculate WaitTime
          WaitTime = calculateWaitingTime()
          let waitAction = SKAction.wait(forDuration: WaitTime)
          run(waitAction, completion: { self.spawnRepeat(count: count - 1) })
          }
          }





          share|improve this answer



















          • 1





            i will try this tomorrow and mark your answer if its working thank you lots :)

            – Beginner
            Nov 24 '18 at 22:41











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447926%2faction-does-not-wait-till-function-call-ends%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The problem is trying to dynamically change the values used within SKActions after the action has been created. For example when your WaitTime variable changes while running the spawnFlowerAction, the waitForNewFlower Action's wait time won't change dynamically because it doesn't reference WaitTime. Instead its wait value is whatever your variable WaitTime was when you declared let waitForNewFlower = SKAction.wait(forDuration: self.WaitTime) (Which I'm guessing was initially 0). Same concept goes with your other two spawn actions.



          I usually use the dispatch Queue for things like these, but to use SKActions here's a function. Just call it once and input the number of times you want it to repeat.



          func spawnRepeat(count: Int) {

          //Put whatever code to spawn flower here
          print("SPAWN FLOWER")

          if count > 1 {
          //Recalculate WaitTime
          WaitTime = calculateWaitingTime()
          let waitAction = SKAction.wait(forDuration: WaitTime)
          run(waitAction, completion: { self.spawnRepeat(count: count - 1) })
          }
          }





          share|improve this answer



















          • 1





            i will try this tomorrow and mark your answer if its working thank you lots :)

            – Beginner
            Nov 24 '18 at 22:41
















          1














          The problem is trying to dynamically change the values used within SKActions after the action has been created. For example when your WaitTime variable changes while running the spawnFlowerAction, the waitForNewFlower Action's wait time won't change dynamically because it doesn't reference WaitTime. Instead its wait value is whatever your variable WaitTime was when you declared let waitForNewFlower = SKAction.wait(forDuration: self.WaitTime) (Which I'm guessing was initially 0). Same concept goes with your other two spawn actions.



          I usually use the dispatch Queue for things like these, but to use SKActions here's a function. Just call it once and input the number of times you want it to repeat.



          func spawnRepeat(count: Int) {

          //Put whatever code to spawn flower here
          print("SPAWN FLOWER")

          if count > 1 {
          //Recalculate WaitTime
          WaitTime = calculateWaitingTime()
          let waitAction = SKAction.wait(forDuration: WaitTime)
          run(waitAction, completion: { self.spawnRepeat(count: count - 1) })
          }
          }





          share|improve this answer



















          • 1





            i will try this tomorrow and mark your answer if its working thank you lots :)

            – Beginner
            Nov 24 '18 at 22:41














          1












          1








          1







          The problem is trying to dynamically change the values used within SKActions after the action has been created. For example when your WaitTime variable changes while running the spawnFlowerAction, the waitForNewFlower Action's wait time won't change dynamically because it doesn't reference WaitTime. Instead its wait value is whatever your variable WaitTime was when you declared let waitForNewFlower = SKAction.wait(forDuration: self.WaitTime) (Which I'm guessing was initially 0). Same concept goes with your other two spawn actions.



          I usually use the dispatch Queue for things like these, but to use SKActions here's a function. Just call it once and input the number of times you want it to repeat.



          func spawnRepeat(count: Int) {

          //Put whatever code to spawn flower here
          print("SPAWN FLOWER")

          if count > 1 {
          //Recalculate WaitTime
          WaitTime = calculateWaitingTime()
          let waitAction = SKAction.wait(forDuration: WaitTime)
          run(waitAction, completion: { self.spawnRepeat(count: count - 1) })
          }
          }





          share|improve this answer













          The problem is trying to dynamically change the values used within SKActions after the action has been created. For example when your WaitTime variable changes while running the spawnFlowerAction, the waitForNewFlower Action's wait time won't change dynamically because it doesn't reference WaitTime. Instead its wait value is whatever your variable WaitTime was when you declared let waitForNewFlower = SKAction.wait(forDuration: self.WaitTime) (Which I'm guessing was initially 0). Same concept goes with your other two spawn actions.



          I usually use the dispatch Queue for things like these, but to use SKActions here's a function. Just call it once and input the number of times you want it to repeat.



          func spawnRepeat(count: Int) {

          //Put whatever code to spawn flower here
          print("SPAWN FLOWER")

          if count > 1 {
          //Recalculate WaitTime
          WaitTime = calculateWaitingTime()
          let waitAction = SKAction.wait(forDuration: WaitTime)
          run(waitAction, completion: { self.spawnRepeat(count: count - 1) })
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 8:48









          Muffinman2497Muffinman2497

          26819




          26819








          • 1





            i will try this tomorrow and mark your answer if its working thank you lots :)

            – Beginner
            Nov 24 '18 at 22:41














          • 1





            i will try this tomorrow and mark your answer if its working thank you lots :)

            – Beginner
            Nov 24 '18 at 22:41








          1




          1





          i will try this tomorrow and mark your answer if its working thank you lots :)

          – Beginner
          Nov 24 '18 at 22:41





          i will try this tomorrow and mark your answer if its working thank you lots :)

          – Beginner
          Nov 24 '18 at 22:41


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447926%2faction-does-not-wait-till-function-call-ends%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Sphinx de Gizeh

          Dijon

          Langue