Copy link to clipboard
Copied
Hello!
Here is my problem.
My main timeline has 3 frames. Each frame contains different movieClip.
I created a button, and when user clicks this button, I want a random movieclip to play.
But, I don't want to play the same movieClip again. So, when user is on the frame 1 and clicks the button, either frame 2 or 3 should play. Similarly, if the user is on the frame 2, frame 1 or 3 should play.
Now, I used this code and almost achieved what I wanted:
var _this = this;
_this.Play_button.on('click', function(){
_this.gotoAndStop(0+Math.random()*2);
});
But. Many times, the click on the button goes to the same frame, and this is not what I want.
Could someone here help me to achieve this? This is a HTML5 canvas, if it makes a difference.
Copy link to clipboard
Copied
var _this = this;
var alreadyPlayedA = [];
_this.Play_button.on('click',buttonF);
function buttonF(){
var fn = randomF();
if(alreadyPlayedA.index(fn)==-1){
alreadyPlayedA.push(fn);
_this.gotoAndStop(fn);
}
// you should use an else-branch do something to indicate button isn't going to work anymore.
}
function randomF(){
return Math.ceil(Math.random()*_this.totalFrames);
}
Copy link to clipboard
Copied
Hey, and thank you for your answer. I tried to apply your solution, but unfortunately it didn't work, or I did possibly something wrong.
Let me explain once more what is my problem, as it would be important for me to make this work.
So, I have 3 frames total.
Play_btn -layer has the following action on every frame:
var _this = this;
_this.Play_button.on('click', function(){
_this.gotoAndStop(0+Math.random()*2);
});
Spin_1 -layer has 3 movieClips, each on their own frame. In the end of the movieClips, there is an action _this.stop(); to prevent movieClip from looping.
I actually wouldn't mind if the button would play repeating frames, but the problem is, the movieClip inside those frames won't play again, because they have the stop() -action, to prevent looping. So I do not want the movieClips to loop, but with a click to a button, they can play again.
Hope this helps!
Copy link to clipboard
Copied
that's a different issue.
first, use one play button in its own layer extending across your timeline.
then use:
this.stop();
var _this = this;
_this.Play_button.on('click',buttonF);
function buttonF(){
var rn = randomF();
_this.gotoAndStop(rn);
_this['mc'+rn].gotoAndPlay(0); // <- assuming your have mc0, m1, mc2 that you want to play/replay in frames 0,1,2, resp.
}
function randomF(){
return Math.floor(Math.random()*_this.totalFrames);
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
the first frame is frame 0.
put that code in that frame.
put the only button in that frame on its own layer which extends across the entire timeline (so it exists in frames 0,1 and 2).
make sure your movieclips are mc0,mc1 an mc2 or edit the code to match your movieclip property panel names.
Copy link to clipboard
Copied
Well, I tried, still doesn't work. I added the code you provided to my actions layer, to frames 1-3. Also changed the movieClip names according to the code you provided(mc1, mc2, mc3). Now the button only plays one random clip, then nothing happens.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Randomizing between 2 movieclips cannot prevent you from having the same movieclip play several times in a row.
Random between 2 and 3 could be 2,2,2,3,3,2,3,2,3,3,3,2,2.
What you need is a toggle and the only way to have one or the other to play would be to check a variable.
var on = true;
_this.Play_button.addEventListener("click", playMC.bind(this));
function playMC(){
if(on){
this.gotoAndStop(2);
// play movieclip1 code
on = false;
}else{
this.gotoAndStop(3);
// play movieclip 2 code
on = true;
}
Copy link to clipboard
Copied
Thank you for your response! Actually I don't mind playing the same movieClip multiple times a row, sorry for not being clear in the beginning. The problem is, every movieClip has an action _stop(); in the last frame in order to prevent them looping. But, if with the button click, they would play from the beginning, that would be totally fine.
So what happens now is: User clicks the button, gets frame 1,2 or 3. User clicks the button again, and maybe gets an different frame number than on the first click, or maybe not. If not, the movieClip doesn't play again, so for user, it looks like there is "empty" clicks with nothing happening.
Copy link to clipboard
Copied
BTW action at the end of the movie should be this.stop() in html5.
Copy link to clipboard
Copied
in order to play a movieclip from the beginning you need to tell it to do so:
_this.movieClipName.gotoAndPlay(1);
because the first frame of a movieclip is number 1 and not number 0.
Copy link to clipboard
Copied
you're using code that answered a different question. since you clarified (and changed) your question, the correct code is:
this.stop();
var _this = this;
_this.Play_button.on('click',buttonF);
function buttonF(){
var rn = randomF();
_this.gotoAndStop(rn);
_this['mc'+rn].gotoAndPlay(0); // <- assuming your have mc0, m1, mc2 that you want to play/replay in frames 0,1,2, resp.
}
function randomF(){
return Math.floor(Math.random()*_this.totalFrames);
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
maybe give the frame a name and call that to be sure! Just a thought...
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
So, if you want to play random movieClips, one by one, repeating if it happens, here is what you need to do:
1. Create your movieClips, name them mc0, mc1, mc2 and so on. Give them also the same instance name, for example movieClip named mc1 should have instance name mc1. In the end of each movieClip, incloude action stop(); -so the movieClips won't loop.
2. Place these movieClips to the main stage, on same layer, but to different frames. mc0 goes to frame 1, mc1 goes to frame 2 and so on.
3. Create a button-symbol on its own layer and create a keyframe to each frame where you have also a movieClip. Name this button-symbol Play_button.
4. Place the following action/script to each keyframe of the button layer:
this.stop();
var _this = this;
_this.Play_button.on('click',buttonF);
function buttonF(){
var rn = randomF();
_this.gotoAndStop(rn);
_this['mc'+rn].gotoAndPlay(0);
}
function randomF(){
return Math.floor(Math.random()*_this.totalFrames);
}
Now it should work. Thank you for your help Kglad and Resdesign.
Copy link to clipboard
Copied