Search code examples
functionactionscript-3flash-cs6

How can i get a function to use any movie clip?


I'm a little lost in understanding this kind of function, i get the feeling this has been asked a thousand times but cannot find an explanation of what the code is doing.

Basically i just want a movie clip with instance name box to do something, then reuse the function for other movie clips afterwards

a little like this, but working.

Many Thanks

//my function to be used on "instance name" box
myfunc (box);

function myfunc ();
{
    while (this is happening);
    {
    //in this case box.x = goes where ever i put it
    .x = goes here
    .y = goes here
    }
}

Sorry it's not quite English, my communication skills are terrible


Solution

  • Sure you can do that. You give the function a parameter, then refer to a parameter to change its properties. With such a simple movement function it could accept a DisplayObject - a distant superclass of a MovieClip and thus a superclass to many other possible classes of those objects that can be displayed by Flash.

    function myfunc(param:DisplayObject):void {
        // no semicolon after declaring the function!
        while (somethingIsHappening(param)) {
            // why not call a query on that object?
            param.x+=1; // move right by 1 pixel
        }
    }
    

    You might want to look at this manual on ActionScript 3 syntax, and the following pages on variables, functions and classes, to learn more.