How to pass parameters components

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…

12 Replies to “How to pass parameters components”

  1. your code

    foo = ( foo == null )? bar : foo

    translates into

    if ( foo == null )
    &nbsp;&nbsp;&nbsp;&nbsp;foo = bar
    else
    &nbsp;&nbsp;&nbsp;&nbsp;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

  2. 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) {
    &nbsp;&nbsp;&nbsp;&nbsp;var mcp = MovieClip.prototype;
    &nbsp;&nbsp;&nbsp;&nbsp;MovieClip.prototype = this.prototype;
    &nbsp;&nbsp;&nbsp;&nbsp;var newmc = t.createEmptyMovieClip(name, level);
    &nbsp;&nbsp;&nbsp;&nbsp;MovieClip.prototype = mcp;
    &nbsp;&nbsp;&nbsp;&nbsp;this.apply(newmc, arguments.slice(3));
    &nbsp;&nbsp;&nbsp;&nbsp;return newmc;
    };

    Usage example:

    o = MovieClip.prototype;
    o.drawRectangle = function(x, y, w, h) {
    &nbsp;&nbsp;&nbsp;&nbsp;this.moveTo(x, y)
    &nbsp;&nbsp;&nbsp;&nbsp;this.lineTo(w, y);
    &nbsp;&nbsp;&nbsp;&nbsp;this.lineTo(w, h);
    &nbsp;&nbsp;&nbsp;&nbsp;this.lineTo(x, h);
    &nbsp;&nbsp;&nbsp;&nbsp;this.lineTo(x, y);
    };

    o = _global.Rectangle = function() {
    &nbsp;&nbsp;&nbsp;&nbsp;Rectangle.rectangles.push(this);
    };
    o.rectangles = [];
    o.prototype = new MovieClip();
    o.onKeyDown = function() {
    &nbsp;&nbsp;&nbsp;&nbsp;if(Key.getCode() == Key.SPACE) {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(Rectangle.rectangles.length && !Rectangle.mouseOver) {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rectangle.rectangles.pop().removeMovieClip();
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};
    &nbsp;&nbsp;&nbsp;&nbsp;};
    };
    Key.addListener(o);
    o = o.prototype;
    o.onPress = function() {
    &nbsp;&nbsp;&nbsp;&nbsp;this.startDrag();
    &nbsp;&nbsp;&nbsp;&nbsp;this.onMouseMove = updateAfterEvent;
    &nbsp;&nbsp;&nbsp;&nbsp;Key.removeListener(Rectangle);
    };
    o.onRelease = o.onReleaseOutside = function() {
    &nbsp;&nbsp;&nbsp;&nbsp;this.stopDrag();
    &nbsp;&nbsp;&nbsp;&nbsp;delete this.onMouseMove;
    &nbsp;&nbsp;&nbsp;&nbsp;Key.addListener(Rectangle);
    };
    o.onRollOver = function() {
    &nbsp;&nbsp;&nbsp;&nbsp;Rectangle.mouseOver = true;
    };
    o.onRollOut = function() {
    &nbsp;&nbsp;&nbsp;&nbsp;Rectangle.mouseOver = false;
    };

    o = _global.Pen = function(timeline, shape) {
    &nbsp;&nbsp;&nbsp;&nbsp;this.timeline = timeline;
    &nbsp;&nbsp;&nbsp;&nbsp;this.shape = shape;
    };
    o = o.prototype = new MovieClip();
    o.onMouseDown = function() {
    &nbsp;&nbsp;&nbsp;&nbsp;var xend, yend;
    &nbsp;&nbsp;&nbsp;&nbsp;if(this.shape.mouseOver) return;
    &nbsp;&nbsp;&nbsp;&nbsp;this.start = {x:this._xmouse, y:this._ymouse};
    &nbsp;&nbsp;&nbsp;&nbsp;this.createEmptyMovieClip(“outline”, 0);
    &nbsp;&nbsp;&nbsp;&nbsp;this.onMouseMove = function() {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.outline.clear();
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.outline.beginFill(0xFFFFFF, 0);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.outline.lineStyle(1, 0x666666, 100);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xend = this._xmouse; yend = this._ymouse;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.outline.drawRectangle(this.start.x, this.start.y, xend, yend);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.outline.endFill();
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;updateAfterEvent();
    &nbsp;&nbsp;&nbsp;&nbsp;};
    &nbsp;&nbsp;&nbsp;&nbsp;this.onMouseUp = function() {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.outline.removeMovieClip();
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var i = Rectangle.rectangles.length+1;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;///////////////////
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var mc = Rectangle.createDerivedMC(this.timeline, “rectangle” + i, i);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;///////////////////
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mc.clear();
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mc.beginFill(this.rColor()

  3. 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… =)

  4. 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?

  5. 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…

  6. Actually, that’s even cooler…

    :: tests ::

    Hrm… yours is so much smaller… but the other one is more readable. Decisions, decisions…

  7. Btw… You could write:
    if ( foo == null ) foo = bar; else foo = foo;

    in 1 line too =)

  8. 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.

  9. 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);

  10. [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>

Comments are closed.