A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Decreasing health

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    7

    Question Decreasing health

    I'm trying to make a game where I have a "ball" object i move around with the cursor and dodge "block" objects. I have a variable called health and I want to make it so whenever the ball is hit by the block, the ball loses 1 health. here's the code.

    health = 10;

    if(_root.ball.hitTest(_root.block)){
    health--;
    }

    nothing at all happens when the block hits the ball.

    Anyone know what the problem is?

  2. #2
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    First of try this instead,
    Actionscript Code:
    if(_root.block.hitTest(_root.ball)) {
        health--;
    }
    Wile E. Coyote - "Clear as mud?"

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    7

    Thumbs up

    ty, works just fine now. Now I just need to know how to keep the health number from going into the negatives :P
    Last edited by wickedwing; 03-06-2010 at 11:38 AM.

  4. #4
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    instead of health--; use a conditional:

    Actionscript Code:
    health>0?health--:health=0;

    That will make sure health doesn't go below 0.

    gparis

  5. #5
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    That's quite nice,... could you explain? As in, what the "?" signify as well as the ":". Thanks.
    Wile E. Coyote - "Clear as mud?"

  6. #6
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    '?' is if and ':' is else. Shorthand conditionnal for lazy programmers like myself.

    gparis

  7. #7
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    Lazy?!! On the contrary... I think it's quite eloquent.
    Wile E. Coyote - "Clear as mud?"

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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center