PDA

View Full Version : Problem moving objects with Actionscript in Flash


yeddish
09-05-2006, 12:54 AM
I'm hoping someone can help me with this issue. It just doesn't make sense to me.

I'm moving a movieClip on the Stage using an interval, so that the motion is apparent to the user. I have to do it dynamically, because the MovieClip could be in different places on the stage, but it needs to move to a specified point.

All the movieClips move properly expect for one. It only happens when there are 2 of these movable clips and it only happens to the left one. The values are right going into the interval function (as seen by trace()), but mysteriously change somehow.

I'll attach the code with some comments showing where the problems are. I can also link to the swf and post some trace output if it's needed.

Thanks for the help.
-yeddish

function drawChildToCenterBox(childNumber, currentNode) {
if (currentNode.parentNode.attributes.children == 5) {
if (childNumber == 1) { xPos = 90; yPos = 232; }
else if (childNumber == 2) { xPos = 170; yPos = 295; }
else if (childNumber == 3) { xPos = 272; yPos = 360; }
else if (childNumber == 4) { xPos = 386; yPos = 295; }
else if (childNumber == 5) { xPos = 460; yPos = 232; }
} else if (currentNode.parentNode.attributes.children == 4) {
if (childNumber == 1) { xPos = 90; yPos = 232; }
else if (childNumber == 2) { xPos = 170; yPos = 323; }
else if (childNumber == 3) { xPos = 389; yPos = 323; }
else if (childNumber == 4) { xPos = 460; yPos = 232; }
} else if (currentNode.parentNode.attributes.children == 3) {
if (childNumber == 1) { xPos = 125; yPos = 252; }
else if (childNumber == 2) { xPos = 276; yPos = 337; }
else if (childNumber == 3) { xPos = 428; yPos = 252; }
} else if (currentNode.parentNode.attributes.children == 2) {
if (childNumber == 1) { xPos = 170; yPos = 285; } // This one has the problem
else if (childNumber == 2) { xPos = 391; yPos = 285; }
} else { xPos = 279; yPos = 289; }

// Draw child box here
boxRef = _root.attachMovie("box", "box", this.getNextHighestDepth(), {_x:xPos, _y:yPos});

// Figure the interval for stepping the box
xMoveStep = (275-xPos)/12.0;
yMoveStep = (125-yPos)/12.0;
trace("xMoveStep = " + xMoveStep); // These traces show the
trace("yMoveStep = " + yMoveStep); // proper data, even when
trace("origin xPos = " + xPos); // the node moves wrong.
trace("origin yPos = " + yPos);

// Loop and move it up to the proper place (275,125)
intervalID = setInterval(
function() {
if (i >= 11) { clearInterval(intervalID); }
xPos += xMoveStep;
yPos += yMoveStep;
trace("xPos = " + xPos + " -- yPos = " + yPos); // This trace shows the bad coords
boxRef._x = xPos;
boxRef._y = yPos;
i++;
}, 1000/12);
i = 0;

// Set up onPress
boxRef.onPress = function () {
clickedChildBox(childNumber);
}

boxRef.name.text = currentNode.firstChild.firstChild.nodeValue;
return boxRef;
}

zoobie
09-06-2006, 04:01 AM
www.flashkit.com for all things flash

tribalmaniac
09-07-2006, 02:59 PM
I'm not 100% sure of what you are trying to do, have you tried using scripted tweens? they work a lot smoother than anything else and they are all dynamic.

They are put together as follows:

import mx.transitions.Tween;
import mx.transitions.easing.*;

var myTween = new Tween(_root.my_MC, "_x", Strong.easeOut, _root.my_MC._x, 38, 1, true);


to break that down:
var myTween = this is the variable name of the tween

new Tween(

_root.my_MC this is the name of the movieclip you are trying to move

, "_x" the way in which its moving "_x", "_y", "_alpha", "_height" etc

, Strong.easeOut the type of easing on the tween

, _root.my_MC._x the starting position of the object, can be an instance name or an integer

, 38 the finishing position of the object you are moving

, 1 the duration of the tween (smaller = faster)

, true); if true, the duration is in seconds, if false the duration is in frames