-
[as2][help?] simple (i hope) timer problems?
hey,
so i have a timer that i was trying to use to separate gunshots but it doesn't work.
Code:
if (shoot1== true){
<my code to shoot>
shoot1 = false;
setInterval(shoot1again, 800);
function shoot1again(){
shoot1 = true;
};
i cant see anything wrong with this. am i just missing something obvious?
-
Senior Member
You must define the function before making a reference to it in the setInterval command. Therefore, your code should be :
PHP Code:
function shoot1again(){ shoot1 = true; }
if (shoot1== true){<my code to shoot>} shoot1 = false; setInterval(shoot1again, 800);
-
i did that. i moved the setInterval part to after the function - it still didn't work any differently
-
Senior Member
I guess you must be expecting the following bit of code to run all the time too so that shooting will happen whenever shoot1 is true.
PHP Code:
if (shoot1== true){<my code to shoot>} shoot1 = false;
The problem is that given you've put this code on your main timeline, it will run only when its frame is reached (ie. probably once). What you probably want to do is something like
PHP Code:
function shoot1again(){ shoot1 = true; } this.onEnterFrame = function () { if (shoot1== true){<my code to shoot>} shoot1 = false; } setInterval(shoot1again, 800);
However, to me it looks like it would be simpler to do something like
PHP Code:
function shoot () { <my code to shoot> } setInterval(shoot, 800);
-
i'll try out that second approach - seems the best for what i'm trying to acomplish. thanks - i'll post how it goes
-
ok that didn't work. let me try to explain in more detail what i'm trying to acomplish. this is a shooting game, and after you click it goes to an onMouseClick function. within that function are 10 if statements that test which gun is equiped. Each if statement is
Code:
if (gunEquip == 1 & shoot1 == true)
all the way up to 10, as i showed before. after all of the bullet duplicating and motion happens, i want there to be a delay before you can shoot again. so i set the shoot1 = false and then i need to delay the function that sets it equal to true. how can i do this?
-
Senior Member
How about this ?
PHP Code:
waitUntil = 0; function shoot1 () { <blablabla> waitUntil = (new Date()).getTime() + <time needed to reload after shoot 1, in milliseconds>; } function shoot2 () { <same> } ...
something.onMouseDown = function () { var t = (new Date()).getTime(); if (t > waitUntil) { <do your tests and call some shooting function> } }
-
wont that make it so after you click you wait soso number of milliseconds, then the bullet fires?
-
Senior Member
No. The action takes place when you're clicking and then sets up a date in time before which you're not allowed to shoot again.
Nothing will happen if you click before this date. Shooting will be instantaneous is you click after it.
-
ok i'll try this one out if i get some time today and post back my success level haha. thanks for sticking with me. i'll post soon
-
its not working now. i set breakpoints and debugged and it went to the call to the function but never entered the function. is there anything wrong this this line for calling the function:
-
Senior Member
What about removing _root. ?
-
then i got an error
PHP Code:
a function call on a nonfunction was attempted
from the way that sounds it seems like it doesn't know that my functions exist, but i put them above the call.
-
Senior Member
It makes it sound like the function was badly declared. Can you post more of your code ?
-
lets see whats useful...
PHP Code:
waitUntil = 0;
function shoot1() {
<a bunch of code, if you need some of this i can add it>
waitUntil = ((new Date()).getTime() + 333)
}
function shoot2() {
<same deal>
waitUntil = ((new Date()).getTime() + 1000)
}
char.onMouseDown = function() {
var t = (new Date()).getTime();
if (t>waitUntil) {
if (gunEquip == 1) {
shoot1();
}
if (gunEquip == 2) {
shoot2();
}
}
}
i'm very new to this so a silly mistake makes sense, but i wouldnt know where to look :/
thanks again for the help!
-
Senior Member
I don't know what's causing the problem exactly, it must be some kind of scope issue but I'm not too used to coding on the timeline. One solution would be to write
PHP Code:
char.shoot1 = function () {blabla} char.shoot2 = function () {blabla} char.onMouseDown = function () { blabla; this.shoot1(); blabla; this.shoot2(); }
It looks nicer this way anyway.
BTW, you have useless parenthesis here :
waitUntil = ((new Date()).getTime() + 333)
-
-
Senior Member
You're welcome.
Have fun making games =)
-
Pumpkin Carving 2008
Assuming you use the method you showed above for all gunEquip values, you can use the following bit of code to speed it up a bit.
Code:
char.onMouseDown = function() {
var t = (new Date()).getTime();
if (t>waitUntil) {
this["shoot"+gunEquip]();
}
}
Edit: I lied, this is faster.
Code:
char.onMouseDown = function() {
var t = (new Date()).getTime();
(t>waitUntil) ? this["shoot"+gunEquip]() : null;
}
Last edited by ImprisonedPride; 07-29-2009 at 08:16 PM.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
Senior Member
PHP Code:
char.onMouseDown = function() { (new Date()).getTime() > waitUntil ? this["shoot" + gunEquip]() : null; }
is even faster :P
If you want to go dirty and use non strict typing + undefined crap, you can also cram it into (assuming there's no shoot0) :
PHP Code:
char.onMouseDown = function() { this["shoot" + gunEquip * ((new Date()).getTime() > waitUntil)](); }
Optimization fight, go go go !
Last edited by fil_razorback; 07-29-2009 at 08:29 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|