Defining functions to move movie clips

Hey,

probably really simple q, but this is my first day in actionscript territory. :slight_smile:

I want to define functions to move a movie clip around the stage, my code works when i define it STRAIGHT to the movie, but when i define it as functions it doesn’t.

my code put straight on the movie is:

onClipEvent (load) {
this._x = 0
this._y = 0
this._alpha = 0
this._rotation = 0

}

onClipEvent(enterFrame) {
if (this._x<=253.2) {
this._x += _global.movespeed; }

if (this._alpha<=100) {
this._alpha += _global.fadespeed; }

if (this._rotation<=45) {
this._rotation += _global.movespeed; }

if (this._y<=178.2) {
this._y += _global.movespeed; }

}

but when i put this into actions, i did it like this. i tried splitting it into 2 since 1 didn’t work.
function loadme () {
this._x = 0
this._y = 0
this._alpha = 0
this._rotation = 0
}
function move () {
if (this._x<=253.2) {
this._x += _global.movespeed; }

if (this._alpha<=100) {
this._alpha += _global.fadespeed; }

if (this._rotation<=45) {
this._rotation += _global.movespeed; }

if (this._y<=178.2) {
this._y += _global.movespeed; }

}

then assigned this code to the movie
onClipEvent (load) {
box.loadme();
}
onClipEvent (enterFrame) {
box.move();
}

and that doesn’t work.

I have no experience in coding so forgive me if this is obvious.
:slight_smile:

What is the point to your _global.fadespeed and movespeed?

Neither fadespeed of movespeed are defined in a variable and they are not Flash commands. If you replace those all with numbers you will see it work

I forgot to post this, i already defined the variables at the top of my code like so

_global.fadespeed=1;
_global.movespeed=2;

And it doesn’t work.
Tried changing the variables just to numbers and still my movie doesn’t budge.

???

Well you are going about the _global thing all wrong.

_lobal is like this like this (taken from Macromedias reference)…

_global

Availability

Flash Player 6.

Usage

_global.identifier

Parameters

None.

Returns

A reference to the global object that holds the core ActionScript classes, such as String, Object, Math, and Array.

Description

Identifier; creates global variables, objects, or classes. For example, you could create a library that is exposed as a global ActionScript object, much like the Math or Date object. Unlike Timeline-declared or locally-declared variables and functions, global variables and functions are visible to every Timeline and scope in the Flash movie, provided they are not obscured by identifiers with the same names in inner scopes.

Example

The following example creates a top-level function factorial that is available to every Timeline and scope in a Flash movie:

_global.factorial = function (n) {
if (n <= 1) {
return 1;
} else {
return n * factorial(n-1);
}
}

Do define a variable you don’t need _global there.

Just have it as

fadespeed = 15

then call it like this

if (this._alpha<=100) {
this._alpha += _root.fadespeed; }

OK. Thanks for the help btw. But it still won’t budge. Here’s the code i’m now trying

FRAME ONE OF THE MOVIE HAS THIS APPLIED TO IT:
fadespeed = 1
movespeed = 2

function loadme () {
this._x = 0
this._y = 0
this._alpha = 0
this._rotation = 0
}
function move () {
if (this._x<=253.2) {
this._x += _root.movespeed; }

if (this._alpha<=100) {
this._alpha += _root.fadespeed; }

if (this._rotation<=45) {
this._rotation += _root.movespeed; }

if (this._y<=178.2) {
this._y += _root.movespeed; }

}

THE MOVIE CLIP HAS THE FOLLOWING APPLIED TO IT
onClipEvent (load) {
this.loadme();
}
onClipEvent (enterFrame) {
this.move();
}

It’s not even reseting the position to 0,0 on the stage. So the function commands don’t seem to be being passed along to it. ???
Thanks again…

Ok, are you using Flash 5 or Flash MX?

Flash MX
but i’d prefer if i could export the code to a flash 5 file.

I started learning with MX, so I don’t know how to make it reverse compatible. What I can do is try and figure out an MX way and post it here, then a Flash 5 user can try and convert it maybe.

Ok, I believe I got this working now.

<U>This goes on a Frame</U>
MovieClip.prototype.loadme = function() {
this._x = 0;
this._y = 0;
this._alpha = 0;
this._rotation = 0;
};
MovieClip.prototype.move = function(fadespeed, movespeed) {
if (this._x<=253.2) {
this._x += movespeed;
}
if (this._alpha<=100) {
this._alpha += fadespeed;
}
if (this._rotation<=45) {
this._rotation += movespeed;
}
if (this._y<=178.2) {
this._y += movespeed;
}
};

<U>This goes on the Movie Clip</U>
onClipEvent (load) {
this.loadme();
}
onClipEvent (enterFrame) {
this.move(1, 2);
}

I optimized the code also. You can define the movespeed and fade speed for each individual movie clip you apply this to. As you see the function called on the movie clip where it says (1, 2) coresponds with MovieClip.prototype.move = function(fadespeed, movespeed)

I hope this helps:)

PS: My mind isn’t functioning correctly tonight, so if it is not what you wanted I am terribly sorry.

I didn’t look at the code too much, but I don’t think there was an error in your _global variable declaration.

pom :asian:

Thanks! That code seemed to do the trick. :smiley:

In his origonal code he declared his functions using this sytax:

function move() {
}

I didnt know that would work, I thought it had to be like this:

blah.move = function() {
}

Does his method work? I also noticed that when lost rewrote the code he used the same syntax I mentioned… could this have been the problem??

Peace

Lost defined a prototype that’s why he can use this syntax. You can have a look at the AS tricks.

pom :asian:

You can get to the AS Tricks sections here…

http://www.kirupa.com/developer/actionscript/as_tricks.asp

Click on “using prototypes instead of functions”

Prototypes are a definite time saver and an excellent way to optimize code.

ahhhh yes… my hamster must have fallen off his wheel :o

Anyone know how to make this work NOT using prototypes? Since it’s quite important i get this working in flash 5.
i can’t figure it out!

Thanks…

::Bump::

Just bumping this thread up to see if anyone knows how to convert my MX as to Flash 5 so it will work.

What do you need to change?? Prototypes work with both flash 5 and MX…

pom :elderly:

Really? That is good info. I kind of regret not starting with 5, because there are so many questions I am not sure of about Flash 5 AS compared to MX AS.