You cannot do:
<pre><code>my_mc = new MovieClip();</code></pre>
Nor can you do:
<pre><code>my_mc = new FUIComponentClass(true, “my_txt”);</code></pre>
So how do you initialize your components just like a class? Furthermore, how do you write them to accept such a usage? Read on…
Comments
12 responses to “How to pass parameters components”
your code
foo = ( foo == null )? bar : foo
translates into
if ( foo == null )
foo = bar
else
foo = foo
there’s no need for the extra assignment. use the if-then construct instead of the conditional operator in this case.
regards,
eokyere
Here’s something I’ve been using:
var o = Function.prototype;
// Modified version of a function
// created by Casper Schuirink
o.createDerivedMC = function(t, name, level) {
var mcp = MovieClip.prototype;
MovieClip.prototype = this.prototype;
var newmc = t.createEmptyMovieClip(name, level);
MovieClip.prototype = mcp;
this.apply(newmc, arguments.slice(3));
return newmc;
};
Usage example:
o = MovieClip.prototype;
o.drawRectangle = function(x, y, w, h) {
this.moveTo(x, y)
this.lineTo(w, y);
this.lineTo(w, h);
this.lineTo(x, h);
this.lineTo(x, y);
};
o = _global.Rectangle = function() {
Rectangle.rectangles.push(this);
};
o.rectangles = [];
o.prototype = new MovieClip();
o.onKeyDown = function() {
if(Key.getCode() == Key.SPACE) {
if(Rectangle.rectangles.length && !Rectangle.mouseOver) {
Rectangle.rectangles.pop().removeMovieClip();
};
};
};
Key.addListener(o);
o = o.prototype;
o.onPress = function() {
this.startDrag();
this.onMouseMove = updateAfterEvent;
Key.removeListener(Rectangle);
};
o.onRelease = o.onReleaseOutside = function() {
this.stopDrag();
delete this.onMouseMove;
Key.addListener(Rectangle);
};
o.onRollOver = function() {
Rectangle.mouseOver = true;
};
o.onRollOut = function() {
Rectangle.mouseOver = false;
};
o = _global.Pen = function(timeline, shape) {
this.timeline = timeline;
this.shape = shape;
};
o = o.prototype = new MovieClip();
o.onMouseDown = function() {
var xend, yend;
if(this.shape.mouseOver) return;
this.start = {x:this._xmouse, y:this._ymouse};
this.createEmptyMovieClip(“outline”, 0);
this.onMouseMove = function() {
this.outline.clear();
this.outline.beginFill(0xFFFFFF, 0);
this.outline.lineStyle(1, 0x666666, 100);
xend = this._xmouse; yend = this._ymouse;
this.outline.drawRectangle(this.start.x, this.start.y, xend, yend);
this.outline.endFill();
updateAfterEvent();
};
this.onMouseUp = function() {
this.outline.removeMovieClip();
var i = Rectangle.rectangles.length+1;
///////////////////
var mc = Rectangle.createDerivedMC(this.timeline, “rectangle” + i, i);
///////////////////
mc.clear();
mc.beginFill(this.rColor()
refer
http://eostudios.port5.com/blog/archives/000008.html
-eokyere-
Neat! Great job, Eokyere! That’s exactly what I want to be able to do. I just think this should be a native Flash feature… =)
Yeah, but isn’t:
<pre>if ( foo == null )
foo = bar
else
foo = foo</pre>
4 lines instead of 1? That’s the only difference right, or did I miss something?
Yeah… that’s the only difference… What okyere meant is that there’s no need to “foo = foo”, so…
if(foo == null) foo = bar;
would be better although i don’t see any problems with your code…
Actually, that’s even cooler…
:: tests ::
Hrm… yours is so much smaller… but the other one is more readable. Decisions, decisions…
Btw… You could write:
if ( foo == null ) foo = bar; else foo = foo;
in 1 line too =)
But let’s say foo already equals “bar”. If that’s the case, there is no point for the else statement, or is there?
Either way, I love how concise this is getting. Micro-coding… rock.
Naw… mine is readble…
Compare to this lol…
var test1;
for(;test1 == null; test1 = 1);
trace(test1);
var test2= 1;
for(;test2 == null; test2 = 1);
trace(test2);
wtf… I’ll stick to my if thens, then, hehe!
[quote]So how do you initialize your components just like a class? [/quote]
I don’t inherit with:
blah.prototype = new MovieClip()
and I block the constructor to be called from Object.registerClass with ASNew()
then I declare a setter in the MovieClip.prototype which gonna instantiate the “special” class
that way I can instanciate class linked to librarie movieclip that way:
setRect( “myRect”, 0, 0, 100, 200 );
passing directly the arguments to the constructors in 1 line,
no attachMovie, init method or other non-logical stuff
more info here (in french):
<a href=”news://flashcodeurs.dyndns.org/jrCs3tEEDHA.3336@YAMA”>[FMX] component in your AS</a>