Skip to main content
tanelij39936801
Known Participant
September 30, 2019
Question

Go to random frame, excluding current frame

  • September 30, 2019
  • 3 replies
  • 3409 views

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.

This topic has been closed for replies.

3 replies

kglad
Community Expert
Community Expert
September 30, 2019

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);
}

tanelij39936801
Known Participant
September 30, 2019
Hey, thanks again. This basically works, but for some reason it never goes to frame 0. It goes between 1 and 2, and every now and then there is "empty" click which i suppose should go to frame 0.
avid_body16B8
Legend
September 30, 2019

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;
}

tanelij39936801
Known Participant
September 30, 2019

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.

avid_body16B8
Legend
September 30, 2019

BTW action at the end of the movie should be this.stop() in html5.

 

 

kglad
Community Expert
Community Expert
September 30, 2019

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);
}

tanelij39936801
Known Participant
September 30, 2019

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!

kglad
Community Expert
Community Expert
September 30, 2019

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);
}