View Full Version : about best practices with pseudo-components
LeonardoZimbres
August 25th, 2007, 06:59 PM
Hello!
I am testing what I learned so far with actionscript, and I created a class for scrollers. But this class dont draw anything. Instead, the class uses a movieclip bypassed by parameters.
like:
var listscroll:ZScroll = new ZScroll(track2,"v",stage)
where track2 is the movieclipbut, done this way, the following code doesnot show any result:
listscroll.x = 25
only this one does it:
track2.x = 25
Someone can sugest me about a better and clear way to work with classes and movieclips? Thanks.
mathew.er
August 25th, 2007, 11:17 PM
Maybe make the ZScroll class extend some DisplayObject and add the MC parameter as its child?
Or you can implement the properties anyway...
// inside of ZScroll
public function set x ( value : Number ) : void
{
trackInstance.x = value;
}
where trackInstance is a reference to the MC you passed to the constructor.
LeonardoZimbres
August 28th, 2007, 10:50 AM
hum, I will try it. What way do you think that suits better for you?
McGuffin
August 28th, 2007, 10:54 AM
hum, I will try it. What way do you think that suits better for you?
Extending the DisplayObject is a good idea, as you won't need to do getter/setter functions for every property in the MC.
tiboO
August 28th, 2007, 11:21 AM
If you extend a DisplayObject (which is really the best thing to do, as in AS3 it really works and it's clear, not like it was in AS2), you'd be happy to use this :
package {
import flash.display.Sprite;
import flash.events.Event;
public class MyClass extends Sprite {
public function MyClass() {
super();
addEventListener(Event.ADDED_TO_STAGE, initMe, false, 0, true);
}
public function initMe(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, initMe, false);
// here you can do the 'real' instanciation
// and you are sure you can access the 'this.stage' property.
}
}
}
Also i suggest you not to directly extend DisplayObject, but rather prefer extending Shape if you dont need mouse interaction, or Sprite if you do.
LeonardoZimbres
August 28th, 2007, 02:16 PM
what the '''super();" is about?
cesig
August 28th, 2007, 02:22 PM
what the '''super();" is about?
super(); makes a call to the class's superclass constructor function. This is important because in classes that inherit from some other superclass, much of the setup and initialization logic is written in the superclass, which means it's NOT explicitly written in the inheriting class. So super() is a way to explicitly execute that logic.
So, calling super(); in a class's constructor function calls it's superclass's constructor function. In tiboO (http://www.kirupa.com/forum/member.php?u=83758)'s example, MyClass calls the Sprite class's constructor function.
LeonardoZimbres
August 28th, 2007, 02:36 PM
as3.0 looks like so different...
In the example above, what if I want to pass a parameter to that class, a movieclip? Because I can pass the movieclip as a parameter...
public function MyClass(mc:movieclip) {
super();
addEventListener(Event.ADDED_TO_STAGE, initMe, false, 0, true);
}
but if I do the real instatiation later, what is the better deal to manage my movieclip?
cesig
August 28th, 2007, 02:41 PM
Can you clarify?
When you talk about delaying instantiation, what are you talking about instantiating? And what do you mean by "manage my movieclip?"
as3.0 looks like so different...
In the example above, what if I want to pass a parameter to that class, a movieclip? Because I can pass the movieclip as a parameter...
public function MyClass(mc:movieclip) {
super();
addEventListener(Event.ADDED_TO_STAGE, initMe, false, 0, true);
}
but if I do the real instatiation later, what is the better deal to manage my movieclip?
LeonardoZimbres
August 28th, 2007, 03:00 PM
Sure. First, the class draws nothing, it uses a movieclip placed on stage. When I want to use it, I call:
var listscroll:ZScroll = new ZScroll(track2,"v",stage)
track2 is the movieclip.
So, in ZScroll class, I make it as:
private var _movieclip:MovieClip
public function MyClass(mc:movieclip) {
_movieclip = mc
super();
addEventListener(Event.ADDED_TO_STAGE, initMe, false, 0, true);
}
And so I finally initiate everyelse in the initMe function. I got it?
LeonardoZimbres
August 28th, 2007, 03:30 PM
Sorry to bother, I just want to be sure that Im programming with best practices. Thanks for all the help.
tiboO
August 28th, 2007, 04:59 PM
But imho, apart for some really particular cases, you shouldn't need to pass a MovieClip as a parameter to a class. When a class inherits from another one, it IS also its parent.
Really hard to explain... :/ let's take an example.
Imagine you want to create a hundred squares. In AS2 you would have a Main class, which creates emptyMovieClips in a container in a for loop for exemple. Then, once the empty movieclip is created, you instanciate the SquareClass, passing it the freshly created movieclip as a parameter.
class Main {
private var c:MovieClip;
private var squareInstances:Array;
private var numberOfSquare:Number = 50;
function Main() {
this.squareInstances = [];
this.c = _root.createEmptyMovieClip("container", _root.getNextHighestDepth());
this.initMe();
}
function initMe() {
for (var i:Number = 0 ; i<numberOfSquare ; i++) {
var targetMC:MovieClip = this.c.createEmptyMovieClip("square"+i, i);
this.squareInstances[i] = new SquareClass(targetMC);
}
}
}
Now for AS3 think a bit different :
Your Main class also makes a loop... but it only instanciates SquareClass (which inherits from Sprite, so it IS a sprite) and then adds it to the displayList. (of course Main also has to extend Sprite, otherwise it wouldn't have a displayList) :
package {
import flash.display.Sprite;
public class Main extends Sprite {
private var numberOfSquare:int = 50;
public function Main() {
launchSquareInstanciation();
}
public function launchSquareInstanciation():void {
for (var i:int = 0 ; i<numberOfSquare ; i++) {
var theSquare:SquareClass = new SquareClass();
theSquare.name = "square" + i.toString();
//necessary only if you need to point the instance using getChildByName()
addChild(theSquare);
//and you dont need any stock array, cause the display list is this array
// use getChildAt(index) or getChildByName(theName) to access
// the square instances. Though once again, if you use the event system
// correctly, you shouldn't need to point them directly.
}
}
}
It may look like a little detail, but even if i dunno how to explain it, it's really a huge difference between as2 and as3, and understanding this is really important. Actually i think that until as2, actionscript was a language all made of tricks and specificities. But now with as3 it really becomes a true language with all its advantages... i mean, stop bending your brain for actionscript, now it works :D ;) You have to "un-learn" a lotta things from as1 and 2...
LeonardoZimbres
August 28th, 2007, 05:57 PM
Really, I am unlearning a lot of things. I was reading the flash 8 bible when flash cs3 was released.
But in this case, I want to work with classes and movieclips placed on flash stage, and not in as run time. Its a scrollbar that I want to draw and change in flash.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.