How to delay one line of code?

  • efares
    Likes 0

    Problem Description

    I have a physics sprite that I want to remove from it's parent **AFTER** I run some animation on it.
    So this is what I have:

    this.object.sprite.runAction(this.animateExplosion);
    this.object.removeFromParent();

    What is the best way to delay the removeFromParent line until the runAction animation is done running (going through all the frames) which is less than a second. Should I use sequence? Should I use setTimeout? Use something else?


  • Sonar Systems admin
    Likes 0

    You need to run a delay action and a function inside a sequence check out our Cocos API guide for more information http://cocos.sonarlearning.co.uk/docs/actions 

  • efares
    Likes 0

    I tried this

    var sequenceAction = new cc.Sequence(this.animateExplosion, this.obstacles[i].removeFromParent());
    this.obstacles[i].runAction(sequenceAction);

    but that didn’t work. I don’t think this.obstacles[i] and this.obstacles[i].sprite are the right nodeName.

    Then I tried 

    var sequenceAction = new cc.Sequence(this.animateExplosion, cc.DelayTime.create(1));
    this.obstacles[i].sprite.runAction(sequenceAction);
    this.obstacles[i].removeFromParent()

    But that didn’t work either I’m guessing it’s because the sequenceAction and removeFromParent still run at the same time or instantly after the other.


  • Sonar Systems admin
    Likes 0

    Try running it in a fresh project on a single sprite and see if it works so we can find out the route of the problem.

  • efares
    Likes 0

    I am still having this issue and I am using Cocos2D-x v3.7
    This is what I tried:

    var delay = cc.delayTime(5);
    var callFunc1 = cc.callFunc(cc.log("test1"), this);
    var callFunc2 = cc.callFunc(cc.log("test2"), this);
    
    var sequenceAction = new cc.Sequence(callFunc1, delay, callFunc2);
    this.sprite.runAction(sequenceAction);

    Both test1 and test2 are being logged at the same time. It’s like the delay is not doing anything.

  • Sonar Systems admin
    Likes 0

    but the delay time as 5000 and try that.


    This reply has been verified.
  • efares
    Likes 0

    Ya I tried putting that too but same result. Test1 & test2 log at the same time.

  • Sonar Systems admin
    Likes 0

    Have you tried other actions in sequences that take different times to see if they work?

  • efares
    Likes 0

    Awesome it works fine, I should have mentioned I was running the sequence on a physicsSprite, it works fine on a regular sprite. And also I had to use this:

    var callFunc1 = cc.callFunc(function callThis(){cc.log("test1");}, this);

    And what is the best way to make a new sprite?

    var sprite = new cc.Sprite("image file");
    // or
    var sprite = cc.Sprite.create("image file");

     

  • Sonar Systems admin
    Likes 0

    Either one is fine

  • efares
    Likes 0

    I think the best practice is to use new and not use .create anymore in the new Cocos versions.

  • Sonar Systems admin
    Likes 0

    Cool.    

Login to reply