PDA

View Full Version : Useless prototype (only works with a single object)


Alvaretto
06-30-2004, 12:27 AM
I know it's not the prototype the one to blame but me.

It should work like this: a MC moves a certain amount of pixels, stops and wait for a certain amount of time, it moves again, stops again, and so on.

It works fine with only one MC but when I add more clips its a complete mess.

Here's the code:


MovieClip.prototype.mover = function(step) {
count = function (clip) {
clip._x += 1;
updateAfterEvent();
if (distance >= step) {
clearInterval(move);
if (distance >= step) {
distance = 0;
}
}
distance++;
};
var move = setInterval(count, 5, this);
};

MovieClip.prototype.stopper = function(step,time) {
clip=this
sarasa = function () {
clip.mover(step);
};
setInterval(sarasa, time);
};
mc1.stopper(100,2000);
//mc2.stopper(100,1500)
//mc3.stopper(150,1000)



Thanks.

Tink
06-30-2004, 03:19 PM
MovieClip.prototype.mover = function(step, time) {
var distance = 0;
this.onEnterFrame = function () {
this._x +=1;
distance ++;
if (distance == step) {
delete this.onEnterFrame;
this.stopper(step, time);
}
}
};

MovieClip.prototype.stopper = function(step, time) {
var startTime = 0;
this.onEnterFrame = function () {
startTime ++;
if (startTime == time) {
delete this.onEnterFrame;
this.mover(step, time);
}
}
};

//mc1.stopper(100, 200);i pretty sure you should hav a function within a function. i.e. you can't have the function that you are running using setInterval inside you prototype.

the problem once its external to your prototype is that there is only 1 setInterval function and therefore when you start trying to run more than one clip you are just overwriting the function and calling it from the beginning again (it sounds like this is happening although you have tried to put it inside your prototype).

the above code should initially fire the timer, once the time has passed it will fired the moving prototype. once this has reach its target it will fire the timer function again and so on

any good?

ericlin
06-30-2004, 04:11 PM
Make those variable "var" might help.
MovieClip.prototype.mover = function(step) {
var distance=0;
var move=0;
count = function (clip) {
clip._x += 1;
updateAfterEvent();
if (distance>=step) {
clearInterval(move);
if (distance>=step) {
distance = 0;
}
}
distance++;
};
var move = setInterval(count, 5, this);
};
MovieClip.prototype.stopper = function(step, time) {
var clip = this;
sarasa = function () {
clip.mover(step);
};
setInterval(sarasa, time);
};
mc1.stopper(100, 2000);
mc2.stopper(100,1500)
//mc3.stopper(150,1000)

Tink
06-30-2004, 04:13 PM
can you run a function using setInterval, that is contained within another function? should the functions be seprate eric?

ericlin
06-30-2004, 05:30 PM
That condition is a bit complicated. I am also confused.

Both sarasa and count function are defined and used immediately, so I think setInterval will work. It is because we use the "reference of function" as parameter. If we use "function name" as parameter, then we will get the over-writtened function executed by setInterval.

I am not sure what that prototype want to do. One call of "stopper" brings out serial calls of a sarasa function. Each call of sarasa brings out a serial of new function "count", although we can only grasp one. Each count is try to move movieClip right ward.

At some time, there are several not-die-yet "count" functions existing there, so the clip does not move in regular steps. It depends how many copies of that function is created by sarasa. SO, the clip may move 3,5,6 ...before it pause.

And the last one, is the problem about "scope" of "var". This is even more complicated.

Your script is much simpler and efficient.

Alvaretto
06-30-2004, 06:59 PM
Thanks, both codes work fine!

I'm going to stay my code (with the adding of "var", of course) because it's independent from the "frames per second". In that way I can make the clips move faster or slower without changing the FPS or the amount of translation in the x axis. The result is a smoother movement.

I put an interval of 5 milliseconds here which is way too small. An interval of 8.3 ms will make the clips move like if the FPS was 120.

I have another question: the first thing executed here is the interval, so, before starting to move the first time the clips just "wait". How can this first interval be eliminated?