dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Detect variable value

  1. #1
    Member
    Join Date
    Oct 2003
    Posts
    72

    Detect variable value

    Hello All,
    I have a Dynamic Text field with the variable name of PowerOut.
    Everytime I press a button (in a separate movie) the value in the variable increases by 1.
    What I need to do now is,

    when the value on the variable reaches 10 jump to frame 15.

    I've tried the code below but the whole thing dissapears before I press the button even once.

    Code:
    If (PowerOut = 10) {
    gotoandPlay (15);
    }
    When I tried this nothing happens at all

    Code:
    If (PowerOut == 10) {
    gotoandPlay (15);
    }
    Any ideas anybody?
    Thanks

  2. #2
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    You need to execute the action more than once. If you just put it on a frame it will only check when it plays that frame.
    Btw, the correct one is ==.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  3. #3
    Member
    Join Date
    Oct 2003
    Posts
    72
    hey,
    I dont really know what you mean by executing the action more than once.
    How do I go about doing that?
    Thanks

  4. #4
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    Instead of a variable name, give your dynamic text field an instance name PowerOut

    Now whenever you need to retrieve the value of the text in this field, you need to access the dynamic textfields property called text

    so in your example:

    Code:
    if (PowerOut.text ==10) {
    gotoandPlay (15);
    }
    Also note: use the keyword if NOT If

    Also instead of naming it PowerOut, it's more conventional to name variables in lowercase and then uppercase if there is another word for e.g

    powerOut

    Normally words beginning with uppercase are used for Class names!!
    Last edited by silentweed; 04-15-2006 at 05:13 AM.
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  5. #5
    Member
    Join Date
    Oct 2003
    Posts
    72
    Hey thanks for your help and the naming covention guidelines.
    I have tried what you've sugested but no luck.

    Here is the file, if you have time to have a look.
    Cheers
    Attached Files Attached Files

  6. #6
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    cant open ur file..im using mx2004 and pc..

    paste all of your code here ... also when is the if statement being checked? is it when you press a button or is it within an onEnterFrame handler?
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  7. #7
    Member
    Join Date
    Oct 2003
    Posts
    72
    Hey
    Here is the file for MX 2004. I'm on a PC also.

    Also here is the code I have in the button. Just to let you know in advance that I will have more than one button. but they will all have the same value.
    So this is the trimmed down version.

    The button sits in its own movie.
    Code:
    //code on the button
    
    on (release) {
    
    _root.num += _root.buttonA_value;
    _root.powerOut = _root.num;
    
    }

    Code:
    //code on the action frame, in the main movie.
    
    stop();
    
    var buttonA_value = 1
    var current_output = powerOut
    var num:Number = 0
    
    if (powerOut == 10) {
    gotoandPlay (15);
    }
    Power out is still the name of the variable, as when i changed it to the actual instance it didnt work

    thanks, I really appreciate this.
    Attached Files Attached Files

  8. #8
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    Whats happening in your code is the if statement is only read once i.e when the movie firsts loads and reads the actions in frame 1. Since at that time the variable num is equal to zero..therefore nothig happens. What you need to do is this:

    everytime a button is pressed you have to say to flash 'hey can you run the 'if' statement again please and check if the condition is satisified now' ..

    The best way of doing this is by putting the if statement within a function. Then every time a button is pressed..you call this function !!

    Also another suggestion, since it seems you are quite new to flash i would recommend you get into the habit of not attaching code to the buttons and movieclips themselves, but insteas giving them instance names and then controlling them from the maintimeline actions frame instead. This will make you code much more manageable and easier to debug etc...

    Ive attached a wokring version of your file ..for you to see all this in action..
    Attached Files Attached Files
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  9. #9
    Member
    Join Date
    Oct 2003
    Posts
    72
    Hey silentweed,
    First of all, the script works fantastic, thank you very much.
    My initial thinking was that the script on the first frame would always be active as there is only one frame in the movie. Cheers for clearing all that up.

    You are totally right about the actions on the timeline. I was aware you could do that (i did it on a couple of projects) but I certanly should get on the habit of doing it all the time.

    Yes, I'm pretty new to flash, how can you tell?

    thanks again.

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