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

AS3 Random Keyframes * I need it not to randomize

Community Beginner ,
Jun 26, 2018 Jun 26, 2018

question for all. I have a movieclip and inside that movieclip i have code & 3 movieclips on each keyframe. eventually 20. my question to all is with this code below:

code below is in keyframe 1.  movieclips are keyframe 2,3,4.  I

stop();

var randNum:Number = Math.floor(Math.random() * 3) + 2;
//var fixedFrame:Number = randNum + 1;
gotoAndStop(randNum);

it works fine, it randomizes & plays but my problem: I just want to play the movieclip once. how do avoid the repeating? eventually I will be putting in 20 separate mocieclips in each keyframe & I only want to show those 20 movieclip once in a rotation. never repeating. can someone assist me

so each time the code gets executed it should play frame 1 (movieclip,)  then the next time it gets executed it plays frame 2( movieclip).

Screen Shot 2018-06-26 at 10.57.49 AM.png

example:   20 choices (movieclips), next go around you only have 19 choices, so on until there is only 1 choice.

for the image that is attached I had just set up 3 keyframes.  but technically speaking it should have 20.

523
Translate
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

correct answers 1 Correct answer

Community Expert , Jun 26, 2018 Jun 26, 2018

Sure. No problem.

The easiest way would be to:

- Move the handler function code to a new function;

var frames:Array = [];

var count:int = -1;

function start():void

{

for (var i:int = 0, total:int = container.totalFrames; i < total; i++)

  frames = i + 1;

getFrame();

}

function getFrame():void

{

var sequence:uint = ++count % frames.length;

if (sequence == 0)

  frames.sort(randomSort);

container.gotoAndStop(frames[sequence]);

}

function randomSort(a:*, b:*):Number

{

if (Math.random() < 0.5)

  return -1;

else

  re

...
Translate
Community Expert ,
Jun 26, 2018 Jun 26, 2018

Hi.

What you can do is to preset the order in a array and then simply increment a index to access each element.

Here is a simple example where the user clicks the screen to change the current frame of the container Movie Clip according to the order stored in the array.

Once all elements from the array are used, another sequence is sorted.

var frames:Array = [];

var count:int = -1;

function start():void

{

for (var i:int = 0, total:int = container.totalFrames; i < total; i++)

  frames = i + 1;

stage.addEventListener(MouseEvent.CLICK, function()

{

  var sequence:uint = ++count % frames.length;

  if (sequence == 0)

   frames.sort(randomSort);

  container.gotoAndStop(frames[sequence]);

});

}

function randomSort(a:*, b:*):Number

{

if (Math.random() < 0.5)

  return -1;

else

  return 1;

}

start();

I hope this helps.

Regards,

JC

Translate
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 ,
Jun 26, 2018 Jun 26, 2018

that would work but let me just ask:   there is no user clicks so I dont need a mouse click.  i have the main movieclip being executed from the main stage ( automatically) so can i just comment out the "btn click)

  1. stage.addEventListener(MouseEvent.CLICK, function() 

can i delete that line?  what do I need to put in its place to run automatically when it initalizes

thanks much.  I have been up most of the night searching for answers.

let me know what I need to do with that line so it doesnt look for mouse click

Translate
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 ,
Jun 26, 2018 Jun 26, 2018

Sure. No problem.

The easiest way would be to:

- Move the handler function code to a new function;

var frames:Array = [];

var count:int = -1;

function start():void

{

for (var i:int = 0, total:int = container.totalFrames; i < total; i++)

  frames = i + 1;

getFrame();

}

function getFrame():void

{

var sequence:uint = ++count % frames.length;

if (sequence == 0)

  frames.sort(randomSort);

container.gotoAndStop(frames[sequence]);

}

function randomSort(a:*, b:*):Number

{

if (Math.random() < 0.5)

  return -1;

else

  return 1;

}

start();

- Put this action...

MovieClip(root).getFrame();

... in the last frame of each child Movie Clip so the next sorted frame gets called every time the timeline reachs the end.

FLA download:

animate_cc_as3_random_frames_02.zip - Google Drive

Regards,

JC

Translate
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 ,
Jun 26, 2018 Jun 26, 2018

thank you .. you are the best....

Translate
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 ,
Jun 26, 2018 Jun 26, 2018

You're welcome!

Translate
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 ,
Jun 26, 2018 Jun 26, 2018

just one other question:

how do i do that but stop it from counting. i want it to select one key frame play the movclip and stop  then when it gets called again it plays a diffrent one.

right now they are continuous. 

Translate
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 ,
Jun 26, 2018 Jun 26, 2018

how do i do that but stop it from counting. i want it to select one key frame play the movclip and stop  then when it gets called again ( i have AS script in timeline that calls in movieclip on first frame and in that movieclip it would play what I was describing above.. like a bingo ball number.

example:  so  ball movieclip gets called on stage from AS on first frame.  in that movieclip is 20 diffrent movieclips on each keyframe and would like to have the action play those clips

i can send you the .fla source file that has everything intact

plays a different one.

right now they are continuous.

Translate
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 ,
Jun 26, 2018 Jun 26, 2018

Sure. You can send to me so I can understand better the situation.

Translate
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 ,
Jun 26, 2018 Jun 26, 2018
LATEST

i just sent it via message

Translate
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 ,
Jun 26, 2018 Jun 26, 2018

i had try and run the code I get 'AS3', Frame 1, Line 23, Column 3 1120: Access of undefined property container."

Translate
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 ,
Jun 26, 2018 Jun 26, 2018

here is full code with error:

'AS3', Frame 1, Line 23, Column 3 1120: Access of undefined property container."

but there is not container in that movieclip?  its 3 movieclips on each keyframe.. am I missing something

Screen Shot 2018-06-26 at 6.59.57 PM.png

no need for button click function

var frames:Array = [];

var count:int = -1;

function start():void

{

for (var i:int = 0, total:int = container.totalFrames; i < total; i++)

  frames = i + 1;

stage.addEventListener(Event.ENTER_FRAME, function()

{

  var sequence:uint = ++count % frames.length;

  if (sequence == 0)

   frames.sort(randomSort);

  container.gotoAndStop(frames[sequence]);

});

}

function randomSort(a:*, b:*):Number

{

if (Math.random() < 0.5)

  return -1;

else

  return 1;

}

start();

Translate
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