Flash function help
- Started
- Last post
- 10 Responses
- thismanslife
I have a function called "mymotion" written in the root of my movie and i'm calling that function like this:
onClipEvent (load) {
mymotion(0,0,25);
}
(This onClipEvent code is obviously on a movieclip)... but it doesn't work!
It only works if I put "_root.mymotion" instead of just "mymotion"... but I don't want to do that if possible... whats the deal? Do you have to path down to functions written in the root like that? Am I doing something wrong??
If anyone can point me at a really simple example of an onclip event on a mc that calls a function that modifies a property of the mc (ie. scale, alpha, possition etc).. that's be great!
I'm a bit of a Flash AS novice, I learn as I go and quickly forget how I did it later!
- Boz0
onClipEvent() when used on a movie clip has a local timeline inside the movieclip not on the current one you're on.
Meaning, if you have an MC movieMC1 on a _root and you put onClipEvent on movieMC1 it will behave as you are calling a function from inside movieMC1.
All you have to do is put
onClipEvent(load){_parent.mymotion(0.0.25);
}
Putting _parent will access objects and functions on the actual timeline you are on where you're putting onClipEvent.
- thismanslife0
Thanks Boz, that does make it work, just as using _root does... the problem with that is, the function seems to change the properties of other objects in the root, not just the one which is calling the function.
I hate Flash. I seriously do.
- thismanslife0
This is the function:
function mymotion(newxpos,newypos,newscal... {
import mx.transitions.Tween;
import mx.transitions.easing.*;var xscaledown:Tween = new Tween(this, "_xscale", Strong.easeInOut, this._xscale, newscale, 0.5, true);
var yscaledown:Tween = new Tween(this, "_yscale", Strong.easeInOut, this,_yscale, newscale, 0.5, true);
var xmoveto:Tween = new Tween(this, "_x", Strong.easeInOut, this._x, newxpos, 0.5, true);
var ymoveto:Tween = new Tween(this, "_y", Strong.easeInOut, this._y, newypos, 0.5, true);
}
- koriner0
Well actually, _parent will access the timeline of the clip that is one level higher than where you're calling it from. If you want to call a function on the root level there's nothing wrong with using "_root.myfunction();"
And the reason that your function is affecting other objects is because your function is on the _root level, and you are applying Tweens to the "this" keyword, which will be referring to the entire root level.
If you want to target the specific clip, do something like this - just for an example this will change the X position:
onClipEvent (load) {
_root.mymotion(this, 100);
}//and the function:
function mymotion(clip, newxpos) {
clip._x = newxpos;
}p.s. it's not Flash's fault that you don't know how to code, so don't be too harsh.
- Boz0
koriner's code is valid and approach is ok. You need to pass the object reference to your function as well. So his code will work, however there is a lot of things wrong with using _root. It's bad coding practice.
_root shouldn't be used at all. Instead use _parent to target timelines. The reason is that if you need to load this movie inside another movie, or even copy paste your object inside a different clip in another FLA your _root reference will pretty much make headaches.
_parent will work properly in all instances and you can always target several levels deep timelines with _parent._parent._parent etc.
- thismanslife0
Thanks chaps, I got it nailed now, working like a dream!
I still hate (sitting here trying to work out how to code) Flash :)
- thismanslife0
BTW: Great work in your folio koriner, and Eriks work is rad too.
- unfittoprint0
2000 called and it wants its onClipEvent back.
- UndoUndo0
you shoud define it as a global function like:
_global.mymotion
and then later you can call it by just
mymotion();
- thismanslife0
Aaah, yeah I've heard of that before somewhere, cheers :)