PDA

View Full Version : Trying to get started learning about classesm,


gpacioli
10-06-2006, 03:57 PM
I copied the following into an empty as3 FLA actions frame and got a line 1 output message saying 'you can't nest packages.' Would someone help me by showing me how to make a very simple as3 file work. Then I will have something to build on.


package {
import flash.display.Sprite;
import flash.geom.Rectangle;

public class RectangleExample extends Sprite {

public function RectangleExample() {
var firstRect:Rectangle = new Rectangle();
trace(firstRect); // (x=0, y=0, w=0, h=0)
var secondRect:Rectangle = new Rectangle(1, 3, 11, 13);
trace(secondRect); // (x=1, y=3, w=11, h=13)
var thirdRect:Rectangle = new Rectangle(5, 8, 17, 19);
trace(thirdRect); // (x=5, y=8, w=17, h=19)
var isContained:Boolean = secondRect.containsRect(thirdRect);
trace(isContained); // false
}
}
}

senocular
10-06-2006, 04:01 PM
You have to place classes (packages) in external .as files. Then Flash will read them and include them in a published swf.

Actions in the Actions Panel in Flash, for AS3, relate to a custom class automatically created by Flash for that timeline when you publish. As such, it cannot contain a package or class block because that would mean you are nesting classes/packages which is not allowed.

dr_zeus
10-06-2006, 04:54 PM
So if you want that code to work in the actions frame, try removing the package and class information so that it looks like this:

var firstRect:Rectangle = new Rectangle();
trace(firstRect); // (x=0, y=0, w=0, h=0)

var secondRect:Rectangle = new Rectangle(1, 3, 11, 13);
trace(secondRect); // (x=1, y=3, w=11, h=13)

var thirdRect:Rectangle = new Rectangle(5, 8, 17, 19);
trace(thirdRect); // (x=5, y=8, w=17, h=19)

var isContained:Boolean = secondRect.containsRect(thirdRect);
trace(isContained); // false

As senocular said, Flash builds a class for you behind the scenes when you code through the Actions panel.

gpacioli
10-06-2006, 09:38 PM
Wow ! You got me going. I appreciate very much your help.

gpacioli
10-06-2006, 09:56 PM
g: How does the FLA file know that it is to reas the .as file?
s: The code that goes into the FLA file is
#include rectangle.as
g: with no semi-colon?
s: no semi-colon. What I don't know is whether it should be inside quote marks. All files should be in the same folder.
g: ok, I'll try it.

gpacioli
10-06-2006, 10:00 PM
g: correction reas should be read.
also I have named both the FLA and .as files as follows:

rectangle.fla and rectangle.as

s: that should work.

g: I'm trying it.

CyanBlue
10-06-2006, 10:03 PM
Hm... How do you do s: and g: stuff??? Interesting... :D

senocular
10-06-2006, 10:22 PM
you do not use #include with class files. You simply keep class files within the classpath specified within Flash and they will be automatically recognized.

You also do not have to have the .as files named the same as the .fla, however the class you are defining has to be the same name as the .as file (though the class name doesn't have the extension .as)

gpacioli
10-06-2006, 10:28 PM
I have the include statement wrong --- don't I?
What is the string literal I left out of the include statement?




**Error** Scene 1, Layer 'Layer 1', Frame 1 : Line 1, Column 1 : [Compiler] Error #1093: Syntax error.
# include /Users/gs/Desktop/flash/actionscript3/examples/rectangle.as
**Error** Scene 1, Layer 'Layer 1', Frame 1 : Line 1, Column 11 : [Compiler] Error #1084: Syntax error: expecting stringliteral before /Users/gs.
# include /Users/gs/Desktop/flash/actionscript3/examples/rectangle.as
**Error** Scene 1, Layer 'Layer 1', Frame 1 : Line 1, Column 20 : [Compiler] Error #1093: Syntax error.
# include /Users/gs/Desktop/flash/actionscript3/examples/rectangle.as
**Error** Scene 1, Layer 'Layer 1', Frame 1 : Line 1, Column 34 : [Compiler] Error #1093: Syntax error.
# include /Users/gs/Desktop/flash/actionscript3/examples/rectangle.as
**Error** Scene 1, Layer 'Layer 1', Frame 1 : Line 1, Column 57 : [Compiler] Error #1093: Syntax error.
# include /Users/gs/Desktop/flash/actionscript3/examples/rectangle.as
**Error** frame1 : Line 2, Column 1 : [Compiler] Error #1068: Unable to open included file: /Users/gs/Desktop/flash/actionscript3/examples/end of program.

ReferenceError: Error #1065: Variable Timeline0_4ba24a1558911db819b0393659d5c is not defined.

senocular
10-06-2006, 11:56 PM
read above ^ do not use #include

gpacioli
10-07-2006, 02:53 PM
I have gone to your site and am studying and am glad I have found a starting place. You need to be rewarded for your contributions.
I have one more question --- wish I didn't have to ask.

The question is: What code do I put into the FLA file so it will read the .as file?


http://www.senocular.com/flash/tutorials/as3withmxmlc/

senocular
10-07-2006, 03:45 PM
That tutorial doesn't cover using Flash 9, it uses the command line compiler. Some of the things in that tutorial will not work with Flash 9 (such as [Embed]), but it might help you get started with a little bit of the coding.

Adobe has a starter tutorial for Flash 9. You can read that here:
http://www.adobe.com/devnet/flash/articles/flash9_as3_preview.html

There are a few other tutorials or articles there about Flash 9 and AS3 that might be helpful as well.

Additionally theres:
http://www.kirupa.com/forum/showthread.php?t=223798

which is a tip of the day thread (no longer daily). There you will find a lot of helpful information. You might want to skim through the list in the first post and check out the tips that include "Flash 9". Most of the others refer to AS3 itself.

But specifically to your question, its not a matter of where you put it, its a matter of what you use. If you use the class you defined in your .as file in Flash, then it will be automatically included in your movie. Theres no extra effort on your part.

For example, you can use that first class you posted just by keeping it in the same directory as your fla (which is in the classpath) and putting the following code in the first frame of the movie:
var example:RectangleExample = new RectangleExample();
And you should get your traces.

Additionally, since that class extends Sprite, you can use it as the Application or Document class (should be covered in one of the tips in that last link). Then, just have nothing selected on the canvas and put RectangleExample in for the document class in the Property Inspector panel. Since its a Sprite, you'll also want to make sure you remove any code you have in your frames of the timeline (sprites dont allow that). Publish, and you should get your traces.

gpacioli
10-08-2006, 05:31 AM
I have gone to your site and am studying and am glad I have found a starting place. You need to be rewarded for your contributions.
I have one more question --- wish I didn't have to ask.

The question is: What code do I put into the FLA file so it will read the .as file?


http://www.senocular.com/flash/tutorials/as3withmxmlc/

senocular
10-08-2006, 05:44 AM
you already asked that question; the answer is in the previous post.

peptobismol
10-08-2006, 05:51 AM
ha,
this is starting to look like JAVA

and you can import your newly created class as well, i'm assuming.
like:
import myClass.doStuff

gpacioli
10-08-2006, 06:22 AM
OK --- I'm getting there --- maybe.
In my FLA actions panel line 1 I have:
var example:RectangleExample = new RectangleExample();

In my FLA Actionscript 3.0 Settings panel I have:
RectangleExample

In my .as I have (starting at line 1):
package {
import flash.display.Sprite;
import flash.geom.Rectangle;

public class RectangleExample extends Sprite {

public function RectangleExample() {
var firstRect:Rectangle = new Rectangle();
trace(firstRect); // (x=0, y=0, w=0, h=0)
var secondRect:Rectangle = new Rectangle(1, 3, 11, 13);
trace(secondRect); // (x=1, y=3, w=11, h=13)
var thirdRect:Rectangle = new Rectangle(5, 8, 17, 19);
trace(thirdRect); // (x=5, y=8, w=17, h=19)
var isContained:Boolean = secondRect.containsRect(thirdRect);
trace(isContained); // false
}
}
}

In my output file I get this message:

Error: Error #2136: The SWF file file:///Erased/Users/gs/Desktop/flash/actionscript3/senocular/lear/rectangle.swf contains invalid data.
at RectangleExample/::frame1()

senocular
10-08-2006, 06:35 AM
you dont want to use RectangleExample as your document class if you have code on the timeline (re-read my last post)

gpacioli
10-09-2006, 08:41 PM
I have followed what you have told me the best I can. I am very happy to report that I have made it work --- at last --- proudly --- and I understand so much more than I would have had it been easy. If you had not been so patient and insightful I would not have made it this far --- this year. I am truly grateful. I know you have lots to do so I suggest closing this thread. I shall go forward now. Thanks again! There is nothing for me more educational than success because it means I have learned from my failures.