Marty1963 0 Newbie Poster

I'm trying to reuse an old Flash file that my client wants on his website however I'm using Flash CC now, and the existing code does not funtion in ActionScript3. I can't see why this won't work. Any advise would be greatly appreciated

function randRange(min:Number, max:Number):Number {
    var randomNum:Number = Math.round(Math.random()*(max-min))+min;   
    return randomNum; 
}     
var myFrame; 
myFrame = randRange(2,7); 
gotoAndStop(myFrame);

Dani AI

Generated

— Migrating AS2 timeline code into ActionScript 3 commonly breaks because AS3 is stricter about typing, scope, and how timeline targets are referenced. The most frequent culprits are (1) untyped or missing variable declarations, (2) calling gotoAndStop without casting the target to a MovieClip, and (3) a random-number expression that can produce a biased distribution. Also confirm the FLA’s Publish Settings are set to ActionScript 3.0.

import flash.display.MovieClip;
import flash.events.Event;

function randRange(min:int, max:int):int {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

addEventListener(Event.ADDED_TO_STAGE, init);

function init(e:Event):void {
    removeEventListener(Event.ADDED_TO_STAGE, init);
    var myFrame:int = randRange(2, 7);
    MovieClip(root).gotoAndStop(myFrame);
}

Troubleshooting and best practices:

  • Always declare types (e.g., int, Number) and function return types to avoid compile errors.
  • Use Math.floor(Math.random() * (max - min + 1)) + min for an even integer distribution.
  • Cast the timeline to a MovieClip when controlling the main timeline from a child clip: MovieClip(root).gotoAndStop(...). If controlling the current clip, use this.gotoAndStop(...) inside that clip.
  • Prefer frame labels (e.g., gotoAndStop("menu")) instead of hard-coded numbers for maintainability.
  • When using classes or document-class code, wait for ADDED_TO_STAGE before accessing root or stage.
  • Use trace() and the Output panel to see exact runtime/compile errors and line numbers.
  • Note: Adobe ended Flash Player support on December 31, 2020 (and browsers began blocking Flash in January 2021). For web delivery, consider migrating to HTML5/Canvas (Adobe Animate/CreateJS or JS frameworks) or packaging as an AIR app for desktop.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.