Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 02-12-2012, 11:33 PM   #1
Registered User
Join Date: Feb 2012
Posts: 5
Question random frame , need help

Hi all, I need some assistance with my as3 code. What I am trying to do is when spacebar is clicked I want it to stop on a random frame. Here is the code I am working with

ActionScript Code:
function random(min:Number=0, max:Number=5):Number {   return Math.floor(Math.random() * (max - min + 1)) + min;   } stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed); function onKeyPressed(evt:KeyboardEvent):void{     switch (evt.keyCode) {         case Keyboard.ENTER:             box.play();             break;         case Keyboard.BACKSPACE:             box.stop();             break;         case Keyboard.LEFT:             box.prevFrame();             break;         case Keyboard.RIGHT:             box.nextFrame();             break;         case Keyboard.SPACE:             box.gotoAndStop(random(5));             break;         default:             trace("keyCode:", evt.keyCode, "charChode:", evt.charCode);     } };
zegestar is offline  
Old 02-12-2012, 11:53 PM   #2
Senior Member
[afz]snickelfitz's Avatar
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,400

In the keyboard event handler, start by generating a random number based on the totalFrames property of the box MovieClip.

Something like this:
ActionScript Code:
var randFrame:int = Math.floor(Math.random() * box.totalFrames) + 1; // ... in the spacebar case box.gotoAndStop(randFrame);

The +1 thing is due to the timeline starting at 1 and not zero.
Might need some additional logic there to avoid an out of range error at the top end of the scale.
[afz]snickelfitz is offline  
Old 02-13-2012, 12:05 AM   #3
Registered User
Join Date: Feb 2012
Posts: 5

it works, thanks big time!!!
zegestar is offline  
Closed Thread

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 01:46 PM.

Follow actionscriptorg on Twitter


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.