apscience
apscience

Reputation: 7243

AS3 Flash - Adding movieclip to stage with Actionscript not working

For some reason unknown to me, I am unable to add a movieclip to stage or my document class.

This is what I have:

var testShard:blockshards = new blockshards();
addChild(testShard);

Nothing happens but the code for blockshards is executed (I see it in output).

When I manually add it in the .fla file it works.

Does anyone know why? When I replace blockshards with another movieclip it works fine.

Here's my blockshards.as:

package  {
    import flash.events.Event;
    import flash.display.MovieClip;
    public class blockshards extends MovieClip{
        var framesToDie:uint = 30;
        var xspeed:Number = 0;
        var yspeed:Number = 0;
        public function blockshards() {
            addEventListener(Event.ENTER_FRAME, onEntFrm);
            xspeed = 2 - Math.random() * 4;
            yspeed = -5;
        }
        public function onEntFrm(e:Event){
            framesToDie--;
            if(framesToDie <= 0){
                this.parent.removeChild(this);
                removeEventListener(Event.ENTER_FRAME, onEntFrm);
            }
            alpha -= 0.03;
            x += xspeed;
            y += yspeed;
            yspeed += 0.2;
            xspeed *= 0.98;
        }
    }

}

Upvotes: 1

Views: 746

Answers (4)

goliatone
goliatone

Reputation: 2174

You should not execute code directly on your display objects constructors, it is always a better idea to place a check for the sage first, other ways, weird things happen.

public function blockshards() {
     if ( stage ) _init( );
     else addEventListener(Event.ADDED_TO_STAGE, _init );
}

private function _init( e:Event = null ):void {
    removeEventListener( Event.ADDED_TO_STAGE, _init );
    addEventListener(Event.ENTER_FRAME, onEntFrm);
    xspeed = 2 - Math.random() * 4;
    yspeed = -5;
}

Same thing on your document class' constructor.

Upvotes: 2

apscience
apscience

Reputation: 7243

I moved the code to another function and it worked somehow without changing anything to do with blockshards.. very weird

Upvotes: 0

Kaken Bok
Kaken Bok

Reputation: 3395

There is nothing drawn in or attached to your movieclip. Your clip will be there but shown empty!

Edit: Apparently this is already known by you. And I don't know Flash :-) But here is a link that gives you a step by step description of exporting movieclips for ActionScript:

http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b8ea63-7fee.html

Upvotes: 0

Benny
Benny

Reputation: 2228

Hope you have used linkage Properties to create the blockshards class.

So it may be registration point problem. So please check the registration point of the MovieClip.

Upvotes: 0

Related Questions