all groups > flash actionscript > march 2007 >
You're in the

flash actionscript

group:

Tween in custom class


Tween in custom class truk nosral
3/20/2007 7:31:09 PM
flash actionscript:
I'm a relative flash/actionscript newbie. I created a towers of hanoi solution
moving movie clips around using Tweens. This all worked great when the action
script containing the Tween creation existed on what I guess is the root movie
clip (within the Action script window of my .fla file).
I tried moving the hanoi solution into a custom class (extending
EventDispatcher) and now the tween does not 'run'. I've stepped through my code
with the debugger and added plenty of trace statements and am pretty sure all
the arguments to the Tween constructor are correct.

_currentTween = new Tween(_movingDiskMc, "_y", Regular.easeOut,
_movingDiskMc._y, _yMin, _duration);

Is there something fundamental I'm missing? Is the frame rate an issue with
the custom class? The value of _duration above is 5.

thx

- Kurt
Re: Tween in custom class SymTsb
3/20/2007 11:15:56 PM
Re: Tween in custom class truk nosral
3/26/2007 4:04:22 PM
Wow... thx for offering to take a look. An instance of Hanoi is created from
the action script in the main .fla file. Below is the function which contains
creation of the tween



// Executes one step of the solution
function executeStep():Void {
var eventObj:Object;
eventObj.type = "stepComplete";
eventObj.moveNumber = _moveNumber;
dispatchEvent(eventObj);

// if we are done stop the interval
if (_moveNumber == _solution.length) {
return;
}

var from:Stake;
var to:Stake;
var step:Step = _solution[_moveNumber++];
switch (step.from) {
case 1 :
from = _stake1;
break;
case 2 :
from = _stake2;
break;
case 3 :
from = _stake3;
break;
}
switch (step.to) {
case 1 :
to = _stake1;
break;
case 2 :
to = _stake2;
break;
case 3 :
to = _stake3;
break;
}

// The disk to move
var disk:Disk = Disk(from.pop());
_movingDiskMc = disk._diskMc;

// The new x and y
_newX = to.xForDisk(disk);
_newY = to.yForDisk(disk);

//start the up tween
_currentTween = new Tween(_movingDiskMc, "_y", Regular.easeOut,
_movingDiskMc._y, _yMin, _duration);
trace("x and y: " + _movingDiskMc._x + ", " + _movingDiskMc._y);

// when finished start the across tween
_currentTween.onMotionFinished = acrossTween;

// update the to disk with the new disk (a bit premature but that's OK)
to.push(disk);
}

// start the across tween, when finished start the down tween
function acrossTween() {
_currentTween = new Tween(_movingDiskMc, "_x", 0, _movingDiskMc._x, _newX,
_duration);
_currentTween.onMotionFinished = downTween;
}

// start the down tween, when finished start the next step
function downTween() {
_currentTween = new Tween(_movingDiskMc, "_y", Regular.easeIn, _yMin, _newY,
_duration);
_currentTween.onMotionFinished = executeStep;
}
Re: Tween in custom class truk nosral
3/26/2007 5:07:48 PM
ahhhh.... after further review found that my 'onMotionFinished' event handlers
were being called but in the debugger the scope is the Tween class not my Hanoi
class. That's messed up :shocked;

found the discussion in the help topic regarding scope of event handlers and
using the 'suggested strategy' things are working fine now



//start the up tween
_currentTween = new Tween(_movingDiskMc, "_y", Regular.easeOut,
_movingDiskMc._y, _yMin, _duration);

// when finished start the across tween
// Yikes! scope of the anonymous function is the Tween class not Hanoi. This
is true even if the function is named.
// This 'strategy' of the anonymous function having access to the local
variables of the enclosing function
// and using a var set to 'this' appears to be the only way around this.
var owner:Hanoi = this;
_currentTween.onMotionFinished = function ():Void {
owner.upDone();
}
AddThis Social Bookmark Button