View Full Version : creating timing for movement
gencoglu
January 30th, 2009, 07:16 AM
hi,
what I am after is to make the object(hero) move to any direction randomly for 3000 millisecond and pause/delay for 2000 milliseconds and move again...
// move any direction for 3000 ms
//delay for 2000 ms
//now move any other direction again..and so on..then I wiil use a hittest to create my simple game..but I can not figure out how to create start and pause functions..to get timing..
any help ?
thanks
tapioca
January 30th, 2009, 12:18 PM
i don't have my source code with me but the way that i do it is something like this (in pseudo code):
walking = function() {
counter++;
take_step(dir);
if counter > 30 {
counter = 0;
clearInterval(random_walk_interval);
random_walk_interval = setInterval(pausing);
}
}
pausing = function() {
counter++;
if counter > 20 {
counter = 0;
dir = random();
clearInterval(random_walk_interval);
random_walk_interval = setInterval(walking);
}
}
setInterval(walking);
good luck and hope that helps. :P sorry i'm not very good at explaining what things do. but reading the help file for the setInterval function is a really good idea, it's very useful for things like this.
substance
January 30th, 2009, 12:35 PM
If you're using AS3 (which you should be) you'd use the Timer class.
t:Timer = new Timer(3000);
t.addEvenetListener(Timer.COMPLETE, walkOrWhatever);
t.start();
gencoglu
January 30th, 2009, 01:41 PM
Hi,
Thanks for your reply...but I seem to fail in making my car(object) move...:whistle:
will you please fix this ?
thanks a lot.
on(press)
{
walking = function() {
counter++;
take_step(dir);
if (counter > 10) {
counter = 0;
clearInterval(random_walk_interval);
random_walk_interval = setInterval(pausing);
}
}
pausing = function() {
counter++;
if (counter > 5) {
counter = 0;
dir =Math.floor(Math.random()*10)+1;
clearInterval(random_walk_interval);
random_walk_interval = setInterval(walking,100);
}
}
setInterval(walking,100);
function walking(dir)
{
_root.car._x+=10;
}
}
tapioca
January 30th, 2009, 04:49 PM
If you're using AS3 (which you should be) you'd use the Timer class.
haha, thanks, i wasn't sure how as3 did timers, i've been stuck on the same as2 project forever and haven't had a chance to switch over yet. :/
tapioca
January 30th, 2009, 04:55 PM
Hi,
Thanks for your reply...but I seem to fail in making my car(object) move...:whistle:
will you please fix this ?
thanks a lot.
i'm not SURE if this is the issue or if you're working in as2 or as3, (you should probably be in as3 if you're starting a project, like substance said), but if it's as2, it might help to do
_root.walking = function(dir)
{
_root.car._x+=10;
}
setInterval(_root.walking,100);
also you shouldn't have function definitions inside of events unless you need the function to re-define itself every time the event is triggered.
nathan99
February 1st, 2009, 09:50 AM
If you're using AS3 (which you should be) you'd use the Timer class.
t:Timer = new Timer(3000);
t.addEvenetListener(Timer.COMPLETE, walkOrWhatever);
t.start();
Why "should" he be using AS3. It's solely personal preference.
descalabro
February 1st, 2009, 11:25 AM
Hello.
I'm a novice here using action script so maybe you people here can help me?
My objective is to have a horizontal bar to move to the right as I press a button. The movement must be limited to 5 increments but also fluid without using motion tweens, so I have to use setInterval.
the code:
on (press) {
if (i<=5) {
_root.barmove = function(dir) {
_root.barra_MC._x += 5;
++i;
//trace("i="+i);
};
setInterval(_root.barmove, 1000);
} else {
clearInterval(_root.barmove);
}
}
My first problem is that the if isn't working, as both the bar and the value of i won't stop advancing.
My second problem is that the speed of the bar will increment as the function is called several times if I press the button several times.
I'm also not sure if clearInterval(_root.barmove); is correct and if it does anything.
I appreciate any help.
Thank you.
descalabro
February 1st, 2009, 12:45 PM
it's solved :book: :
on (press) {
if (i<5) {
Pause = function () {
if (i<5) {
_root.barra_MC._x += 10;
++i;
trace("i="+i);
} //possibly unnecessary//
else {
clearInterval(Pausei);
}
};
} else {
clearInterval(Pausei);
trace("i is bigger than 5");
}
Pausei = setInterval(Pause, 1000);
}
Hello.
I'm a novice here using action script so maybe you people here can help me?
My objective is to have a horizontal bar to move to the right as I press a button. The movement must be limited to 5 increments but also fluid without using motion tweens, so I have to use setInterval.
the code:
on (press) {
if (i<=5) {
_root.barmove = function(dir) {
_root.barra_MC._x += 5;
++i;
//trace("i="+i);
};
setInterval(_root.barmove, 1000);
} else {
clearInterval(_root.barmove);
}
}
My first problem is that the if isn't working, as both the bar and the value of i won't stop advancing.
My second problem is that the speed of the bar will increment as the function is called several times if I press the button several times.
I'm also not sure if clearInterval(_root.barmove); is correct and if it does anything.
I appreciate any help.
Thank you.
felixirDesign
February 4th, 2009, 09:24 AM
i have a similar problem. i am trying to create my "hero" so that he has limited flight, the walking and jumping aspects work fine it it purely the in flight that i am having trouble with. the character flys up ok but when you take your finger off the button his flight time is supposed to slowly recharge and it is this im having trouble with, any help would be much appreciated
here is my code
var yspeed = 0;
var xspeed = 0;
var gravity = 0.6;
var rotationspeed = 0;
var power = 1.6;
var flightPower = 1.6;
var friction = 0.9;
var flightTime = 0;
onEnterFrame = function() {
yspeed += gravity;
xspeed *= friction;
this._x += xspeed;
rotationspeed *= friction;
this._rotation = 0+rotationspeed;
if (this._y<=400-this._height/2) {
this._y += yspeed;
if (Key.isDown(Key.LEFT)) {
xspeed -= power;
rotationspeed += power*xspeed/2;
} else if (Key.isDown(Key.RIGHT)) {
xspeed += power;
rotationspeed += power*xspeed/2;
}
} else {
xspeed *= friction;
yspeed = 0;
this._y = 400-this._height/2;
}
//here is where the trouble begins
if (Key.isDown(Key.UP)) {
yspeed -= flightPower;
flightTime =3;
countDown = function () {
flightTime--;
if (flightTime == 0) {
flightPower = 0.2;
}
}
timer = setInterval(countDown, 500);
}else{
powerBack = function (){
flightTime = flightTime +0.2;
flightPower= 1.6;
if (flightTime >=3){
flightTime=3;
}
}
timer = setInterval(powerBack, 3000)
}
//and ends
if (yspeed<0) {
yspeed *= friction;
}
}
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.