PDA

View Full Version : As3 + Packages... driving me mad



Grofit
September 24th, 2007, 06:51 AM
Hello!

I got my hands on Flash 9 the other day so i started trying to write some classes to do some similar stuff to what i did in AS2. I made a project, made 2 .as files:

// flashtest1.as
package flashtest.base
{
// firstclass
}

// flashtest2.as
package flashtest.main
{
// secondclass
}

Then i thought, ok those seem to be ok lets add this to my .fla file so i open that up and do:

import flashtest.main.*;

Keeps saying "Definition flashtest.main could not be found", so i thought ok, lets just do it directly to the class in there:

import flashtest.main.secondclass;

Still same error, so at this point i thought ok, lets go out on the net and see if anyone can show me how to use .as files classes in my .fla file. After 2 hours of looking im still stumped. Is there some magical way to link .as files into .fla files these days?

Any advice would be great!

- Edit -

Just realised that im being kinda vauge there, so just some more information to help you help me :D

- The classes defined in the packages are all public with valid constructors
- The files are all in the same folder on the computer
- The case is the same on the filenames/classnames/package names
- They are all within a project file and they are the only files in it

All the tutorials and examples i see online dont seem to use .fla files to run the code they do it all internally in the .as by dynamically creating the items they need, but im going to need to write external .as files and use them in .fla files...

Hope that helps a bit more :D

Templarian
September 24th, 2007, 07:36 AM
if the class is in your project you don't need to import or anything to them.

Grofit
September 24th, 2007, 08:01 AM
if the class is in your project you don't need to import or anything to them.

Thats what i thought, as AS2 used to do things that way as long as the names matched up. However if i dont import anything it just says:

"Type was not found"

soulwire
September 24th, 2007, 08:48 AM
The files are all in the same folder on the computer


The location of the .as files should reflect the package names, and each .as file should define only one class. so a class 'MyTest' in package 'flashtest.main' should be MyTest.as in folder 'main' which is inside folder 'flashtest' relative to your classpath.

Grofit
September 24th, 2007, 09:00 AM
AH!

Will go and move things around and see if that makes things worky!

OK getting somewhere now, can i clear up a few things...

if my hierarchy is:

root
|
|- core
|--|
|--|- classes
|-----|
|-----|- testy.as
|-----|- foo.as
|
|- stuff.fla

Does that mean that my package would be "core.classes"

and my classes would be testy and foo i nthe relevent .as files. Im just wondering if i can have multiple class files (.as) in the same package or if i have to make a package for each file as i remember reading that you cant make more than 1 public class per package, but wasnt sure if that was per .as file or per package...

hakukaji
September 24th, 2007, 09:28 AM
you can have as many classes in your packages as you want. you can even have the same name class in different packages.

If you have a domain name the best idea is to use it backwards for package naming.

i.e. com.google.test, com.google.foo, etc...

soulwire
September 24th, 2007, 10:00 AM
you can have as many classes in your packages as you want. you can even have the same name class in different packages.

If you have a domain name the best idea is to use it backwards for package naming.

i.e. com.google.test, com.google.foo, etc...

Yes, thats right! Packages are just useful groupings of related classes (I did mean one class per .as, but as many classes per package as you need), and you can have the same names of classes in different packages but avoiding reserved keywords is advisable!

Your example structure looks fine :)

Grofit
September 24th, 2007, 11:17 AM
Yeah cheers guys it works as i would expect now!

I still dont like actionscript much but maybe i just dont know what im doing with it, making me dislike it a bit... im gonna make a new thread for a new question i have!

hakukaji
September 24th, 2007, 11:20 AM
I still dont like actionscript much but maybe i just dont know what im doing with it, making me dislike it a bit... im gonna make a new thread for a new question i have!

Stick with it, you will like it, especially since it's becoming a much stronger language. Object-Oriented Programming can seem daunting at first, but it will make sense once you get the hang of it. I remember when packages in java used to make me want to kill Sun, but now I wish all programming languages had them.

soulwire
September 24th, 2007, 01:24 PM
I second that ! :)

Grofit
September 24th, 2007, 03:53 PM
Well im still undecided as i have lots more questions before im *happy* with actionscript.... but they arew for different threads...

Aquilonian
September 24th, 2007, 07:53 PM
I second that ! :)
I third that