[FMX] Function after function

Hi,

I have a menu that appears on the stage with the following function (thank you divarch for making me undestand functions way better )

[AS]function moveMenu (clip, yPos, speed){
clip.onEnterFrame=function(){
var endPos = yPos-this._y;
this._y += endPos/speed;
}
}
moveMenu (menu, 50, 3);[/AS]

Next to the menu I have a movieclip (instance name “square”) which I want to appear on the stage from the left, but not before menu (see above) has reached it’s final position. How do I integrate this in the above function or should I make a seperate function for this :q:

You’ve got a little syntax error in there The operator to check for equality (==**) is not equal to the assignment operator. So you would have to change if (this._y = yPos) to if (this._y == yPos). Here’s my solution for this (a different way).


MovieClip.prototype.moveY = function(pos, func, obj, param) {
	this.onEnterFrame = function() {
		this._y = pos-(pos-this._y)/1.2;
		if (this._y>pos-1 && this._y<pos+1) {
			delete this.onEnterFrame;
			func.apply(obj,param)
		}
	};
};
mc1.moveY(250,MovieClip.prototype.moveY,mc2,[250])

Hi Voetsjoeba,

Thanks for your solution. The ProtoType is indeed a good solution. I still have one question about my own function. I changed the = in == so my code looks like this:

[AS]function moveMenu (clip, yPos, speed){
clip.onEnterFrame=function(){
var endyPos = yPos-this._y;
this._y += endyPos/speed;
if (this._y == yPos) {
function moveSquare (clip2, xPos, speed){
clip2.onEnterFrame=function(){
var endxPos = xPos-this._x;
this._x += endxPos/speed;

			}
		}
				
	}
}

}
moveMenu (menu, 50, 3);
moveSquare(square, 200, 3);[/AS]

but still nothing happen with the mc square. I think it has someting to do with calling the second function, but i’m not sure. What am I doing wrong?

Well, first of all, youare checking if y is equal to the position to move to. But, since you are always dividing by 3, there is a chance that the position won’t be exactly the same as the target. Therefore, I always check if the position is inside a range of 2px from the target:


if(this._y > endyPos-1 && this._y < endyPos+1)

Using this code, there is a chance that the position of the movieclip is at max 1px off it’s target, but I don’t that’s really important; after all, who’s gonna notice 1px ?

Secondly, you have an error in how it works. When the movieclip has reached it’s position, you keep declaring the function but not calling. You must call the function, not declare it. You are already calling it at the beginning:


moveMenu (menu, 50, 3);
moveSquare(square, 200, 3);

But it hasn’t been defined yet because the first movieclip hasn’t reached it’s target yet, and it’s declared when the target is reached. This is absolutely wrong. Also, you should delete the first onEnterFrame when it has reached it’s target.


function moveSquare(xPos,speed){
square.onEnterFrame=function(){
var endxPos = xPos-this._x;
this._x += endxPos/speed;
if(this._x > endxPos-1 && this._x < endxPos+1){
delete this.onEnterFrame;
}
}
}
function moveMenu (yPos, speed){
        menu.onEnterFrame=function(){
                var endyPos = yPos-this._y;
                this._y += endyPos/speed;
                if(this._y > endyPos-1 && this._y < endyPos+1) {
                     delete this.onEnterFrame
moveSquare();   
                }
        }
}
moveMenu();

It’s working :beam: thanks a lot for the clear explanation! Thumps up :thumb:

No problem :wink:

yeah, thanks to voets for taking over…
I was at a party last night, else, I’d have answered right away. luckily, voets is here, backing me up :wink: :stuck_out_tongue: