Jump to content


If statement with variable help


  • You cannot reply to this topic
1 reply to this topic

#1 Blue_Monkey

    Young Padawan

  • Members
  • Pip
  • 11 posts

Posted 15 December 2007 - 06:12 PM

I'm trying to make an RPG-esque flash game. I created dynamic text with the var: "FH" (for foe's health), dynamic text with the var: "YM" (for your magic), and dynamic text with the var: "YH" (for your health; although, this text is not important). This is the code I have for the first layer of the second frame (the first frame being the start button frame) :
YH = 100;
FH = 9000;
YM = 10;
if(YM < 1){
    _root.magic.gotoAndStop(2);
}

I want the attack button for the magic attack to be disabled once "YM" has reached 0. Therefore, I changed the button into a movie clip with frame 1 as the button and frame 2 without the button. I gave the movie clip the instance name of "magic". The button's code is:
on (release) {
    _root.FH -= 300;
    _root.YM -= 1;
}

When I start the movie, and click the magic attack button, the "FH" text decreases by 300 and "YM" decreases by 1 as it should. However, when "YM" is below 1, the movie clip instance "magic" does not go to frame 2. Yet, if I change the frame's code to start "YM" at 0, the movie clip goes to frame 2. For some reason, the "if" statement does not recognize that the variable changes, and only applies to the initial variable. Can you help me figure out what the problem is?

Edited by Blue_Monkey, 18 January 2008 - 05:45 PM.


#2 flamereaper

    Young Padawan

  • Members
  • Pip
  • 65 posts
  • Gender:Male
  • Location:UK

Posted 09 January 2008 - 12:34 PM

a simple problem that many overlook in flash.

you have to understand that unless you actualy tell flash to check all the time, it will only check once, when the code loads.

What you need to do is have your code in frame two look like this.

onEnterFrame=function(){
if(YM < 1){
_root.magic.gotoAndStop(2);
}
}

this will then make sure that your if test runs at (the frame rate) times per second. another way of doing it would be to put the if test on the button so that the test is only run when the magic value changes, and make sure its put after the decrease code.


There ya go hope it helps. good luck with the game.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users