Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

trying to understand classes having a hard time

Community Beginner ,
Jan 01, 2019 Jan 01, 2019

I think I understand document class and the class that is directly connected to a movieClip.  So I have a list of questions about other classes.   In order to show this I have a sample code that I edited and stole from a tutorial.  It is not connected to a movieclip or the document class.  I did nothing but get a new .as file and put the code below and have it in the same folder as the swf.  The code for the SWF is the bottom code which communicates with the .as file.

1. why does  ColorThis(mov2,2222222) not work?

but this work

var author:Name = new Name();

author.SetFirstName("Charles");

author.SetLastName("Dickens");

trace(author.GetFullName());

but if I put ColorThis(mov2,2222222) document class it works?  but it doesn't seem that useful there because

it would clutter up too fast

Call to a possibly undefined method ColorThis through a reference with static type Class.

I tried this too

//Name.ColorThis(mov2,2222222)

2.  why wouldn't I put import Name at the top of the timeline

and if I move out of the main folder why won't import myfolder.Name work?

3. can I add multiple classes to a single MovieCLip and how would I go about this?

4.  Is oop really better?

package  {

import flash.display.MovieClip;

import flash.geom.ColorTransform;

import flash.display.MovieClip;

public class Name2 {

private var firstName:String;

private var lastName:String;

public function Name2() {

// constructor code

}

public function SetFirstName(val:String):void

{

firstName = val;

}

public function SetLastName(val:String):void

{

lastName = val;

}

public function GetFullName():String

{

return firstName + " " + lastName;

}

public function ColorThis(Pmov:MovieClip,mycolor:Number):void {

var newColorTransform_2:ColorTransform = new ColorTransform();

newColorTransform_2.color = mycolor;

Pmov.transform.colorTransform = newColorTransform_2;

}

}

}

//////////////////////////////////////////////////////////

/////this code is from the timeline/////////////////////////

//import Name

var author:Name = new Name();

author.SetFirstName("Charles");

author.SetLastName("Dickens");

trace(author.GetFullName());

//Name.ColorThis(mov2,2222222)

ColorThis(mov2,2222222)

397
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 02, 2019 Jan 02, 2019

Hi.

When you declare a variable or function inside of a class block, you're adding a property or method to an instance of that class. For example:

[Person.as]

package

{

     public class Person

     {

          private var _firstName:String = "";

          private var _lastName:String = "";

          public function Person()

          {

          }

          public function getFirstName():String

          {

               return _firstName;

          }

          public function setFirstName(name:String):void

          {

               _firstName = name;

          }

          public function getLastName():String

          {

               return _lastName;

          }

          public function setLastName(name:String):void

          {

               _lastName = name;

          }

          public function getFullName():String

          {

               return _firstName + " " + _lastName;

          }

     }

}

[FLA code]

var person1:Person = new Person();

person1.setFirstName("Nathan");

person1.setLastName("Drake");

trace(person1.getFirstName());

trace(person1.getLastName());

trace(person1.getFullName());

var person2:Person = new Person();

person2.setFirstName("Lara");

person2.setLastName("Croft");

trace(person2.getFirstName());

trace(person2.getLastName());

trace(person2.getFullName());

So you have to instanciate a class before using its methods and properties.

But if you want methods and properties that belong to the class itself and not to a specific instance, you have to use the static keyword.

[Person.as]

package

{

     public class Person

     {

          private var _firstName:String = "";

          private var _lastName:String = "";

          private static var _peopleCount:uint = 0; // NEW

          public function Person()

          {

               _peopleCount++; // NEW

          }

          public function getFirstName():String

          {

               return _firstName;

          }

          public function setFirstName(name:String):void

          {

               _firstName = name;

          }

          public function getLastName():String

          {

               return _lastName;

          }

          public function setLastName(name:String):void

          {

               _lastName = name;

          }

          public function getFullName():String

          {

               return _firstName + " " + _lastName;

          }

         

          // NEW

          public static function numberOfPeople():uint

          {

               return _peopleCount;

          }

     }

}

[FLA code]

var person1:Person = new Person();

person1.setFirstName("Nathan");

person1.setLastName("Drake");

trace(person1.getFirstName());

trace(person1.getLastName());

trace(person1.getFullName());

trace(Person.numberOfPeople()); // NEW

var person2:Person = new Person();

person2.setFirstName("Lara");

person2.setLastName("Croft");

trace(person2.getFirstName());

trace(person2.getLastName());

trace(person2.getFullName());

trace(Person.numberOfPeople()); // NEW

Now, with this new static function numberOfPeople, I can keep track of how many people have been created. It makes sense to set this method as static because the number of instances is an information that regards to the whole class not to a specific instance.

For comparison, take the default Math class and its methods: Math.random, Math.abs, and so on. You don't need to instanciate the Math class to use them. Because they are static.

About importing, it's not mandatory by default to import classes in the same folder. But for classes in subfolders, you have to use an import statement. Just remember to set your code respositories in the AS3 settings.

In AS3, objects can only extend one class. You can add multiple interfaces, though.

And there is a debate if OOP is the best approach or not. What I like to do is to use a mix of procedural and OOP approaches for simple situations and a more involved OOP implementation for larger and more complex projects.

If you want a very deep and detailed explanation about OOP and other AS3 stuff, please have a look at this series by Dru Kepple:

https://code.tutsplus.com/series/as3-101--active-7395

I hope this helps.

Regards,

JC

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 02, 2019 Jan 02, 2019

that is good info, thank you very much.  I get it I have to call out aurther.Name.ColorThis(mov2,2222222).  I will have to look at interfaces.  I have never even heard of them.   For now being able to call and move a method not tied to a particular object will be valuable.  I don't think I am ready to jump full on into OOP.  I like storing my as3 in different layers and jumping through them really quick and not having to open and save new files every time I want to change something or compile

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 02, 2019 Jan 02, 2019
LATEST

Nice.

It takes some time to get used to it. But I'm confident you're gonna learn pretty fast.

There are some kind of products, like games, that OOP is extremely necessary. It can be a nightmare to code a game without a proper structure.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines