Flash 8 / Actionscript 2.0 – How do I draw dynamically in a custom class.
November 12, 2006 2:51 PM   Subscribe

Flash 8 / Actionscript 2.0 – How do I draw dynamically in a custom class.

First, I’m pretty new to Actionscript and I’m getting confused going between tutorials written for different versions.

I’d like to create a class that when instantiated draws a shape on the screen and tracks various properties. In an ordinary .fla file I can do something like:
this.createEmptyMovieClip('mybox',200);
mybox.lineStyle(0,0x00ff00);
mybox.lineTo(50,20);
mybox.lineTo(70,40);
So I’ve got the basics to drawing. So why can’t I do this:
class Thing extends MovieClip{
	function Thing(){
		this.createEmptyMovieClip("mything",200);
		mything.lineTo(50,20);
		mything.lineTo(70,40);
	}
}
And instantiate it from my .fla movie. My understanding is that since Thing extends MovieClip it is a MovieClip, so I should be able to create an empty movie clip inside and then draw in that.

But it isn’t working. Can someone tell me what I’m doing wrong. I assume my problem is at a deeper level than just syntax, et cetera.

Note, I don’t want to create a symbol in the library and attach it. I want to do the whole thing programmatically.
posted by miniape to Computers & Internet (4 answers total)
 
This will work:

class Thing extends MovieClip{

function Thing(){

var mc:MovieClip = new MovieClip();
mc = this.createEmptyMovieClip("mything",200);
mc.lineStyle(1);
mc.lineTo(50,20);
mc.lineTo(70,40);

}

}

Make sure you're remembering to set the Linkage... properties in the Library for the parent movieclip. Check export for actionscript and enter Thing as the AS 2.0 class.

The problem with your code was that you weren't specifying mything's container object. But this.mything won't work, since mything isn't created until runtime. When the file is compiled, it looks at the constructor function and can't figure out what mything is. But the createEmptyMovieClip method returns an instance of whatever movieclip it creates. My code catches that instance in a variable (mc).
posted by grumblebee at 3:10 PM on November 12, 2006


At your leisure, you might want to read up on a OOP principle called Composition. It's an alternative to Inheritance. Using Composition, you don't extend MovieClip. Instead, your class creates a movieclip (you're halfway there, because you do use createEmptyMovieClip, but you're still subclassing MovieClip).

Composition has some advantages over Inheritance (not in all cases, but in many). I'm not a big fan of subclassing MovieClip, because there's no way to link a symbol to your subclass via code. You have to do that stupid linkage thing in the library. Which means you're tied to a specific class and can't change it at runtime. Composition solves that problem.
posted by grumblebee at 3:13 PM on November 12, 2006


I just got a chance to try this and it still isn't working.

This my whole swf file: motes.fla contains on frame one of layer one:

var m:Mote = new Mote();
stop();

Mote.as contains this:

class Mote extends MovieClip{
var mc:MovieClip;
function Mote(){
mc = this.createEmptyMovieClip("mymote",200);
mc.lineStyle(1,0x00ff00);
mc.lineTo(50,20);
mc.lineTo(70,40);
}
}

I've also tried it with the variable declaration inside the constructor. When I trace 'this', I get Object Object. When I trace 'mc' I get undefined and nothing happens on the stage.

I don't know what else to try. To me, code looks perfectly correct. Any ideas?
posted by miniape at 7:46 PM on November 13, 2006


This won't work because you have a class inside an fla. Classes can't be inside fla files. They must be in .as files. Since your class is called Mote, it most be is a file called Mote.as, and Mote.as must be saved in a valid classpath. You can define classpath in the Actionscript preferences (click the AS 2.0 button).
posted by grumblebee at 5:46 AM on November 14, 2006


« Older anchor tags and php   |   'Lolita' copyright status Newer »
This thread is closed to new comments.