View Full Version : ready to go as3 from as2 but need help
Uli
April 12th, 2009, 07:25 PM
Hello :)
So i'm ready to switch to AS3 but there is something i can't figure out is, how do you write a class that attaches a clip from a library, WITHOUT specifying the class in the export to stage thing of the clip in my library ?
All i want is to have a single line like :
var myClassIntance:MyClass = new MyClass();
And have a clip attached to the stage and the class assigned to the clip...
Any one ? :)
Cheers
valetine
April 12th, 2009, 08:14 PM
As far as i know, if you want to attach a MovieClip from the library to the stage, you need to assign a Class Name to the clip.
1. In the Library right click on the clip, select properties and type MyClip in the Linkage/Class Textbox. This will assign the Class to the Library MovieClip.
2. Define a Class who extends the MovieClip:
MyClip Class:
package {
public class MyClip extends MovieClip {
}
//Your class...
}
}
3. Write this in your Main Timeline or Main Class
var mc:MyClip = new MyClip();
addChild(mc)
Uli
April 12th, 2009, 08:21 PM
Thanks for your reply but as i said, i want to attach it WITHOUT specifying a class in the export field or even limiting an object to a class. It makes no sens to me as if i want to use the object with different classes, i can't.
Uli
April 12th, 2009, 08:34 PM
In fact i'm looking for a way to convert that as2 code to as3 :
MovieClip.prototype.isCustomClip = function(clip:MovieClip){
var clip = clip;
this.someFunction(){
//some stuff here
}
this.init = function(){
this.attachMovieClip(clip, this.getNextHighestDepth());
}
this.init();
}
function makeClassInstance(){
this.createEmtyMovieClip("someClip", this.getNextHighestDepth());
someClip.isCustomClip(libraryClip);
}
makeClassInstance()
Noahdecoco
April 12th, 2009, 11:39 PM
Whoa... I didn't know one could do that... interested in this post. Anyone has an answer to this one? :)
JonnyR
April 13th, 2009, 04:35 AM
Uli,
I'm not 100% clear on the approah you are working towards, but hopefully I can offer some advice:
First off, in AS3 you must export your Symbol in the Library as suggested by valetine, however, 99% of the time you will just be extending flash.display.MovieClip - rahter than specifying a custom class. Once you have exported your Symbol, you can create and instance of it with the new keyword.
The prototype chain is still avaliable in AS3 for applying "monkey patches (http://www.bit-101.com/blog/?p=1290)", so your AS2 code above could still be written as:
var myClip : MovieClip = new MyLibrarySymbol();
myClip.prototype.isCustomClip = function(target : MovieClip) : void
{
function nestedFunction() : void
{
};
};
However, this is considered poor Object Orientated practice. The Object Orientated approach would be to use Composition to store a reference to a DisplayObject inside a Class; you mentioned in your comment that you felt you were limiting your an Object (I presume you mean DisplayObject) to a Class - this isn't the case at all!
Whenever I have to deal with display logic (aka "View Logic") in my code, I will use Composition, in a similar fashion to this:
public class MonsterView
{
/**
* Fill colour used when drawing a rectangle on the Monster
*/
private static const FILL_COLOUR : uint = 0xFF0000;
/**
* Holds a reference to the Display Object which will represent this class
* on the Display List.
*/
private var _displayObject : DisplayObject;
/**
* Constructor.
*
* @param target A reference to the Display Object which will represent
* this Class on the Display List.
*/
public function MonsterView(target : DisplayObject)
{
_displayObject = target;
}
/**
* Draws a rectangle on the embedded DisplayObject, here we use Composition of
* The display object to keep all the code in this class.
*/
public function drawRectangleOnMonster() : void
{
_displayObject.graphics.beginFill(FILL_COLOUR);
_displayObject.graphics.drawRect(0, 0, 50, 100);
_displayObject.graphics.endFill();
}
/**
* Return a reference to the embedded DisplayObject
*/
public function getDisplayObject() : DisplayObject
{
return _displayObject;
}
}
By using Composition in this fashion - you can pass your DisplayObject, which you created from your Library via a Class Export, into mutliple different Classes and keep the logic inside the Class - NOT the DisplayObject.
Hope that helps
Jonny.
Uli
April 13th, 2009, 10:17 AM
Thanks Jonny,
My approach is that i want to add an instance to the stage from the library and assign it a class with a single like like :
var myObj:myClass = new myClass(libraryObject);
I understand half way your class because i just don't see where to pass the lib ref to it nor where it does attach it. Could you be a bit more specific please?
Thanks
Uli
JonnyR
April 13th, 2009, 01:25 PM
Uli,
Sure, sorry, I should have supplied an example of how to use the MonsterView Class which I posted, this assumes that you have a Symbol in your library called "Godzilla" that you wish to "assgn" to the MonsterView Class:
// Create an instance of the Godzilla Clip from the Library.
var godzillaClip : MovieClip = new Godzilla();
// Pass it through the MonsterView constructor.
var godzillaView : MonsterView = new MonsterView(godzillaClip);
// Add it to the state (assuming "this" expends DisplayObjectContainer)
this.addChild(godzillaView.getDisplayObject());
Hope that helps
Jonny.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.