Skip to main content
Inspiring
July 15, 2023
Question

What is the best way to create a delay unit a MC has finished playing?

  • July 15, 2023
  • 3 replies
  • 369 views

Hi team,

Just creating an example for school students and want to use the most sensible (industry appropriate) way to make this work.

I have a two separate MC drawings of a hand and a coin.  Also a button on the stage.  When you click the button the hand animates (the thumb extends), a sound effect plays and the coin flips- this happens over 55 frames.

so far so good - Now I can code the random number between 0-1 and the if/then-->else condition to work out if the toss is a heads or tails (and there will be a graphic that will become visible to show the result.  My question is - what is the most appropriate way to time the appearance of the result to match the 55 frame lead in?  Do you create a ticker and try to get the timing right or is there some other type of event listener I can use to send a message from the end of the MC (i.e - when it gets to the end of its 55 frame run) to say "do this" and trigger a function on the main timeline?

Very interested to hear how you guys would make this work (And also why that is the best idea)

Thanks for your help

have a great day

    This topic has been closed for replies.

    3 replies

    kglad
    Community Expert
    Community Expert
    July 16, 2023

    use a function call on the 55th frame to determine the coin flip result and what to display.

    Inspiring
    July 19, 2023

    Hey Kglad,

    Would  this work if that 55th frame is inside a MC on the main stage? (there in only 1 frame on the main timeline)- also isnt it bad form to have bits of code scattered through your project? Should you try to keep it all contained (As much as possible) in the one place?

    Thanks for the pointers and have a great day

    kglad
    Community Expert
    Community Expert
    July 19, 2023

    it is more desirable to have code in one location but with comments it's acceptable to spread some code.

     

    and yes, the movieclip (from any frame) can call a parent function.

    kglad
    Community Expert
    Community Expert
    July 15, 2023

    @Bundaberg North5E42 

     

    do you still have a question?

    Inspiring
    July 15, 2023

    Well I am happy that this works but as always would love the breakdown of why it works and is this the best way to acheive this outcome.

    Thanks for your time and interest 🙂

    Inspiring
    July 15, 2023

    Oh I forgot to mention we are using the html5 canvas

    I did think of a dumb way to do this- I could make a heads MC and a tails MC and each of these could have a 55 frame blank at the front- so the result happens instantaneously but the result takes 55 frames to show up?

    too caveman?

    Inspiring
    July 15, 2023

    No I really do need a better way to do this because it is clear that I should be using a dynamic text box at some point to show "Heads" or "Tails" - (I can make it show the answer immediately but its the same problem as the graphics) so would this be done with a timer? 55 frames is about 2 sec?

    Inspiring
    July 15, 2023
    var _this = this;
    var randomNumber = Math.floor(Math.random() * 1) + 1;
    var secTimer = 0;
    var sec = 4;
    var oText = "have a go";
    _this.outcomeText.text = oText;
    
    function chooseNumber() {
    	randomNumber = Math.floor(Math.random() * 2) + 1;
    }
    
    function changeTextAfterDelay() {
    	setTimeout(function() {
    		if (randomNumber === 1) {
    			oText = "heads";
    		} else if (randomNumber === 2) {
    			oText = "tails";
    		}
    		_this.outcomeText.text = oText;
    	}, 1800); // 2000 milliseconds = 2 seconds
    }
    
    function checkNumber() {
    	_this.heads.gotoAndStop(0);
    	_this.tails.gotoAndStop(0);
    	if (randomNumber === 1) {
    		_this.heads.play();
    	} else if (randomNumber === 2) {
    		_this.tails.play();
    	}
    }
    
    chooseNumber();
    
    _this.goButt.on('click', function() {
    	oText = "wait for it";
    	_this.outcomeText.text = oText;
    	chooseNumber();
    	checkNumber();
    	_this.coinToss.play();
    	_this.coinO.play();
    	changeTextAfterDelay();
    });