View Full Version : Document class with subclasses
waffe
May 8th, 2007, 09:27 PM
Hi,
I have recently learned that if you want to draw to flash's stage with code in an .as file you will need to put that class name in the "Document class" option found on the properties panel.
Now what I am trying to do is build a more complex class structure for drawing to flash's stage.
So how can I have a second, third, fourth subclass of the document class and have them all be able to draw to flash's stage?
I have tried the extends class argument but I am having difficulty making it work. Any suggestion / ideas?
waffe
TheCanadian
May 8th, 2007, 09:32 PM
Not sure what you mean. You can draw with any Graphics object, which is reference by either Sprite.graphics or Shape.graphics.
For example:
var s:Shape = new Shape();
s.graphics.lineStyle(1).
s.graphics.lineTo(100, 100);
this.addChild(s);
McGuffin
May 8th, 2007, 09:57 PM
He's talking about how you create new classes from the Document class, and further and further, and still write to the main stage.
It's a problem I've been dealing with since I started plugging away with AS3 on the weekend. I've come up with 2 solutions, neither are very elegant and I'm sure I've approached this wrong (or, at least, I hope so)...
1. Declare static vars in the Document class
package {
import flash.display.Stage;
import flash.display.DisplayObject;
import flash.display.MovieClip;
public class Site extends MovieClip {
public static var stage:Stage;
public static var root:DisplayObject;
function Site() {
Site.stage = this.stage;
Site.root = this;
}
}
}
From here, you can either access the stage from "Site.stage", so when you have a new DisplayObject you need to add, you simply put...
var whatever:Sprite = new Sprite();
Site.stage.addChild(whatever);
Which sucks, because I can't figure out a way to build in a heirarchy that way.
2. This way includes making your custom classes extend a DisplayObject class (Sprite, MovieClip, something) and then adding addChild when declaring a new instance of that class, like so:
public class Site extends MovieClip {
public static var stage:Stage;
public static var root:DisplayObject;
function Site() {
Site.stage = this.stage;
Site.root = this;
var test:TestDisplayObject = new TestDisplayObject();
addChild(test);
}
and then TestDisplayObject would extend Sprite, whatever, you can use addChild() to add onto the heirarchy as such...
Stage
-- test (TestDisplayObject)
-- -- child of test
Hopefully there are more elegant solutions. I'm absolutely dying for Colin Moock's Essential Actionscript 3.0 to be released :P
waffe
May 8th, 2007, 10:10 PM
What I mean in part, "TheCanadian" is this:
Taking the code you posted, I put it in a class and saved it in a .as file like so
package {
import flash.display.MovieClip;
import flash.display.*;
public class MachineView extends Sprite {
public function MachineView() {
drawShape()
}
private function drawShape(){
trace("FOFOF")
var s:Shape = new Shape();
s.graphics.lineStyle(1)
s.graphics.lineTo(100, 100);
addChild(s);
}
}
}
Now I call the class from the fla:
var mvMachineView = new MachineView();
When I do this, nothing draws to the stage.
But, If I remove the instantiation of the MachineView class and put its name in the "Documents class" panel, and then render, there is a line drawn on the stage.
So, my problem is, I want to use many classes to make up the view of my flash project. Since all graphics must be sent through the "Document class" in flash to draw to the stage, I want to know how I can apply many subclasses to the "Document class".
waffe
McGuffin
May 8th, 2007, 10:16 PM
package {
import flash.display.MovieClip;
import flash.display.*;
import com.package.YourClass1;
import com.package.YourClass2;
public class MachineView extends Sprite {
public function MachineView() {
drawShape()
}
private function drawShape(){
trace("FOFOF")
var s:Shape = new Shape();
s.graphics.lineStyle(1)
s.graphics.lineTo(100, 100);
addChild(s);
var class1:YourClass1 = new YourClass1();
var class2:YourClass2 = new YourClass2();
addChild(1);
addChild(2);
}
}
}
waffe
May 8th, 2007, 10:22 PM
Thanks McGuffin,
I'll see if I can implement what you posted.
As for the Colin Moock's Essential Actionscript 3.0 book, I too have been awaiting its arrival.
I have a safari book account that allows me to view up to ten books online all at once. When searching for flash answers I have come across the this book. But of course they want an additional 20 dollars to view the book which is not complete - I don't know, I might have to cough up another 20 bucks, but will it have the solution?
http://safari.oreilly.com/0596526946
**Ahh I see, you import the classes you need from the MachineView perspective, instantiate them and then add them via addChild();
Thanks again McGuffin
waffe
TheCanadian
May 9th, 2007, 02:11 AM
What I mean in part, "TheCanadian" is this:
Taking the code you posted, I put it in a class and saved it in a .as file like so
package {
import flash.display.MovieClip;
import flash.display.*;
public class MachineView extends Sprite {
public function MachineView() {
drawShape()
}
private function drawShape(){
trace("FOFOF")
var s:Shape = new Shape();
s.graphics.lineStyle(1)
s.graphics.lineTo(100, 100);
addChild(s);
}
}
}
Now I call the class from the fla:
var mvMachineView = new MachineView();
When I do this, nothing draws to the stage.
You just need to add mv to the display list by calling addChild:
addChild(mv);
joshchernoff
May 9th, 2007, 09:46 PM
TheCanadian ^ beat me to it.
I think the addChild method is gonna be on of them things thats gonna be hard for some people to get around.
awflasher
May 13th, 2007, 04:58 AM
graphics holds the drawing work while you need to do more a "addChild" to fire it ~ :)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.