PDA

View Full Version : extended movieclip class


mcgiddin
06-04-2005, 01:34 AM
I'm trying to create a class that builds a box for me, I have thron together a quick class that draws the box, but it will only draw if I use _root.lineStyle... If I use "this" instead of "_root" I get nothing. I'm thinking I need to create a movieclip for my class but I'm not sure how to go about doing so. Below is my code and where its placed, either in the movie or an external class.

main.fla layer:1 frame:1


var box:MovieClip = new Box();
box._x = 0;
box._y = 50;
box._width = 150;
box._height = 499;
box._lineSize = 1;
box._lineColor = 0x000000;
box._lineAlpha = 100;
box._boxFill = 0xA6FFDB;
box._boxAlpha = 100;
box.draw(); // doesn't draw the box unless I use _root... in the class.

this.createEmptyMovieClip("logo_mc", this.getNextHighestDepth() );
this.createEmptyMovieClip("login_mc", this.getNextHighestDepth() );
this.createEmptyMovieClip("bodyFrame_mc", this.getNextHighestDepth() );

logo_mc.loadMovie("../logo/logo.swf");
logo_mc._x = 0;
logo_mc._y = 0;

login_mc.loadMovie("../login/login.swf");
login_mc._x = 0;
login_mc._y = 130;

bodyFrame_mc.loadMovie("../bodyFrame/bodyFrame.swf");
bodyFrame_mc._x = 150;
bodyFrame_mc._y = 50;

stop();

Box.as


import mx.events.EventDispatcher;

class Box extends MovieClip {
public var _x:Number;
public var _y:Number;
public var _width:Number;
public var _height:Number;
public var _lineSize:Number;
public var _lineColor:Number;
public var _lineAlpha:Number;
public var _boxFill:Number;
public var _boxAlpha:Number;

private var fillBox:Boolean = false;
private var _x1:Number;
private var _y1:Number;
private var _x2:Number;
private var _y2:Number;
private var debug:Boolean = true;

function Box() {
EventDispatcher.initialize(this);
if( this.debug ) {
trace( "in Box()" );
}
}

function draw() {
if( this.debug ) {
trace("now in Box.draw");
}
this._x1 = this._x;
this._y1 = this._y;
this._x2 = this._x + this._width;
this._y2 = this._y + this._height;

if( this._boxFill != NaN ) {
if( this._boxAlpha == NaN ) {
this._boxAlpha = 100;
}
this.fillBox = true;
}

if( this.debug ) {
trace( "this._x1 = " + this._x1 );
trace( "this._y1 = " + this._y1 );
trace( "this._x2 = " + this._x2 );
trace( "this._y2 = " + this._y2 );
trace( "this._boxFill = " + this._boxFill );
trace( "this._boxAlpha = " + this._boxAlpha );
trace( "this.fillBox = " + this.fillBox );
trace( "this._lineSize = " + this._lineSize );
trace( "this._lineColor = " + this._lineColor );
trace( "this._lineAlpha = " + this._lineAlpha );
}

this.lineStyle( this._lineSize, this._lineColor, this._lineAlpha ); // doesn't work unless I use "_root" in place of "this"
this.moveTo( this._x1, this._y1 ); // doesn't work unless I use "_root" in place of "this"
if( this.fillBox ) {
this.beginFill( this._boxFill, this._boxAlpha); // doesn't work unless I use "_root" in place of "this"
}
this.lineTo( this._x1, this._y2); // doesn't work unless I use "_root" in place of "this"
this.lineTo( this._x2, this._y2); // doesn't work unless I use "_root" in place of "this"
this.lineTo( this._x2, this._y1); // doesn't work unless I use "_root" in place of "this"
this.lineTo( this._x1, this._y1); // doesn't work unless I use "_root" in place of "this"
if( this.fillBox ) {
this.endFill(); // doesn't work unless I use "_root" in place of "this"
}
}

}

pan69
06-04-2005, 06:21 AM
Well, you're doing a lot of things wrong and I basicly just gave up fixing your code. Sorry to bring it like that. Instead I made a little example for you that does work and I've attached it for you.

The main thing you did wrong was that you didn't make a library symbol for your subclassed movieclip called Box. Second you used the "new" operator to create an instance of your Box class and well, things don't work that way in Flash. I suggest you read up on some material on subclassing the MovieClip class and a great way to start is here: http://www.oreilly.com/catalog/actscript/chapter/ch13.html (Get the whole book!)

Hope this helps...

mcgiddin
06-04-2005, 06:42 PM
If you couldn't tell, its my first attempt at any type of flash programming. Things are twisted enough from what I'm use to to working with that that it doesn't make logical sense to me (not saying its wrong, just saying I couldn't make sense of it). As for my code, I don't blame you for giving up. Its something that I threw together just to test what I wanted to do, and it didn't work. I can tell you that I have a new found appriciation for flash and flash programmers.

I haven't looked at your example yet, I'll let you know how things go after I look at the example. Thanks for your reponse and your time to provide an example.

Thanks,
mcgiddin

Xeef
06-04-2005, 08:36 PM
the problem

if you use "this" inside the class then "this" the class which has no manifestation
on the stage

either you use createEmptyMovieClip in the class on it's initialization where you then draw in

or
you link this class to a movieclip instance in the library
atach this whit attachMovie and use it's methode draw();

mcgiddin
06-05-2005, 10:24 PM
well I used the example given by pan69 and after some work I did get it working. There was something messed up in my main.fla file that didn't allow it to work at all, even after deleting all the code that was in it.

The thing that I'm still confused with is this: I have a "Box.as" file with a class "Box" that extends a movieclip. How in the heck does the following line know anything about my "Box" class? The closest thing I see to referencing "Box" is the "myBox_mc" in the following code. Nowhere else in any of the code does "Box" get referenced. I'm total baffled by that.


var myBox:MovieClip = _root.attachMovie("FBoxSymbol", "myBox_mc", _root.getNextHighestDepth());

snapple
06-05-2005, 10:29 PM
The movieClip you are attaching should have its class set in the library.

Right click in the library and go to linkage. Set its 'AS 2 class' to the name of your class (Box).

And change the code to:


var myBox:Box = _root.attachMovie( "FBoxSymbol", "myBox_mc", _root.getNextHighestDepth() );


I would suggest searching this forum, there are loads of posts about getting to grips with extending the MovieClip class.

mcgiddin
06-05-2005, 10:49 PM
Ok, what gives. I tried creating my own movie and using the code in the example and it doesn't freaking work! Am I missing something here? Do I need to register "Box.as" somewhere? I have "Box.as" sitting in the same folder as my .fla file, yet actionscript doesn't seem to know anything about my "Box.as" file. This is driving me freakin nuts.

mcgiddin

mcgiddin
06-05-2005, 10:53 PM
Thanks snapple,

I see I was missing something. Now things are beginning to make sense to me. I have the "Flash MX 2004 Pro" book by Sams and I've been through it dozens of times yet I've either missed it or its not mentioned in it. (I probably missed it). Anyway, thanks for your response.

mcgiddin

mcgiddin
06-05-2005, 11:25 PM
I'm Still banging my head on the desk over this one. I have the following setup for my box class yet I get compile errors when I try to run my script. What am I missing?


**Error** Scene=login, layer=action, frame=1:Line 2: Type mismatch in assignment statement: found MovieClip where Box is required.
var myBox:Box = _root.attachMovie( "mcgBoxSymbol", "myBox_mc", _root.getNextHighestDepth() );

Total ActionScript Errors: 1 Reported Errors: 1




Symbol Properties:
------------------
Name : Box
Behavior: MovieClip
Linkage
Identifier: mcgBoxSymbol
AS 2.0 Class: Box
Linkage: Export for ActionScript, Export in First Frame

Thanks,
mcgiddin

senocular
06-05-2005, 11:28 PM
attachMovie still returns a MovieClip, even though your class instance is created and is an instance of the class you specified. Its just the nature of attachMovie to return a MovieClip.

What you need to do is cast the return value coming from attachMovie to be Box - forcing the type to change from MovieClip to Box. To do this, simply wrap the attachMovie command with your class constructor like so:
var myBox:Box = Box(_root.attachMovie( "mcgBoxSymbol", "myBox_mc", _root.getNextHighestDepth() ));

mcgiddin
06-05-2005, 11:38 PM
Thanks for everyone's help. Its time for me to put this project away for the night, I'm starting to make real stupid mistakes, like using :Box instead of :MovieClip like I should have. The casting worked also btw.

thanks again,
mcgiddin