PDA

View Full Version : need a peice of code please


avelives
03-14-2003, 11:25 AM
I need to know if there is an operator in flash which you can set to control time.
I need a peice of code to work with this

onClipEvent(load){
tx=280.3;
ty=280.3;
k=.4;
damp=.4;
}
onClipEvent(enterFrame){
tx=Math.floor(Math.random() * (530 - 20)) + 20;;
ty=Math.floor(Math.random() * (380 - 20)) + 20;;
ax=(tx-_x)*k;
ay=(ty-_y)*k;
vx+=ax;
vy+=ay;
vx*=damp;
vy*=damp;
_x+=vx;
_y+=vy;
}



However i want the two line with the mathcode to function every 30 seconds?? is there an easy way of doing this??
(i realise that the codee would have to go outside the enterframe event in order for it not to constantly jiggle around.

tost
03-14-2003, 11:34 AM
use setInterval:


onClipEvent(load){
tx=280.3;
ty=280.3;
k=.4;
damp=.4;
}

// define a function for the Interval
function calculate(){
tx=Math.floor(Math.random() * (530 - 20)) + 20;;
ty=Math.floor(Math.random() * (380 - 20)) + 20;;
ax=(tx-_x)*k;
ay=(ty-_y)*k;
vx+=ax;
vy+=ay;
vx*=damp;
vy*=damp;
_x+=vx;
_y+=vy;
}
// define the interval, params are the function to be executed and time in millisec.
myInterval = setInterval(calculate, 30000);
// run the function when you get in
// if you remove this, the function will be executed the first time after 30 sec.
calculate();

avelives
03-14-2003, 02:36 PM
I understand where your coming from but cant work out where to put this:

// define the interval, params are the function to be executed and time in millisec.
myInterval = setInterval(calculate, 30000);
// run the function when you get in
// if you remove this, the function will be executed the first time after 30 sec.
calculate();

I put all the other code in the 'load' and 'enterframe' areas.
so where does the the code for defining the interval go.
thanks for the help by the way

Ricod
03-14-2003, 03:03 PM
I think tost put them on the actions of the mc ?

avelives
03-14-2003, 03:05 PM
do you mean put them inside the actual frame actions rather than attached to a clip??

StoaVio
03-14-2003, 04:02 PM
...geniuses...

Ricod
03-14-2003, 04:08 PM
avelives, did you try just copy pasting the code and attaching it to your mc, just like that ? Looks like it just works like that.

StoaVio : Don't be like that.

tost
03-14-2003, 04:23 PM
it was just an examle, i think i see what your problem is. use this, just give "yourMC" a name and replace it here. the code goes in a frame on the timeline, where "yourMC" is on the scene too.


with (yourMC){
tx=280.3;
ty=280.3;
k=.4;
damp=.4;
}
// define a function for the Interval
function calculate(){
with (yourMC){
tx=Math.floor(Math.random() * (530 - 20)) + 20;;
ty=Math.floor(Math.random() * (380 - 20)) + 20;;
ax=(tx-_x)*k;
ay=(ty-_y)*k;
vx+=ax;
vy+=ay;
vx*=damp;
vy*=damp;
_x+=vx;
_y+=vy;
}
}
// define the interval, params are the function to be executed and time in millisec.
myInterval = setInterval(calculate, 30000);
// run the function when you get in
// if you remove this, the function will be executed the first time after 30 sec.
calculate();


hope it helps :)
tost

StoaVio
03-15-2003, 01:09 AM
Don't be like what? That was sincere because I'm really very impressed. There's no way I'd say something like that sarcastically because I'm just a beginner and have absolutely no room to critisize others. That was a compliment :)

Sorry if it came off the wrong way.

Ricod
03-15-2003, 07:08 AM
Ah, no okay then. It's hard reading emotions without seeing someone's facial expressions. :) This happened before, and will probably happen again !

avelives
03-15-2003, 11:02 AM
Thanks tost and ricod will try that now, and then on with the actionscript learning...
EDITED: Ok i tried it and hooray!! it works , thanks guys i now have my own random movement generator (simple one but looks quite nice!!)
I have lots of mini test projects of, movement and, elastic effects.
do you think its worth posting them somewhere for beginners
(which is virtually what i am) as i found it hard to find tutorials all in one place with lessons regarding actionscripting movement to make 'buttons' , for instance.
All the code i have butchered together from others tutorials is very easy to implement (it had to be made that way because as i said im only a beginner to)
Thanks for the help TOST
oh P.S found it worked smoother if you left this code out of the frame

with (yourMC){
tx=280.3;
ty=280.3;
k=.4;
damp=.4;
}
And kept it as an onclip.. (load) event attached to the clip itself then every interval it bounces to its new position rather than just appearing there, just thought id share that!

Ricod
03-15-2003, 11:21 AM
heh ! No need to thank me, tost did all the work :D ! But it does help set a nice mood on these forums. Thank you for that ! :)

tost
03-15-2003, 09:16 PM
my pleasure :)
btw, did you edit the post above? :D :D :D

Ricod
03-16-2003, 09:04 AM
;)

How to delete one of your own posts :

press the 'edit' button of the post you want to delete. At the top of the new screen, you will see a checkbox with "delete ?" next to this. Check this box and press the delete button next to it. You will return to the thread, and the post will be gone !

Well, we have a movies / library section where you can provide content. See the buttons above.

avelives
03-16-2003, 12:40 PM
Made a mess of that post didnt i!!
apologies for posting twice ...DOH!

Ricod
03-16-2003, 03:44 PM
No need to apologize fot that ! Happens to everybody ! *well, happened to me yesterday ... 3 identical posts* :D