Log in

View Full Version : Random xy ease + drag issues



Mark__G
09-27-2008, 12:47 AM
I am trying to have a movieclip ease into a random position. Then the mc is dragable and swaps depth with the other mc's in the movie.

Here is the code I have so far but my random positioning is interfering with the dragability of the mc. Once the mc eases into its random position if you try and drag it anywhere it wants to lock back to the x y coordinate that it randomly eased to...



acceleration = 10
newpos = function () {
ranx = Math.round((Math.random ()*250));
rany = Math.round ((Math.random ()*250));
}
newpos();
this.onEnterFrame = function() {
this._x += ((ranx-this._x)/acceleration);
this._y += ((rany-this._y)/acceleration);
};

drag9.onPress = function() {
_root.slide9.startDrag();
_root.slide9.swapDepths(_root.getNextHighestDepth());
}
drag9.onRelease = drag.onReleaseOutside = function() {
_root.slide9.stopDrag();
}

Medyman
09-27-2008, 03:28 PM
That's because you're setting the x and y coordinates in an onEnterFrameEvent. That means that event fires once a frame. So, you're continuously setting the x and y coordinates equal to the random coordinates.

I'd suggest that you use a tween to ease the MC. That'll allow you to get rid of the onEnterFrame and get rid of this problem. That would also be a more efficient use of system resources.

At the very least delete the onEnterFrame after the MC is at the desired coordiates. You could do it within the onPress function. But, I suggest doing it within the onEnterFrame itself (with the use of a conditional).

Mark__G
09-29-2008, 09:38 PM
I am trying to figure out what the best conditional statement would be for it to only set the x and y coordinates randomly once, and once the coordinates have been set for the initial landing then to release them to allow for the drag function to work properly.

Master_script_maker
09-29-2008, 10:31 PM
try:

acceleration = 10
newpos = function () {
ranx = Math.round((Math.random ()*250));
rany = Math.round ((Math.random ()*250));
}
newpos();
var set:Boolean=false;
this.onEnterFrame = function() {
if(!set) {
this._x += ((ranx-this._x)/acceleration);
this._y += ((rany-this._y)/acceleration);
set=true;
} else {
this.onEnterFrame=null;
}
};

drag9.onPress = function() {
_root.slide9.startDrag();
_root.slide9.swapDepths(_root.getNextHighestDepth());
}
drag9.onRelease = drag.onReleaseOutside = function() {
_root.slide9.stopDrag();
}

Medyman
09-29-2008, 10:46 PM
The above would work. You could also test to see if the x coordinate of the MC is equal to randx (or test the y).