Results 1 to 12 of 12

Thread: health regenerate

  1. #1

    health regenerate

    Hi, new to scripting. Been going through tutorials and flash help file for days and I'm still wrapping my brain around learning it. I'm trying to make a countdown loop in which for example your maxhealth is 100. If it goes below 100 a countdown starts for say 60sec. At the end it will add 1 point to your health and restart the countdown. Basically regenerate over time. I'm sure this is a simple process but not sure if I use the
    var i:Number; for (i = 0; i < 100; i--) loop or put the countdown in a movie clip and have it play the movie clip if health gets below 100. I need the countdown to show up in a dynamic text as well. I've fiddled with the countdown function examples and can get it to go to zero but need it to start over after it's checked the health. I can't seem to find an example anywhere and keep trying ways but I'm still getting use how the code works and the order of operations so to speak. BTW i'm working in AS2. if that matters.

    Thanks in advance for the help.

  2. #2
    You don't use loops for countdowns, they're an entirely different beast

    In as2, you'll want to use setInterval();

    It's been awhile since I've used it, but it's along the lines of this:

    Code:
    var myInterval = setInterval( myFunction, milliseconds, optionalParameters );
    
    function myFunction()
    {
       trace( "the interval is done!" );
    
       // Use clearInterval to stop the interval from repeating.
       clearInterval( myInterval );
    }
    for more info, see Adobe's documentation:
    setInterval()


    So when you're health drops below 100, set the interval, create a function that will be called when the interval is up that will add what you need to the health, and then maybe decide in that function if the interval needs to be repeated again. Keep in mind, you can add parameters with the setInterval call, so maybe you'd make it more specific, like

    Code:
    healthInterval = setInterval( healthBoost, 100000, hero );
    function healthBoost ( whichChar )
    {
        whichChar.health += 5;
        if ( whichChar.health >= 100 )
        {
             clearInterval( healthInterval );
        }
    }
    you = function(){
    setEnabled( true );
    live();
    setEnabled( false );
    }

  3. #3
    Thanks for the help! That helped me understand setinterval much better for some reason. I got it to subtract my healthtimer by one point every second then add a point to health but I don't understand how to make it reset when the timer gets to 0. I've tried the clearinterval script but it continues to keep going into the negatives. Do I need to work with the 'var IntervalID;Number' script example in the help files and make a minimumcount = 0 and tell it to clearinterval at that point then have it restart until health is back to 100? I'm under the impression that it takes up a lot of processing to have the timer function to keep looping so I need to clear the function then restart it?

    Thanks again!

  4. #4
    most probably you are setting the interval in some place like enterframe, thereby executing/setting it again and again after it is unset!
    MY BLOG

    I need a SPRITER who can do pixel arts for an arcade fighter project. If you can help out, please pm me.
    I also need someone who can write simple xml files.


  5. #5
    Thanks bluemagica, I need this action to be continual throughout the game no matter what scene or frame i'm on. I guess the enterframe will work if I build everything in my classes (which I still need to learn) and just have one frame for the whole game. Is that something I should consider? If that isn't needed and I end up jumping to multiple frames how to I keep it going? My game is a simple game based off of icons/pictures and selecting them or mc's to progress through the game. There is no visible character running around.

  6. #6
    just put a check before you set the interval! But setInterval means that it will keep executing a function repeatedly till the interval is cleared, so actually if you call it just once it will keep looping till you clear it!
    I think it would be better for you to first read up on setInterval, atleast it will teach you more than i can say here.

    And lastly, "onEnterFrame", what does it mean to you? The function is executed whenever the playhead reaches over that frame....which means there is already "frame jumping", lol.....


    PS:- Just set the interval once outside enterframe or in onLoad, and then dont clear it till the game is over and you dont need it anymore..simple, isn't it?
    MY BLOG

    I need a SPRITER who can do pixel arts for an arcade fighter project. If you can help out, please pm me.
    I also need someone who can write simple xml files.


  7. #7
    I think i'm understanding setInterval a bit better but it says that it continues until you clear it. What I don't understand is how to reset the function or the number. So if I have a number (5) that counts down incrementally each second to 0 it will go back to 5 and countdown again. I worked with the intervalID:number script:

    var intervalId:Number;
    var count:Number = 5;
    var maxCount:Number = 0;
    var duration:Number = 1000;

    function executeCallback():Void {
    if(count > maxCount)
    count--;
    }



    function beginInterval():Void {
    if(intervalId !=null) {
    clearInterval(intervalId);
    }

    intervalId = setInterval(this, "executeCallback", 1000);
    }

    beginInterval();

    - made a dynamic text to see the number countdown on the stage but it stops at 0. How do i get the count to get back 5?

    I apologize if you've explained what needs to be done such as putting a 'check' before the setinterval. I don't quite know what kind of action that requires.

    Thanks again!

  8. #8
    Okay, i am gonna show you a simple file for example! You will see the i am not clearing the interval , and it is only cleared if you get the number less than or equal to 0.
    Attached Files Attached Files
    MY BLOG

    I need a SPRITER who can do pixel arts for an arcade fighter project. If you can help out, please pm me.
    I also need someone who can write simple xml files.


  9. #9
    Thanks! The example helped a lot! It was close to what I was looking for. After changing it around I was able to get what I needed. I had something similar before but must have misplaced code in the wrong spot. Attached is an example of what i was looking for.
    Thanks again!
    Attached Files Attached Files

  10. #10
    humm... I never used setInterval...

    I would make something like:

    Code:
    <EnterFrameLoop>{ // an enterframeloop could be object.onEnterFrame = function() or onClipEvent(enterFrame)
        if(health < 100){
            regenCounter++;
            addHealth = regenerate(regenCounter, 2, 30); //regenerate(counter, rate in seconds, fps);
            if(addHealth > 0){
                health += addHealth;
                regenCounter = 0;
            }
        }
    }
    
    ---------
    
    function regenerate(counter, rate, fps){
        var regeneration = 0;
        if(counter == rate*fps){
            regeneration = 1;
            counter = 0;
        }
        return regeneration;
    }
    it's not that clean but works...
    Last edited by SparK_BR; August 2nd, 2008 at 11:23 PM.

    "A human can interpret missing logic but a computer can't." - wkt
    "That goes into the 'too bad' box." - TOdorus
    "This is a three year old thread, there's no reason to post here..." - flyingmonkey456
    "Despite beeing the oposite, they look very similar to me." - SparK

  11. #11
    Quote Originally Posted by SparK_BR View Post
    humm... I never used setInterval...
    I only used setInterval a few times in Actionscript 2, and it usually always came with a pile of grief/frustration. I probably would've sprung for spark's method

    Now, in AS3, I've been leaning toward the Timer class big time. It's a nice improvement over setInterval, and (in my mind) tons better than enterframes. I recommend switching over to 3 if you're even remotely serious about actionscript.
    you = function(){
    setEnabled( true );
    live();
    setEnabled( false );
    }

  12. #12
    nobody likes as1...

    it's one of the first languages I learned when I was a child
    I think I was 8 years old when I made my first game in flash 4...
    no actions in MCs, only in buttons and frames!

    then you always had a splash with a button to initialize vars, and then go to the frame with the MCs, which had actions in their's first frame...

    "A human can interpret missing logic but a computer can't." - wkt
    "That goes into the 'too bad' box." - TOdorus
    "This is a three year old thread, there's no reason to post here..." - flyingmonkey456
    "Despite beeing the oposite, they look very similar to me." - SparK

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012