Copy link to clipboard
Copied
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).
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.
1 Correct answer
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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)
- 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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
thank you .. you are the best....
Copy link to clipboard
Copied
You're welcome!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Sure. You can send to me so I can understand better the situation.
Copy link to clipboard
Copied
i just sent it via message
Copy link to clipboard
Copied
i had try and run the code I get 'AS3', Frame 1, Line 23, Column 3 1120: Access of undefined property container."
Copy link to clipboard
Copied
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
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();

