dcsimg
A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: [as2][help?] simple (i hope) timer problems?

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    25

    [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?

  2. #2
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    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(shoot1again800); 

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    25
    i did that. i moved the setInterval part to after the function - it still didn't work any differently

  4. #4
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    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(shoot1again800); 
    However, to me it looks like it would be simpler to do something like
    PHP Code:
    function shoot () { <my code to shoot> }
    setInterval(shoot800); 

  5. #5
    Junior Member
    Join Date
    Jul 2009
    Posts
    25
    i'll try out that second approach - seems the best for what i'm trying to acomplish. thanks - i'll post how it goes

  6. #6
    Junior Member
    Join Date
    Jul 2009
    Posts
    25
    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?

  7. #7
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    How about this ?
    PHP Code:
    waitUntil 0;
    function 
    shoot1 () {
    <
    blablabla>
    waitUntil = (new Date()).getTime() + <time needed to reload after shoot 1in milliseconds>;
    }
    function 
    shoot2 () { <same> }
    ...

    something.onMouseDown = function () {
     var 
    = (new Date()).getTime();
    if (
    waitUntil) {
    <do 
    your tests and call some shooting function>
    }


  8. #8
    Junior Member
    Join Date
    Jul 2009
    Posts
    25
    wont that make it so after you click you wait soso number of milliseconds, then the bullet fires?

  9. #9
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    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.

  10. #10
    Junior Member
    Join Date
    Jul 2009
    Posts
    25
    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

  11. #11
    Junior Member
    Join Date
    Jul 2009
    Posts
    25
    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:
    Code:
    _root.shoot1();

  12. #12
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    What about removing _root. ?

  13. #13
    Junior Member
    Join Date
    Jul 2009
    Posts
    25
    then i got an error
    PHP Code:
    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.

  14. #14
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    It makes it sound like the function was badly declared. Can you post more of your code ?

  15. #15
    Junior Member
    Join Date
    Jul 2009
    Posts
    25
    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 
    = (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!

  16. #16
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    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 () {
    blablathis.shoot1();
    blablathis.shoot2();

    It looks nicer this way anyway.
    BTW, you have useless parenthesis here :
    waitUntil = ((new Date()).getTime() + 333)

  17. #17
    Junior Member
    Join Date
    Jul 2009
    Posts
    25
    i know they're useless, i was just seeing if they did anything. nope lol. so when i clean it up as you suggested... (testing it now )
    :O

    thanks! it works! my gosh that was crazy eh? again, thank you so much for sticking with me
    so much for a little timer problem!
    thanks again, cant thank you enough.

  18. #18
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    You're welcome.

    Have fun making games =)

  19. #19
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    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

  20. #20
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center