• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Go to random frame, excluding current frame

Community Beginner ,
Sep 30, 2019 Sep 30, 2019

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.

TOPICS
Code , How to , Timeline

Views

1.8K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2019 Sep 30, 2019

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 30, 2019 Sep 30, 2019

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. 

 

clipboard_image_0.png

 

Play_btn -layer has the following action on every frame:

 

clipboard_image_1.png

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. 

 

clipboard_image_2.png

 

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2019 Sep 30, 2019

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

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 30, 2019 Sep 30, 2019

Copy link to clipboard

Copied

Thank you again. Could you also tell me where I need to apply this code, on the 1st frame (actions-layer), on the button, or inside the movieClips?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2019 Sep 30, 2019

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 30, 2019 Sep 30, 2019

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.

 

clipboard_image_0.png

 

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2019 Sep 30, 2019

Copy link to clipboard

Copied

(i know, it's a forum issue.)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2019 Sep 30, 2019

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 30, 2019 Sep 30, 2019

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2019 Sep 30, 2019

Copy link to clipboard

Copied

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

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2019 Sep 30, 2019

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2019 Sep 30, 2019

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 30, 2019 Sep 30, 2019

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2019 Sep 30, 2019

Copy link to clipboard

Copied

Should not the movie play frame 1 in html5 since the first frame is number 1?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 30, 2019 Sep 30, 2019

Copy link to clipboard

Copied

Well yeah, it would make sense. Animate CC's timeline starts from 1, but when you click the output, it tells the following: Frame numbers in EaselJS start at 0 instead of 1. For example, this affects gotoAndStop and gotoAndPlay calls. (10) Motion tweens are published as frame by frame animations. Use classic tweens where possible. (9)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2019 Sep 30, 2019

Copy link to clipboard

Copied

maybe give the frame a name and call that to be sure! Just a thought...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 30, 2019 Sep 30, 2019

Copy link to clipboard

Copied

It doesn't work. 😞 MovieClips play randombly but whenever same frame is called second time, it won't play, because the movieClip has stop(); in its last frame, which is necessary, so it won't loop.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 30, 2019 Sep 30, 2019

Copy link to clipboard

Copied

Okay, I think I managed to get it work, thank you both!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2019 Sep 30, 2019

Copy link to clipboard

Copied

Glad you did but please tell us what you did so it will help someone else! Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2019 Sep 30, 2019

Copy link to clipboard

Copied

wait, wait , wait. you want the movies to loop? So you cannot really have a stop at the end, can you? When I want this kind of things I have a stop on frame 1 so the movie does not play automatically and yo can stop there when needed. I have a gotoAndPlay(2) for looping. and play() on frame 2 for looping again since frame 1 has a stop. Then I can call frame 1 to stop and frame 2 to play and loop. If that makes sense. But it is kind of convoluted, I will admit. Our great coders here probably can show you a better way.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 01, 2019 Oct 01, 2019

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 01, 2019 Oct 01, 2019

Copy link to clipboard

Copied

LATEST
you're welcome. (but step 3 is unnecessary and step 4 is only needed in the first frame that contains the button.)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines