Log in

View Full Version : functions



jamiller
11-07-2007, 09:19 PM
So I've been having this problem for quite a while and it would really be nice to figure it out. I write a bunch of code for something and instead of copy and pasting the code each time I need it I'd like to turn it into a function. The problem is that I don't write the function on the same action layer that I call the function, to me that is useless. So I pasted the function onto the root timeline and am calling the function from a few mc's deep. Here's the function:


function slideOut(totBtns, xmouse, ymouse) {

over.onRollOver = function() {};

for(var i=0; i<=totBtns; i++) {
var btn = this.box["btn"+i];
btn.onRollOver = function() {
this.gotoAndPlay("over");
}
btn.onRollOut = function() {
this.gotoAndPlay("overOut");
}
}

this.onEnterFrame = function() {
if(_xmouse < 0 || _xmouse > xmouse || _ymouse < 0 || _ymouse > ymouse) {
gotoAndPlay("overOut");
this.onEnterFrame = null;
}
}
}


I'm pretty sure that it has something to do with all the "this" keywords I have. I'm curious, does the called function call all objects from where the actual function is located? In my case that would be _root. So would all paths need to originate from _root or where I call the function? To me it seems that they should originate from the called function, as I have it above. But I've tried it the other way too, and nothing. The only way this works is if I paste the above function on the same action layer I call the function, which, as I stated above is absolutely useless in what I'm building.

And here is the called function code:


slideOut(3, 97, 99);


I've heard that if a function resides on the root timeline then I have to put in the _root keyword before the function.


_root.slideOut(3, 97, 99);

But that didn't work either...

Anybody have any ideas?? I'm about 30 minutes away from throwing my computer out the window so it would really appreciate the help. Before it dies!

Jeff

Twey
11-07-2007, 09:45 PM
Try:
_root.slideOut.call(this, 3, 97, 99);

jamiller
11-07-2007, 09:57 PM
hah, I figured it out! Although your suggestion looks a little nicer than mine, I got it to work. Basically by adding in another argument to the function, the button name; btn1, btn2, etc, I was able to correctly path everything.


function slideOut(myBtn, totBtns, xmouse, ymouse) {

myBtn.over.onRollOver = function() {};

for(var i=0; i<=totBtns; i++) {
var btn = myBtn.box["btn"+i];
btn.onRollOver = function() {
this.gotoAndPlay("over");
}
btn.onRollOut = function() {
this.gotoAndPlay("overOut");
}
}

myBtn.onEnterFrame = function() {
if(_xmouse < 0 || _xmouse > xmouse || _ymouse < 0 || _ymouse > ymouse) {
myBtn.gotoAndPlay("overOut");
myBtn.onEnterFrame = null;
}
}
}


And I can now call the function anywhere in my movie!!


_root.slideOut(_root.btn1, 3, 97, 99);


Thanks for your help tho man, I owe you one for at least trying to help!

BLiZZaRD
11-08-2007, 04:13 AM
The problem was with the this. callings. You said that the effect was called from a few MC's deep.

the this. refers to the top most instance. So if you have button then this. is what you use. if you have button1 inside button 2 inside button 3 then this. will refer to button 3. instead you need to use this.btn1.btn2.btn3 for relative coding and _root.btn.btn1.btn2.btn3 for absolute. Note that each instance will need an instance name.