Results 1 to 4 of 4

Thread: functions

  1. #1
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default functions

    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:
    Code:
    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:
    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.
    Code:
    _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

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Try:
    Code:
    _root.slideOut.call(this, 3, 97, 99);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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.
    Code:
    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!!
    Code:
    _root.slideOut(_root.btn1, 3, 97, 99);
    Thanks for your help tho man, I owe you one for at least trying to help!

  4. #4
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    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.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •