Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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();
});
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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 🙂
Copy link to clipboard
Copied
use a function call on the 55th frame to determine the coin flip result and what to display.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Ok thanks that is an interesting way to solve the problem- in the end I edited my two movieclips and make them into single frame graphics and just made them visible true when the function that checks the random number works out if we threw a heads or tails (using the timer) it got rid of the checknumber function completely - so I think this cleaned up the code a lot, but your way would have been a little more straight forward I think.
Thank you
Copy link to clipboard
Copied
you're welcome.