View Full Version : Movement on random time
LippuZz
November 30th, 2005, 08:32 AM
Hi there,
i want to make a movement. An object has to come from behind an object and go back again. The interval between each time the object appears has to be random.
i was trting to maken something with the setTimer() function, and a random. The random gives a var. This var i would use as time for the object appears in combination with the setTimer()
Problem is that i can't come up with a decent code that would work.
been a long time i used AS so i need somebody who can help me with this, simple, code. Where do i need to define the start vars, adn where do i define the random script.
greetz
stringy
November 30th, 2005, 10:02 AM
Hi there,
i want to make a movement. An object has to come from behind an object and go back again. The interval between each time the object appears has to be random.
i was trting to maken something with the setTimer() function, and a random. The random gives a var. This var i would use as time for the object appears in combination with the setTimer()
Problem is that i can't come up with a decent code that would work.
been a long time i used AS so i need somebody who can help me with this, simple, code. Where do i need to define the start vars, adn where do i define the random script.
greetz
give the movieclip an instance name of mc and try
var boo = this;
var t = 2000+random(10000);
var startx = mc._x;
var endx = mc._x+200;
var tx = endx;
function move(targetx) {
this.onEnterFrame = function() {
this._x += (targetx-this._x)/3;
if (Math.abs(targetx-this._x)<2) {
this._x = targetx;
delete this.onEnterFrame;
t = 1000+1000*random(10);
(playing=!playing) ? tx=startx : tx=endx;
setTimeout(this, "move", t, tx);
trace(t);
}
};
}
_global.setTimeout = function(a, b, c) {
var args = arguments.slice(3);
var func = function () {
boo[b].apply(a, args);
clearInterval(arguments.callee.ID);
};
func.ID = setInterval(func, c, args);
};
setTimeout(mc, "move", t, tx);
this just moves it along the _x axis and back but you could easily add code for _y axis and even make it move a random distance each time.
Barn
November 30th, 2005, 11:55 AM
Or you could do it this way. Note that the part between the double-dash lines is just a script to create a couple of objects on the screen for swapping, so that all you need to do is copy and paste this code into an otherwise empty single-frame new movie and then run Test Movie:
// ==============================
// FUNCTION to create objects.
fncInit = function () {
// Create objects
rraColor = [0xFF0000, 0x00FF00];
for (var icrClip = 0; icrClip<2; icrClip++) {
var rfcClip = this.createEmptyMovieClip("mvcClip"+icrClip, 100*icrClip);
rfcClip._x = 100+icrClip*25;
rfcClip._y = 100+icrClip*25;
rfcClip.beginFill(rraColor[icrClip], 75);
rfcClip.moveTo(0, 0);
rfcClip.lineTo(100, 0);
rfcClip.lineTo(100, 100);
rfcClip.lineTo(0, 100);
rfcClip.lineTo(0, 0);
rfcClip.endFill();
}
};
// ==============================
fncSwap = function () {
clearInterval(itvSwap);
mvcClip0.swapDepths(mvcClip1);
itvSwap = setInterval(function () {
clearInterval(itvSwap);
mvcClip0.swapDepths(mvcClip1);
fncSwapLaunch();
}, nbrTimeFront);
};
fncSwapLaunch = function () {
nbrItv = Math.floor(Math.random()*(nbrSwapMax-nbrSwapMin))+nbrSwapMin;
itvSwap = setInterval(fncSwap, nbrItv);
};
// Create objects
fncInit();
// nbrTimeFront: number of milliseconds the object remains in front after first swap
nbrTimeFront = 500;
// nbrSwapMin: minimum number of milliseconds for object to remain behind.
nbrSwapMin = 500;
// nbrSwapMax: maximum number of milliseconds for object to remain behind.
nbrSwapMax = 2000;
// Begin
fncSwapLaunch();
LippuZz
November 30th, 2005, 12:14 PM
thanks alot! this helped me a lot
Barn
November 30th, 2005, 01:08 PM
You're welcome.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.