dcsimg
A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: Variables that can't go below zero etc

  1. #1
    Senior Member
    Join Date
    Jul 2003
    Posts
    119

    Variables that can't go below zero etc

    Hey all, just getting the grip around the variables and im experimenting.
    This is what i have as a variable

    money = 1000;
    health = 100;

    Then i have the dynamic textbox that shows them.
    works great, and i also have various effect that makes the two go up and down. Here's the problem, or more, how i would like to expand the concept but is stuck on.

    Right now both variables can go as high and low as possible (of course).
    Is there something i can add in that script that makes so the money variable can't go below 0.

    Also is there something i can add so that the Health can't go ABOVE say 500 and when health reaches zero (or a negative) this triggers

    _root.gotoAndPlay("gameover");

    you get what im trying to achive here

    Thanks for the help! I hope i made myself clear

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    // place the following after where ever you have something like this: money -= 10;
    if (money<0) {
    	money = 0;
    }
    
    // place the following after where ever you have something like this: health += 10;
    if (health>500) {
    	health = 500;
    }
    if (health<0) {
    	_root.gotoAndPlay("gameover");
    }

  3. #3
    Senior Member
    Join Date
    Jul 2003
    Posts
    119
    That did it, thanks alot!

  4. #4
    Senior Member
    Join Date
    Jul 2003
    Posts
    119
    hmm might as well put this question here instead of making a new thread.
    I've been trying to expand the script to a moving character hitting something and getting -10 life (the character movieclip is named 'man').

    This is the script i tried.

    Code:
    onClipEvent (load) {
    	if ((this.hitTest(_root.man))) {
    		health -= 10;
    	if (health>500) {
    		health = 500;
    	}
    	if (health<1) {
    		_root.gotoAndPlay("gameover");
    	}
    }
    But i keep getting a syntax error on itthough i can't figure out what it is... can anyone help out with this?

  5. #5
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    You're missing a } at the end of the first statement.

  6. #6
    Senior Member
    Join Date
    Jul 2003
    Posts
    119
    Code:
    onClipEvent (enterFrame) {	}
    	if ((this.hitTest(_root.man))) {
    		health -= 10;
    	if (health>500) {
    		health = 500;
    	}
    	if (health<1) {
    		_root.gotoAndPlay("gameover");
    	}
    }
    gets me

    **Error** Scene=Scene 1, layer=Hero, frame=4:Line 2: Statement must appear within on/onClipEvent handler
    if ((this.hitTest(_root.man))) {

    Total ActionScript Errors: 1 Reported Errors: 1

    in the debug

  7. #7
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Improper formatting causing the problems, try this...
    Code:
    onClipEvent (enterFrame) {
    	if (this.hitTest(_root.man)) {
    		health -= 10;
    	}
    	if (health>500) {
    		health = 500;
    	}
    	if (health<1) {
    		_root.gotoAndPlay("gameover");
    	}
    }

  8. #8
    Senior Member
    Join Date
    Jul 2003
    Posts
    119
    Quote Originally Posted by dawsonk View Post
    Improper formatting causing the problems, try this...
    Code:
    onClipEvent (enterFrame) {
    	if (this.hitTest(_root.man)) {
    		health -= 10;
    	}
    	if (health>500) {
    		health = 500;
    	}
    	if (health<1) {
    		_root.gotoAndPlay("gameover");
    	}
    }
    hmm alright this is odd, this script works in that flash doesn't complain about the formating. But the health bar doesn't go down on hit test

    I have one layer on top with

    health = 500;

    and then a dynamic text box in the game layer that shows the health. The buttons work, but the hit test doesn't affect it.

    Any clues on why?

  9. #9
    :
    Join Date
    Dec 2002
    Posts
    3,518
    The posted code is only changing the value of the variable 'health'.
    Do you have code in your health bar that makes it update when the value of the 'health' variable is changed?

  10. #10
    Senior Member
    Join Date
    Jul 2003
    Posts
    119
    Quote Originally Posted by dawsonk View Post
    Do you have code in your health bar that makes it update when the value of the 'health' variable is changed?
    hmm interesting, you learn something new everyday it seems

    No the health bar is only a dynamic text box with the var health.

    I didn't know i needed a script to update the value itself, when i have a button with this script:

    Code:
    on (release) {
    	health -= 10;
    	if (health>500) {
    		health = 500;
    	}
    	if (health<1) {
    		_root.gotoAndPlay("gameover");
    	}
    }
    It works and updates the dynamic textbox without a script to update it

  11. #11
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Can you post a stripped down version of your fla here?

  12. #12
    Senior Member
    Join Date
    Jul 2003
    Posts
    119
    Absolutely! Here's the barebones fla with some notes in it that points to the problem
    Attached Files Attached Files

  13. #13
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    onClipEvent (enterFrame) {
    	if (this.hitTest(_root.man)) {
    		_root.health -= 10;
    	}
    	if (_root.health>500) {
    		_root.health = 500;
    	}
    	if (_root.health<1) {
    		_root.gotoAndPlay("gameover");
    	}
    }

  14. #14
    Senior Member
    Join Date
    Jul 2003
    Posts
    119
    Ah of course! I should have known that, sometimes you dont see the forest behind all the trees...

    It even works as i intended with the health draining. Just for future references, is there anything i can add to that script that makes it so that that it only triggers once? IE that one you hit it you get -10 then you have to walk out and hit it again so you get -10 and so on?

    Anyway, thanks alot. The problem in itself is solved

  15. #15
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    onClipEvent (load) {
    	var hitme = false;
    }
    
    onClipEvent (enterFrame) {
    	if (!hitme) {
    		if (this.hitTest(_root.man)) {
    			_root.health -= 10;
    			hitme = true;
    			if (_root.health<1) {
    				_root.gotoAndPlay("gameover");
    			}
    		}
    	} else {
    		if (!this.hitTest(_root.man)) {
    			hitme = false;
    		}
    	}
    }

  16. #16
    Senior Member
    Join Date
    Jul 2003
    Posts
    119
    Works wonderfully! Always a nice variant to keep around just in case. Thanks alot for all the help, truly.

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