I have the following as code in a separate file placed in the root folder of the swf file. The as file is the class for a MovieClip in the library.
When i manually place the library symbol on the stage, the MC works fine and draws the square with the nodes.
However, when I try to create a copy using ’ new sqMC();’ code, I get an error.
It would be wonderful if somebody could explain what I am doing wrong.
package {
import flash.events.MouseEvent;
import flash.events.Event;
import flash.geom.Point;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.geom.Rectangle;
import flash.geom.Matrix;
public class sqMC extends MovieClip {
public function sqMC():void {
var drawing:Shape = new Shape();
var n1:MovieClip = new node();
var n2:MovieClip = new node();
var n3:MovieClip = new node();
var n4:MovieClip = new node();
var origWidth:uint = n1.width;
var origHeight:uint = n1.height;
addChild(drawing);
addChild(n1);
addChild(n2);
addChild(n3);
addChild(n4);
n1.x=n1.y=0;
n2.x=100;
n2.y=0;
n3.x=n3.y=100;
n4.x=0;
n4.y=100;
n1.visible=false;
n2.visible=false;
n3.visible=false;
n4.visible=false;
this.addEventListener(Event.ENTER_FRAME, drawSq);
n1.addEventListener(MouseEvent.MOUSE_DOWN, dragPressHandler);
n2.addEventListener(MouseEvent.MOUSE_DOWN, dragPressHandler);
n3.addEventListener(MouseEvent.MOUSE_DOWN, dragPressHandler);
n4.addEventListener(MouseEvent.MOUSE_DOWN, dragPressHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, dragReleaseHandler);
function drawSq(event:Event):void {
drawing.graphics.clear();
drawing.graphics.lineStyle(1, 0x000000, 00);
drawing.graphics.beginFill(0x0000FF, 1);
drawing.graphics.moveTo(n1.x, n1.y);
drawing.graphics.lineTo(n2.x, n2.y);
drawing.graphics.lineTo(n3.x, n3.y);
drawing.graphics.lineTo(n4.x, n4.y);
drawing.graphics.lineTo(n1.x, n1.y);
drawing.graphics.endFill();
function dragPressHandler(event:MouseEvent):void {
event.target.startDrag(false);
}
function dragReleaseHandler(event:MouseEvent):void {
event.target.stopDrag();
}
parent.paper.addEventListener(MouseEvent.MOUSE_DOWN, hideNodes);
function hideNodes(event:MouseEvent):void {
if (event.target!=sqMC) {
n1.visible=false;
n2.visible=false;
n3.visible=false;
n4.visible=false;
}
}
function showNodes(event:MouseEvent):void {
//enableTT=false;
var p1:Point=new Point(n1.x, n1.y);
var p2:Point= new Point(n2.x, n2.y);
var p3:Point = new Point(n3.x, n3.y);
var p4:Point= new Point(n4.x, n4.y);
var nMatrix:Matrix= n1.transform.concatenatedMatrix;
nMatrix.invert();
nMatrix.concat(n1.transform.matrix);
n1.transform.matrix = nMatrix;
n2.transform.matrix = nMatrix;
n3.transform.matrix = nMatrix;
n4.transform.matrix = nMatrix;
n1.x=p1.x;
n1.y=p1.y;
n2.x=p2.x;
n2.y=p2.y;
n3.x=p3.x;
n3.y=p3.y;
n4.x=p4.x;
n4.y=p4.y;
n1.visible=true;
n2.visible=true;
n3.visible=true;
n4.visible=true;
}
}
}
}
}