views:

33

answers:

2

I have a file "simple.as" with the code:

lineStyle(1,0,100);
lineTo(100,100);

and I call it from my Flash Professional project using an action with the code on frame 1:

#include "simple.as"

and it works fine.

Now, I'm trying to make this same code run in a ActionScript 3 class, but with no success. My try was:

package
{
    import flash.display.MovieClip;

    public class SimpleClass extends MovieClip
    {
        public function SimpleClass()
        {
            lineStyle(1,0,100);
            lineTo(100,100);
        }
    }
}

with the following code on frame 1 action:

addChild(new SimpleClass());

But nothing is drawn.

Any hint about how to make it work?

+1  A: 

You could simply make SimpleClass the project class (i.e. make sure nothing is selected, then in the properties panel under "Publish" there's a space for class, just type SimpleClass in that box); this is a better practice than including code in frames IMO.

Zev
Oh yeah you need to include "graphics." before your method calls also, though I'm surprised it isn't giving you a compiler error for that, if that is your problem.
Zev
The Graphics is included.. ;) About make SimpleClass the project class, it worked, thanks! =D
Tom Brito
But, as a good scientist, I still need to know, why it doesn't work adding to the mxml file? I'll not be able to use every class as the project class in future projects.. ;)
Tom Brito
Realized that I need to extend SpriteVisualElement to add to an MXML.
Tom Brito
I'm not sure about why the mxml file isn't working but you don't need to use it as the project class to have access to it. Rather, you can put it in another .as file, and then create an object of that class within your project class (don't forget to addChild your new object to make it visible).
Zev
+3  A: 

Use the property graphics from your MovieClip, which is the object where you will be able to draw line, rect, etc..

package {
import flash.display.MovieClip;
import flash.display.Graphics;

public class SimpleClass extends MovieClip
{
    public function SimpleClass()
    {
        var g:Graphics=graphics;

        g.lineStyle(1,0,100);
        g.lineTo(100,100);
    }
}
}
Patrick
Huge changes from ActionScript 2!
Tom Brito
Still not working, is it correct the way I'm calling it from the frame1 action?
Tom Brito
Your answer + Zev's hint to use as a project class worked. Should have a way of choose 2 answers as right here.. XD
Tom Brito