9 Replies Latest reply on Jul 31, 2013 1:01 PM by kglad

    How to populate a dynamic text box with text through OOP coding

    Charine -FB buggoop Level 1

      Hi,

       

      I want to add a score to my game.

      I have a movie clip called ScoreText with dynamic text box instance called myScore (text is embedded). I exported this MC to its own class file.

       

      I want to accept values in the draw score function to add to the text. For now how do I code myScore = 0 and later if player does x-click give 10 points.

       

      Thanks

      Charine

       

      Here is the class file so long:

       

      package 

      {

         

          import flash.display.MovieClip;

          public class ScoreText extends MovieClip

          {

              public function ScoreText()

            {

              x = 1157;

              y = 606;

       

              }

         

              private function drawScore():void  //I'll call this function in my gameloop in my constructor class.

              {

                      trace("Score box can accept input");

              }

          }

      }

       

      Here is what is looks like in the game:

       

      Capture.PNG

        • 1. Re: How to populate a dynamic text box with text through OOP coding
          kglad Adobe Community Professional & MVP

          use:

           

           

           

          package 

          {

             

              import flash.display.MovieClip;

              public class ScoreText extends MovieClip

              {

                  public function ScoreText()

                {

                  x = 1157;

                  y = 606;

           

                  }

             

                  private function drawScore(s:String):void  //I'll call this function in my gameloop in my constructor class. <- not sure this makes sense. but if the gameloop is outside this class, make drawScore public

                  {

                          trace("Score box can accept input");

          myScore.text = s

                  }

              }

          }

           

          1 person found this helpful
          • 2. Re: How to populate a dynamic text box with text through OOP coding
            Charine -FB buggoop Level 1

            Ok, just to see if I am able to code correctly to change the dynamic text in the swf file, I tried this:

             

            {

             

                import flash.display.MovieClip;

                public class ScoreText extends MovieClip

                {

                    public function ScoreText()

                  {

                    x = 1157;

                    y = 606;

             

                    trace("Score box constructor read");

             

                      drawScore();

                    }

             

                    private function drawScore(s:String):void

                    {

                        trace("Score box can accept info");

                        myScore.text = s

                    }

                }

            }

             

            I get this compiler error:

             

            C:\Users\Charine\Creative Cloud Files\BugGoopFS\Classes\ScoreText.as, Line 13, Column 51136: Incorrect number of arguments.  Expected 1.

             

            Also, as this is supposed to be a score, should it say string? Am I skipping some steps here?

             

            Thanks,

            Charine

            • 3. Re: How to populate a dynamic text box with text through OOP coding
              Ned Murphy Adobe Community Professional & MVP

              The error is describng the problem fairly clearly.  You have defined the function to require an argument, but you are calling it without one...

               

                                  drawScore();  // missing the String argument

              }

               

              private function drawScore(s:String):void

               

               

              If you do not want to require the argument then do not specify it.  If it might be required sometimes but not always then define it to be null to allow for not being passed...

               

              private function drawScore(s:String=null):void

              1 person found this helpful
              • 4. Re: How to populate a dynamic text box with text through OOP coding
                Charine -FB buggoop Level 1

                Thanks Ned,

                 

                When I set the (s:Sring = null) like you suggested, it took away all the text in both my dynamic text boxes.

                 

                I then used the code below and I am able to change the text to whatever I want, e.g. "0" at the start. But it is still hard coded

                 

                I still do not understand how to write the code to change the number dynamically to increase the player's score. How do I code that?

                 

                Code working for "0" //or whatever I change it to manually

                 

                drawScore();

                     

                 

                        private function drawScore():void

                        {

                            myScore.text = "0"

                        }

                • 5. Re: How to populate a dynamic text box with text through OOP coding
                  kglad Adobe Community Professional & MVP

                  drawScore assigns text to your textfield.  it doesn't make sense to pass nothing or null to drawScore.

                   

                  you might want to pass an empty string to drawScore IF you want to clear text from it:

                   

                  drawScore("");

                   

                  and/or:

                   

                   

                    private function drawScore(s:String=""):void 

                          {

                                  trace("Score box can accept input");

                  myScore.text = s

                          }

                   

                   

                  but normally you pass something like:

                   

                  drawScore("100")

                  • 6. Re: How to populate a dynamic text box with text through OOP coding
                    Charine -FB buggoop Level 1

                    Hi kgald,

                     

                    Yes, it would not make sense to pass nothing to the textfield. What I would like to happen is that the text field must be a number that gets updated every time the player clicks the correct letter.

                     

                    I guess an event listener that adds 10 to the value everytime a letter tile is clicked and the letter tile == the active (first letter in the array). This is what I have so far, but I'm sure it is not correct /complete.

                     

                    package 

                    {

                        import flash.display.MovieClip;

                        public class ScoreText extends MovieClip

                        {

                         public var _score :int = 0;

                     

                            public function ScoreText()

                          {

                            x = 1157;

                            y = 606;

                     

                            trace("Score box constructor read");

                     

                            drawScore(100);

                     

                            }

                     

                    private function drawScore(_score : int)

                     

                            {

                                trace("Score box can accept info");

                                myScore.text = _score;

                            }

                        }

                    }

                    • 7. Re: How to populate a dynamic text box with text through OOP coding
                      kglad Adobe Community Professional & MVP

                      that's probably not what you want.

                       

                      my guess is you're going to be incrementing that score from another class.  if that's the case, drawScore should be public, not private.  also, if you're incrementing and not assigning the score, you should pick a more appropriate function name.  so, use:

                       

                       

                      package 

                      {

                          import flash.display.MovieClip;

                          public class ScoreText extends MovieClip

                          {

                           private var _score :int = 0;

                       

                              public function ScoreText()

                            {

                              x = 1157;

                                                    y = 606;

                              }

                       

                      public function incrementScore(score : int=0)

                              {

                       

                                  trace("Score box can accept info");

                      _score+=score;

                                  myScore.text = _score.toString();

                              }

                          }

                      }

                       

                       

                      and pass the amount you want to increment your score by.  for example, in whatever class where you've instantiated your ScoreText instance:

                       

                      var scoreText:ScoreText = new ScoreText();

                      .

                      .

                      .

                      if(this is the correct letter){

                      scoreText.incrementScore(10);

                      } else {

                      // if there's penalty for selecting the wrong letter:

                      scoreText.incrementScore(-5);

                      }

                      • 8. Re: How to populate a dynamic text box with text through OOP coding
                        Charine -FB buggoop Level 1

                        Thank you kglad.

                        This is exactly what I needed.