Skip to main content
August 8, 2009
Question

Pass var value between classes

  • August 8, 2009
  • 2 replies
  • 899 views

Hi  quick question, I understand how individual classes can be used, what I am struggling with is passing info between classes

This package returns a value that shows the day number for the year basded on the date of the pc

package
{
import flash.display.MovieClip;


  public class DayOfYear extends MovieClip
  {
     public var numberDay;
    
     public function dayOfYear()
     {  
       var tmp: Date=new Date();
       var t1:Number=tmp.getTime();
       tmp.setMonth(0);
       tmp.setDate(0);
       var t2:Number=tmp.getTime();
       numberDay =  Math.floor((t1-t2)/86400000);
       trace(numberDay);
       return;
     }
    
   }

}

So if I set the pc to Jan 9th the trace displays 9

Now I want to take that 9 and use it in

package  // display quote based pc date
{
import flash.display.MovieClip;
import DayOfYear;

public class Quotes extends MovieClip

{

     var q8 = "this is quote for day 8"

     var q9 = "this is quote for day 9"

     var q10 = "this is quote for day 10"


     public function quotes()

     {

          //code that will take the day number and display the correct quote from the list of vars in a dynamic text field (called a_quote) on the stage

     }      

  }

}

Question:

How do I pass what I can trace in DayOfYear to Quotes?

Any tips or tutorials on passing variables between classes would be appreciated, also a pointer on displaying the quote in a dynamic text field on the stage would be great.

Thanks in advance for your help

This topic has been closed for replies.

2 replies

August 9, 2009

Hi Andrei1. Thanks for the explanation and examples. But I am having trouble getting this to work. It should be stright forward enough but I still can't get my head around it.

What I have done is make a fla with one mc on it.

Have linked Quotes as file to it.

All three files are in the same directory

When I test it there are no errors, the text in the Output window is '0 quotes'.

Here is the code for both Quotes and DayOfYear as files

Package for DayOfYear

package
{

public class DayOfYear
  {
    public var numberDay;
  

  public static function get getDayOfYear():int {
        var tmp: Date = new Date();
        var t1:Number = tmp.getTime();
        tmp.setMonth(0);
        tmp.setDate(0);
        var t2:Number = tmp.getTime();
        var numberDay:int =  int((t1-t2)/86400000);
        trace(numberDay + "DayOfYear");
        return numberDay;

    }
   
  }

}

Package for Quotes


package
{
import flash.display.MovieClip;


      public class Quotes extends MovieClip
      {
            private var _dayOfYear:int;


 
             public function Quotes()
               { 
              trace(_dayOfYear + "Quotes");
               }

         
               public function get DayOfYear():int { return _dayOfYear; }
       
               public function set DayOfYear(value:int):void
               {
                       _dayOfYear = value;
               }

     }
}

It seems that when I test the file it does not read or use the DayOfYear class.

Appologise if I am asking something obvious, but I cant see what I am missing.

Inspiring
August 9, 2009

First of all, you shouldn't name functions with the same names as classes - it will confuse Flash. So, you Quote class should have functions with lower cases:

public function get dayOfYear():int { return _dayOfYear; }
       
public function set dayOfYear(value:int):void
{
     _dayOfYear = value;
}

Try inside the Quote class (after you fix the above):

public function Quotes() 
{      // DayOfYear with capital case is the reference to the class
     dayOfYear = DayOfYear.getDayOfYear;
     trace(dayOfYear);

}    

August 9, 2009

Hi Andrei1,

Brilliant .  that worked.  Thanks for your time and effort

Inspiring
August 8, 2009

First of all, constructor function must be the same case as class name. You have constructors starting in lower case and class names with upper case:

 

 public class DayOfYear extends MovieClip 
 {
      public function DayOfYear() 
      {
      }
 }


public class Quotes extends MovieClip
 {
      public function Quotes() 
      {

      }
}

Second. If your class DayOfYear's responsibility is to retrieve the day, why does it need to extend MovieClip? It can perform as asked without being a MovieClip unless you plan to to write a code that will display something.

As for passing values, it depends on how you intend to use it.

In essence, DayOfYear can have a static method that returns the day. Inside DayOfYear:

 
public static function get getDayOfYear():int {
     var tmp: Date = new Date();
     var t1:Number = tmp.getTime();
     tmp.setMonth(0);
     tmp.setDate(0);
     var t2:Number = tmp.getTime();
     var numberDay:int =  int((t1-t2)/86400000);
     trace(numberDay);
     return numberDay;
}

In your Quotes class you need to create a method and property that accept the value of day. Your Quotes class may look like this:

package 
{
    import flash.display.MovieClip;

    public class Quotes extends MovieClip
    {
         private var _dayOfYear:int;
         public function Quotes() 
         {
               
         }
          
         public function get dayOfYear():int { return _dayOfYear; }
         
         public function set dayOfYear(value:int):void 
         {
             _dayOfYear = value;
          }
     }
}

Here is a fork. If you want every instance of Quotes have the same day value, you might want to just read it directly in the quotes:

 
public function Quotes() 
{
     dayOfYear = DayOfYear.getDayOfYear;
}

In addition you can set the day when you make an instance of Quotes:

 
var quotes:Quotes = new Quotes();
quotes.dayOfYear = DayOfYear.getDayOfYear;

If you make method getDayOfYear NOT static - you will need to make an instance of DayOfYear class and then call this method on this instance - not class itself.