View Full Version : Class extending Sprite
LyonB
10-19-2009, 06:40 AM
I've been tutoring a couple fellow students on ActionScript 3.0, as all we learn in school is mostly just basic button-script time-line functionality, and they wanted to get a better understanding of the language.
I'm about ready to start discussing displaying objects on the scene and the method I currently know is to have my class extend the Sprite or Movie Clip class, and then have the constructor require a sprite/movie clip value, which would be the 'this' selector on the timeline.
var myScript = new Script(this);
package {
import flash.display.Sprite;
public class Script extends Sprite{
public var myParent:Sprite;
public function (s:Sprite){
myParent = s;
}
}
}
Am I to understand that if we're going to be avoiding the timeline completely, that displaying content with the Movie Clip class is somewhat deprecated?
Also, I just want to discuss other possible methods of doing this. Is it possible to extend the Stage class? Or is that not technically possible because even the stage class needs a parent?
Basically I'm just looking for any thoughts, suggestions, or input, before I go off telling my classmates all the wrong things.
lordofduct
10-19-2009, 07:11 AM
MovieClip is for when you want or need a timeline. It actually extends Sprite.
Sprite is just like MovieClip, except that it doesn't have a timeline at all (and thusly functions like gotoAndStop, gotoAndPlay, play, stop, etc all don't exist).
You can not extend Stage. You can't even instantiate a new Stage instance. Only one Stage instance exists per flash player. Stage actually refers to the flashplayer in which the swf is being played.
There are other DisplayObjects you can extend and use.
Bitmap - displays image data from a BitmapData object.
Shape - acts as a very simple DisplayObject... it can not have children (it doesn't extend DisplayObjectContainer) so it's a much slimmer version of Sprite, with just the DisplayObject properties and a graphics instance for you to draw vector graphics too. Furthermore Shape doesn't implement InteractiveObject, which means it doesn't dispatch MouseEvents at all. This is probably the most basic of visual data you can have.
Here's a decent overview of the core display classes that you can extend and use:
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000143.html
some of these classes are useless when instantiated as is, or if extended directly. Like directly extending DisplayObject really doesn't offer much, or extending DisplayObjectContainer. Use sub versions of these classes like Sprite, MovieClip, etc.
The Little Guy
10-19-2009, 07:13 AM
Basically here is what you do...
1. Create a new fla file
2. Click on the properties and find the text field "Document Class" and enter the name of your class for example: "Main"
3. Next you create the class "Main.as" and place it in the same folder/directory as the fla file.
There are more steps, but I decided to create an example for you, it may make things easier...
This is a very basic all it does is create a square on the screen. As you look at my example, open the fla, check the doc class, find that class and look through it, then read the Box class.
Everything is commented, so if you have any questions about it let me know!
CyanBlue
10-19-2009, 07:14 AM
Um... Wasn't there one more parent above the stage??? They don't utilize it yet, but I think they use one in case when they support multiple stages... Not sure what it is called though...
lordofduct
10-19-2009, 07:23 AM
Um... Wasn't there one more parent above the stage??? They don't utilize it yet, but I think they use one in case when they support multiple stages... Not sure what it is called though...
There may be something logically up there, but as the DisplayList API goes, it is innaccessible and basically treated not there.
e.g. trace(stage.parent) returns null
When dealing with AIR or something this is a little different, but that's because you display stuff in NativeWindows which a stage is a child of. But the NativeWindow is a direct reference to the window in you OS displaying the stage that is rendering the content. And still is ignored in relation to the DisplayList API.
Bringing this back to flashplayer, the stage again technically has a parent, this could be the projector window, or the html container, etc etc. But these you get no/limited access to (considering things like ExternalInterface, etc etc, out of the scope of OPs question).
LyonB
10-19-2009, 07:46 PM
Thanks for all the input you guys, I'll be writing a blog post for the guys I'm tutoring that starts to explore that tomorrow. This is a very interesting endeavor for me because I'm learning so much more about the structure of ActionScript 3.0 than I did before.
Thanks :)
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.