Search code examples
actionscript-3flashactionscriptflash-builder

Adobe Flash frame scripting versus seprate file scripting


in my game , I have created separated action script classes and files for enemies, players and other objects of the game. this is pretty good , because this makes me to use dry ( don't repeat yourself) design principle . but in my menu screen there is a script like this at start of frame 100:

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

var clouds: Array = [cloud1, cloud2, cloud3, cloud4];
var initCloudsX: Array = [cloud1.x, cloud2.x, cloud3.x, cloud4.x];
var nclouds = clouds.length;
var scrollSpeed: Number = 1;

if (this.hasEventListener(Event.ENTER_FRAME)) {
    this.removeEventListener(Event.ENTER_FRAME, scroll);

}


this.addEventListener(Event.ENTER_FRAME, scroll);
function garbageFrame(): void {
    sndBgChannel.stop();
    if (this.hasEventListener(Event.ENTER_FRAME)) {
        this.removeEventListener(Event.ENTER_FRAME, scroll);

    }
}
function scroll(e: Event): void {
    ground1.x -= scrollSpeed;
    ground2.x -= scrollSpeed;

    mountains1.x -= scrollSpeed;
    mountains2.x -= scrollSpeed;
    if (ground1.x < -ground1.width) {

        ground1.x = ground2.x + ground2.width - 5;
    }

    if (ground2.x < -ground2.width) {
        ground2.x = ground1.x + ground1.width - 10;
    }

    if (mountains1.x < -mountains1.width) {

        mountains1.x = mountains2.x + mountains2.width - 2;
    }
    if (mountains2.x < -mountains2.width) {

        mountains2.x = mountains1.x + mountains1.width - 2;
    }




    for (var i: Number = 0; i < nclouds; i++) {
        clouds[i].x -= scrollSpeed;
        if (clouds[i].x < -clouds[i].width) {
            clouds[i].x = initCloudsX[i];
        }
    }
}

So far , I think it is a good approach and after leaving the frame 101, by calling garbageFrame which has been defined above , I can simply leave the frame without any hassles or bothering. But the question raises here is , for example suppose that in frame 300 , there is a movie clip with instance name of clouds. the compiler will throw me an error that you have defined a clouds of type of array before in frame 101.so:

1 ) it shows me that if I define a variable, frame 1 , it is not destroying in any other frames.

2 ) I think in the background , the flash compiler makes a class which puts every actions of frames onto it , so it doesn't allow me to redefine a variable or function twice the times.

Now what's your idea for mixed approach of using action script programming , I mean using both frame scripting and separated file classes ( and linkage). Does it make any sense? what is the pron and cons of frame scripting?

Thanks in advance..


Solution

  • Maybe someone have treated that the post is not exciting and voted down the post, but I will answer this question myself. after reading ,searching and also thinking to the other comments , even though , maybe other users would think that there is no answer for that , the final answer is :

    Before I go any further, I should go ahead and state for the record that coding on the timeline should be avoided at all costs. There is basically nothing that you can’t do with classes to control your DisplayObjects at this point, and forcing your code into classes imposes better architecture and less sloppy shortcuts that will later come back to bite you. Now , I say “ basically ” because until AS3 (Flash CS3, specifically) you still had to put a stop() action on the last frame of any MovieClip you didn’t want to loop. Since switching to all-class scripting architecture, I found it very frustrating to not be able to easily remove this last bit of straggling timeline code from my FLA once and for all. Then I discovered an undocumented method of MovieClips. It’s called addFrameScript, and it is a complete mystery to me why Adobe hasn’t documented it or encouraged its use, because it is a fantastic piece of code. Basically, it allows you to tell a particular function to run when a certain frame of a MovieClip is hit. Unlike all the other MovieClip functions, it is zero based, rather than one based, so you must subtract one from the desired frame number to use it correctly. Here is its syntax in the context of a MovieClip class:

    public function MyMovieClip() {
      addFrameScript(totalFrames-1, stop);
    }
    

    Now when the clip reaches the last frame, it will call its stop() method and not loop. Obviously this has further reaching implications and uses than simply stopping a MovieClip from playing. In fact, I have come up with a way to use this method to make up for what I see as a defi ciency in ActionScript with regard to MovieClips and frame labels. Since early versions of Flash, you could put string labels on any frame in the timeline and use them as reference points for navigation. Starting in AS3, Adobe finally introduced the ability to see what label you’re currently on in a clip (with the currentLabel property) as well as a list of all the labels in a clip (the currentLabels property). I’ve long thought that Flash should dispatch an event whenever a frame label is hit so you could trigger actions based on label markers.