0
votes

I am new to AS3. I have 2 files:

main.as:

package
{
    import flash.display.*;
    public class Main extends Sprite 
    {
        public function Main()
        {
            var f0:Flower = new Flower("rose");
            var f1:Flower = new Flower("daisy");    
        }
    }
}

Flower.as:

package 
{
    public class Flower 
    {
        public var namex:String; 
        public function Flower(name:String) 
        {
            trace("Previous public var name: " + namex);
            namex = name; 
        }
    }
}

Basically, I get 2 nulls in my output. The first one I understand why; when I first trigger

var f1:Flower = new Flower("rose");

It calls the flower's function and demands a trace of a var which has not been set yet, so therefore we get a null. After the trace it sets the var with

namex = name;

which is the value string rose. but then, when I trigger the flower daisy, I am supposed(in my opinion) to receive this message: "Previous public var: rose", because when we triggered rose we told him to set public var namex to the value which we first supplied in main(rose). So why do I get 2 nulls instead of 1 null, shouldn't the other be "Previous public var: rose";

Another thing, can I get a clear explanation about what

var f1:Flower = new Flower("rose");

does exactly? does it create an object? an instance? or is it only supplying a value(rose) to the function in flower.as?

3
Read an AS3 OOP tutorial. This is basic OOP stuff. Only static variables are shared between class instances. Your namex variable is not set to static... so it's different for every instance of the class Flower.Iansen
are these instances saved somewhere once triggered in main.as? what is the instance exactly? the value "rose"? And as I said, I am a beginner; it's not basic to me. Btw I set the public var to "public static var" and now I get an error: null.RunningFromShia

3 Answers

1
votes

Your code:
var f1:Flower = new Flower("rose");

Creates a new instance of an object of type Flower and assigns it to the variable f1. It passes the string "rose" to the constructor function of the Flower class.

As lansen says, each instance of a Flower will have it's own namex variable with an independent value.

At this point I think it would be more productive if you take a step back and learn some fundamentals of object oriented programming rather than banging your head against something you don't have a basic understanding of. This book is a pretty good place to start: Essential ActionScript 3.0.

0
votes

Look here for Basic OOP tutorials : http://tv.adobe.com/watch/actionscript-11-with-doug-winnie/objectoriented-programming-episode-45/

Doug Winnie explane it pretty easy and good way ;)

0
votes

but what does type "Flower" mean? I mean, I know there are types called "MovieClip" etc... just not sure what it does when you give the object name a type with the same name

It might have made more sense to you if your code had looked like this..?

import flash.display.*;
import Flower;

But "importing" Flower wasn't necessary here so it still works. Everything exists as code so behind the scenes imagine there is "MovieClip.as" file that Flash uses when you make a new MovieClip type variable. Advanced coders can even override the built-in functions with their own custom-made ones of same name etc..

So typical code is: variable Name : Type = new Type();

the brackets/braces in Type(); tell us its a function, so now that code will run a function with same name as Type, which in your Main.as code means do this.. "my variable f1 is made from Flower.as by the function public function Flower (name:String)". Here you are "initialising" the variable/type so that you can from now on create multiple AND "unique" instances of the same code under different reference names.

See if this example code helps you... (also shows a basic method of how to pass & retrieve information between your two classes
Main.as

package
{

import flash.display.*;

public class Main extends Sprite 
{
    public var f0 : Flower = new Flower("rose");
    public var f1 : Flower = new Flower("daisy");  

    public function Main()
    {
        Do_Something1 (); //runs a function called...
    }

    public function Do_Something1 () : void
    {
        //trace namex directly from Flower class
        trace("new f0 namex: " + f0.namex );
        trace("new f1 namex: " + f1.namex );

        //now change the contents of "namex" String in Flower class 
        f0.namex = "violet"; f1.namex = "bluebell";

        //run function from each instance of Flower class
        f0.check_renamed_Flower();
        f1.check_renamed_Flower();

    }

} //end class Main
} //end package

Flower.as

package 
{
    public class Flower 
    {
        public var namex : String; 

        public function Flower( F_name:String) : void 
        {
            trace("Previous public var name: " + F_name);
            namex = F_name; 
        }

        public function check_renamed_Flower() : void 
        {
            trace( "changed var name: " + namex );          
        }
    }
}

Hope it helps.