Forum Topic: How do I use .as file in Flash?

(125 views • 9 replies)

This topic is 1 page long.

<< < > >>
None

ElDebugger

Reply To Post Reply & Quote

Posted at: 8/29/09 04:26 AM

ElDebugger NEUTRAL LEVEL 01

Sign-Up: 04/11/08

Posts: 176

I've been writing AS2.0 into my flash timeline for a long time to code games. I finally learned the "right way" to code using OOP.

However, I want to get away from sticking my code on the timeline or inside movielcips on the stage. I want to learn to use the .as file, but I don't know how. I wrote my first class today and saved it in a .as file. I then created a new .fla and used the #include directive that points to my .as file. I get this error when I attempt to publish:

attribute used outside class

I have my classpath set properly under Publish Settings (I think). Both my FLA and my AS files are in the same directory. What does this error mean and how can I fix it? Even better, can someone post a link to a good tutorial on using .as files with flash, please. Thanks!


None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 8/29/09 05:01 AM

Afro-Ninja EVIL LEVEL 38

Sign-Up: 03/02/02

Posts: 13,467

At 8/29/09 04:26 AM, ElDebugger wrote: I've been writing AS2.0 into my flash timeline for a long time to code games. I finally learned the "right way" to code using OOP.

However, I want to get away from sticking my code on the timeline or inside movielcips on the stage. I want to learn to use the .as file, but I don't know how. I wrote my first class today and saved it in a .as file. I then created a new .fla and used the #include directive that points to my .as file. I get this error when I attempt to publish:

attribute used outside class

I have my classpath set properly under Publish Settings (I think). Both my FLA and my AS files are in the same directory. What does this error mean and how can I fix it? Even better, can someone post a link to a good tutorial on using .as files with flash, please. Thanks!

If this is the case then you shouldn't have to do anything- just use the class as you please, no include or import necessary. So if your class is GenericObject you should be able to do this from the main timeline:

var myObj:GenericObject = new GenericObject();

include statements are for directly importing sections of code, import statements are for including classes outside the classpath/ default configuration. For example when using the Point class it's necessary to type

import flash.geom.Point;

for it to work right

BBS Signature

None

ElDebugger

Reply To Post Reply & Quote

Posted at: 8/29/09 05:20 AM

ElDebugger NEUTRAL LEVEL 01

Sign-Up: 04/11/08

Posts: 176

Thanks for the reply. I understand what you're saying. Why create an external file and go through the hastle of trying to import it into flash, when I could just write my code onto the timeline.

The thing is, you can't declare classes on the timeline--at least, I don't know that you can. For example,

class SuperHero{
  var life;
  var game_level;

 (several methods[functions])...
}

This won't work. Classes can't be declared on the flash timeline. To define a class, I would have to create an object instead, using something like:

var SuperHero = new Object()

...which I DON'T want to do. It's my preference to have an FLA hold my art and an AS file hold the code. Twoseperate files.

If this makes me an inefficient coder, please let me know. I am a rookie to some extent. I hope you guys can see that I am writing my code in a different format, which I don't want to change.

class{ ... } vs. new Object()...

I appreciate the feedback. I would really like to know how to connect my AS file to my FLA so that I can define my classes outside of my file and then just call them from my FLA.


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 8/29/09 06:15 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,418

What are you yapping about? We know how to define classes. AN was saying that you don't have to use #include to use a class. Flash will find it itself when you use the class in the FLA.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

ElDebugger

Reply To Post Reply & Quote

Posted at: 8/29/09 06:41 AM

ElDebugger NEUTRAL LEVEL 01

Sign-Up: 04/11/08

Posts: 176

Oh. I misunderstood what African-Ninja said. I re-read it and it makes sense now. Thanks, Gustavo!

I have removed the "#include" from my FLA, then. It still won't work. When I am on my AS file, I click "Check Syntax" and the following error pops up:

Attribute used outside class.
     public class SuperHero{

What is this error? What is wrong with my class code. There is nothing else in my AS file but this single class called "SuperHero". Within my class are several properties, a constructor and some methods. I tried commenting-out all the code so that the only thing left is the empty class declaration...and nothing improves. The error is still there. I tried removing the "public" keyword and other errors show up.

Help, NG dudes!


None

Thomas

Reply To Post Reply & Quote

Posted at: 8/29/09 07:51 AM

Thomas LIGHT LEVEL 13

Sign-Up: 02/14/05

Posts: 2,830

I might be ignorant right now, but I haven't read anywhere that you named the class and .as file the same. In order for your class in the .as file to work properly in the .fla, you must name the .as file and class the same (eg class superhero { } would have to be in a file called superhero.as to work).


None

ElDebugger

Reply To Post Reply & Quote

Posted at: 8/29/09 08:05 AM

ElDebugger NEUTRAL LEVEL 01

Sign-Up: 04/11/08

Posts: 176

Thanks for that tip. I am new to working with AS files and I didn't know that. Let me give that a shot!


None

ElDebugger

Reply To Post Reply & Quote

Posted at: 8/29/09 08:20 AM

ElDebugger NEUTRAL LEVEL 01

Sign-Up: 04/11/08

Posts: 176

Nothing.

I'm still getting that same error:

Attribute used outside class.

None

milchreis

Reply To Post Reply & Quote

Posted at: 8/29/09 08:43 AM

milchreis DARK LEVEL 16

Sign-Up: 01/11/08

Posts: 446

At 8/29/09 08:20 AM, ElDebugger wrote: Nothing.

I'm still getting that same error:

Attribute used outside class.

http://gotoandlearn.com/play?id=30

"Even if it's just cynicism that remained to push us ahead, we'll carry on!"


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 8/29/09 08:54 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,418

'public' is an attribute. AS2 doesn't have class attributes (that is to say attributes go only inside classes). So read the error again.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


All times are Eastern Standard Time (GMT -5) | Current Time: 08:18 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!