SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Targeting mc inside of a loop

    I have this loop as part of the timeline of an swf loading into a parent movie:
    Code:
    var TNsHolder1 = this.createEmptyMovieClip("TNsContainer1_mc", this.getNextHighestDepth());
    	
    var maxTNsInRow = 6;
    var TNsWidth = 0;
    var TNsHeight = 0;
    
    for (var i:Number = 0; i<=(_root.artTNsArray.length-1); i++) {
    
    	var TN = TNsHolder1.createEmptyMovieClip("TN"+i, TNsHolder1.getNextHighestDepth());
    	var TNt = TN.attachMovie(_root.artTNsArray[i], "TNpic"+i, TN.getNextHighestDepth());
    	var TNb = TN.attachMovie("boxFrame", "boxFrame"+i, TN.getNextHighestDepth());
    	TNt._alpha = 100;
    	TNb._alpha = 100;
    
    		TN._x = TNsWidth;
    		TN._x = TN._x;
    		TN._y = TNsHeight;
    		TN._y = TN._y;
    
    	if (i == maxTNsInRow-1 || i == ((maxTNsInRow*2)-1) || i == ((maxTNsInRow*3)-1)) {
    		TNsWidth = 0;
    		TNsHeight = TNsHeight + (TN._height) + 10;
    	} else {
    		TNsWidth = TNsWidth + (TN._width) + 10;
    	}
    			
    	TN.id = i;
    	
    	TNsHolder1["TN"+TN.id].onRollOver = _root.boxOvrIn;
    	TNsHolder1["TN"+TN.id].onRollOut = _root.boxOvrOut;
    	TNsHolder1["TN"+TN.id].onRelease = _root.TNtPress;
    	
    };
    The functions it is calling (_root.boxOvrIn, etc) are located on the timeline of the main (parent) movie:
    Code:
    function boxOvrIn() {
    	var tnZoomXIn_Twn = new Tween(this, "_xscale", Regular.easeOut, this._xscale, 107, .2, true);
    	var tnZoomYIn_Twn = new Tween(this, "_yscale", Regular.easeOut, this._yscale, 107, .2, true);
    	var tnZoomAOut_Twn = new Tween(this, "_alpha", Regular.easeOut, this._alpha, 50, .3, true);
    }
    Here's my dilemma,
    "this" (in the function) is referring to what in the loop is declared as TN. That is fine and works as it should. How do reference TNt and TNb (both nested inside of TN)? Using this.TNt doesn't seem to do it. It appears the function only knows that this=TN but nothing beyond that...
    any ideas?

  2. #2
    RTFM ashleyk's Avatar
    Join Date
    Dec 2005
    Location
    Rochester, New York
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the references TNt and TNb are absolute references to those MCs, so that's why using this won't work - it would evaluate to something like this: this._root.artTNsArray[i] etc...which obviously isn't right.

    you would have to pass a reference to the name of the TNt movie clip ("TNpic"+i) into the function, then you would be able to reference it using "this".
    ---------------------------------------
    Ashley Kennedy
    ---------------------------------------
    how does a designer get roped into writing code?

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you know i gotta say sometimes when I look at code my mind blanks out completely... this is one of those times. This loops is starting to drive me nuts, and loopy.

    ...you're suggesting I should do something like this?
    TNsHolder1["TN"+TN.id].onRollOver = _root.boxOvrIn(TNt);

    and then
    function boxOvrIn(TNpic) {
    var tnZoomXIn_Twn = new Tween(this.TNpic, "_xscale", Regular.easeOut, this.TNpic._xscale, 107, .2, true);
    }

    ...this a complete "free willie off the top of my head" coding, i'm not sure if it makes any sense

  4. #4
    RTFM ashleyk's Avatar
    Join Date
    Dec 2005
    Location
    Rochester, New York
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well yes, but it would actually be like this (i think):

    Code:
    TNsHolder1["TN"+TN.id].onRollOver = _root.boxOvrIn(TNt);
    
    
    function boxOvrIn(TNpic) {
    var tnZoomXIn_Twn = new Tween(TNpic, "_xscale", Regular.easeOut, TNpic._xscale, 107, .2, true);
    }
    because if you were to trace "TNt" it would trace to
    Code:
    _root.TNsHolder1.TNpic0
    so you shouldn't need the "this".
    ---------------------------------------
    Ashley Kennedy
    ---------------------------------------
    how does a designer get roped into writing code?

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you know this may work because it seems to be tracing correctly but here's another issue (which I've seen happen before, not sure how to resolve it)

    if from within loop I call a function like this:
    TNsHolder1["TN"+TN.id].onRollOver = _root.boxOvrIn; // "_root.boxOvrIn;" WITHOUT parenthesis
    then it it calls it only when activated, ie. on roll over in this case

    if I phrase it like this
    TNsHolder1["TN"+TN.id].onRollOver = _root.boxOvrIn(); // "_root.boxOvrIn();" WITH parenthesis (regardless if containing argument or not)
    it automatically jums to this function once it finds the line with the call, not waiting for on roll over or whatever other trigger

    why is that?

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok so I've found the explanation as to why this is happening, now I need an explanation on how to go around it.

    as it turns out the function name is a reference to a particular function and the parenthesis is the actual 'run the function now' command. So the parenthesis is actually the part that executes a function, which explains why it's being triggered right away.

    Now how do I encode the argument without triggering the function immediately?

  7. #7
    RTFM ashleyk's Avatar
    Join Date
    Dec 2005
    Location
    Rochester, New York
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah i figured that'd be a problem...you just have to write out the anonymous function call...

    Code:
    TNsHolder1["TN"+TN.id].onRollOver = function(){
    _root.boxOvrIn(params);
    }
    ---------------------------------------
    Ashley Kennedy
    ---------------------------------------
    how does a designer get roped into writing code?

  8. #8
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...ok getting closer
    it works other than it's only targeting the last reference number in iteration of the loop; it only does the tween on the last element created by the loop regardless which element triggers the function

    I'm trying to get it to be unique for each element... here's my latest attempt (unsuccessful):
    Code:
    for (var i:Number = 0; i<=(_root.artTNsArray.length-1); i++) {
    
    	var TN = TNsHolder1.createEmptyMovieClip("TN"+i, TNsHolder1.getNextHighestDepth());
    	var TNt = TN.attachMovie(_root.artTNsArray[i], "TNpic"+i, TN.getNextHighestDepth());
    	var TNb = TN.attachMovie("boxFrame", "boxFrame"+i, TN.getNextHighestDepth());
    
    		TN._x = TNsWidth;
    		TN._x = TN._x;
    		TN._y = TNsHeight;
    		TN._y = TN._y;
    
    	if (i == maxTNsInRow-1 || i == ((maxTNsInRow*2)-1) || i == ((maxTNsInRow*3)-1)) {
    		TNsWidth = 0;
    		TNsHeight = TNsHeight + (TN._height) + 10;
    	} else {
    		TNsWidth = TNsWidth + (TN._width) + 10;
    	}
    			
    	TN.id = i;
    	TNt.id = i;  // created this element... not sure if it's needed but it feels like it should be there
    	
    	TNsHolder1["TN"+TN.id].onRollOver = function() {  //
    		_root.boxOvrIn(TNsHolder1["TN"+TN.id]["TNpic"+TNt.id]); // I'm guessing these two lines need to be fixed to point to a unique element
    	}
    };
    and the function, which I think is good as is:
    Code:
    function boxOvrIn(TNpic) {
    	var tnZoomXIn_Twn = new Tween(TNpic, "_xscale", Regular.easeOut, TNpic._xscale, 107, .2, true);
    }


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
  •