Student
Student

Reputation: 28345

How to draw a line in ActionScript 3?

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?

Upvotes: 1

Views: 8590

Answers (2)

Patrick
Patrick

Reputation: 15717

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);
    }
}
}

Upvotes: 6

Zev
Zev

Reputation: 169

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.

Upvotes: 1

Related Questions