Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 6 of 6

Thread: [Flash 8]Help: Making a "Health" system...

  1. #1

    Fla Script [Flash 8]Help: Making a "Health" system...

    I'm fairly new to flash, and I'm trying to add a system of health to one of my games. The actionscript I'm using doesn't seem to be working, and I've tried many different variations.

    This is what I'm trying to use:

    In Frame 1 Actions:

    _root.health=25;

    if(_root.character.hitTest(_root.enemy))
    if(health>0){
    health-=5
    }
    if(charhp<0){
    health=0
    }

    Can somebody show me a code that actually works?

    Much appreciated, thanks.

    PS: Go easy on me, I've only been using flash for a few days.

  2. Quote Originally Posted by JHunt609 View Post
    I'm fairly new to flash, and I'm trying to add a system of health to one of my games. The actionscript I'm using doesn't seem to be working, and I've tried many different variations.

    This is what I'm trying to use:

    In Frame 1 Actions:

    _root.health=25;

    if(_root.character.hitTest(_root.enemy))
    if(health>0){
    health-=5
    }
    if(charhp<0){
    health=0
    }

    Can somebody show me a code that actually works?

    Much appreciated, thanks.

    PS: Go easy on me, I've only been using flash for a few days.
    So you say _root.heatlth = 25. And then you substract from health? That's bad coding, pick one of the two. Not that it really matters if this is coded where I think it is, but it helps overall.

    From this little piece of code I understand that you have two variables, health and charhp. You substract from health, but if charhp is less than zero health is set to zero. What are you trying to do here? Have one health-variable, charhp or health and use it like this:

    if (health > 0 ) { health -= enemyAttackPower; }
    else { removeMovieclip(_root.charMC); }


    Or something like that. What you have here make too little sense without any explanation about what the variables do and mean.

    Hope you can do something with this

  3. #3
    Apparently, he wants a constant (for character max health) and a variable (for character current health). At least that's the only explanation I could find for having two health variables for one character. But, doing it this way, you won't get anywhere JHunt.

    If that is, indeed, the case, try something like this:
    Code:
    // Initializing Variables 
    var MAX_HEALTH:Number = 25;
    var charHealth:Number = MAX_HEALTH;
    var enemyDamage:Number = 5;
    var healthIncrement:Number = 10;
     
    // Check hit and apply damage
    if (_root.character.hitTest(_root.enemy)) {
    if (charHealth > 0){
    charHealth -= enemyDamage;
    } else {
     
    // Code for whatever happens when the character dies.
     
    }
    }
     
    // If you want to cure the character (after all that's the only purpose I see for having a maxHealth)
    if (_root.character.hitTest(_root.magicalCurePill)) {
    charHealth += healthIncrement;
    if (charHealth > MAX_HEALTH) charHealth = MAX_HEALTH;
    }
    Bruno Palermo
    When everything is catastrophic, thank the Gods things aren't worse!

  4. #4
    207
    posts
    Registered User
    Look up onEnterFrame or something. Remember you want the initialization of your health variable to run only once, but you want the hitTest to run every frame. So both can't be in the same block of code.

  5. #5
    207
    posts
    Registered User
    ..
    Last edited by rrh; June 20th, 2008 at 02:04 PM. Reason: double

  6. #6
    1)
    Code:
    character
    this is an actionscript code; will occasionally fail. I use "char" instead
    Code:
    health = 25;
    this.onEnterFrame = function(){
         if(_root.enemy.hitTest(_root.char._x, _root.char._y, true)){
              health -=1;
         }
         if(health<=0){
              _root.gotoAndStop("insert game over frame number")
         }
    }
    Last edited by Lolghurt; June 23rd, 2008 at 03:38 AM.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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