Keeping score in Flash (n00b level)

Discussion in 'Mac Programming' started by Macnoviz, Nov 9, 2006.

  1. Macnoviz macrumors 65816

    Macnoviz

    Joined:
    Jan 10, 2006
    Location:
    Roeselare, Belgium
    #1
    Hi, I am making a little game for the youth council in my town, but the only code I have ever written before that was on a Texas instruments calculator.
    I already figured out quite a bit, but I need help to make a system to keep the score.
    I need a piece of code that I can use in a symbol, which adds 1 to a number in the bottom in the stage every time the symbol is pushed.
    roughly it would look like:

    (beginning of the scene)
    0 store to "a"

    (in the actions for the symbol, I already have the trigger code, I just need the effect code)
    ("a"+1) store to "a"

    and then the ability to display "a" in a text frame on stage



    I also need to have code to trigger a frame jump after a certain time period (say 30 seconds)
     
  2. electric macrumors regular

    Joined:
    Oct 14, 2002
    Location:
    San Francisco, CA
    #2
    Try this

    // put your dynamic text field name below to the right of the equals sign
    var textFieldName = yourTextFieldHere
    // make a equal 0;
    var a=0;
    // give text field count of 0
    textFieldName.text = a;
    // function called (triggered) by what ever makes a score
    function counter(){
    // add new score
    a++;
    // give text field updated count
    textFieldName.text = a;
    trace(a);
    }

    // to trigger the score to count, call the function
    counter();
     
  3. electric macrumors regular

    Joined:
    Oct 14, 2002
    Location:
    San Francisco, CA
    #3
    Paste that code into the first keyframe of a layer.

    change the name of the text field "yourTextFieldHere" to match yours. make sure that your text field name is in the box to the left in properties not the box to the right(var)

    trigger the score to count with counter();
     
  4. Macnoviz thread starter macrumors 65816

    Macnoviz

    Joined:
    Jan 10, 2006
    Location:
    Roeselare, Belgium
    #4
    I tried it and it almost works

    The only problem is that the action triggering the count is buried in a symbol that is not directly on the main timeline, and that doesn't seem to trigger the count. I tried it with a simple button in the main timeline, and there it worked beyond expectations. Could it be the a isn't a global variable? and if so, how do I need to change the code

    PS I checked the help, but it got worse
     
  5. electric macrumors regular

    Joined:
    Oct 14, 2002
    Location:
    San Francisco, CA
    #5
    New code

    // put your dynamic text field name below to the right of the equals sign
    var textFieldName = yourTextFieldHere
    // make a equal 0;
    var a=0;
    // give text field count of 0
    textFieldName.text = a;
    // function called (triggered) by what ever makes a score
    _global.counter = function(){
    // add new score
    a++;
    // give text field updated count
    textFieldName.text = a;
    trace(a);
    }

    // to trigger the score to count, call the function
    counter();

    -----------------------------------------------------
    just replace:
    function counter(){

    with:
    _global.counter = function(){

    ------------------------------------------------------
    now you can call counter(); from where ever
     
  6. Macnoviz thread starter macrumors 65816

    Macnoviz

    Joined:
    Jan 10, 2006
    Location:
    Roeselare, Belgium

Share This Page