bigtoothmedia
bigtoothmedia

Reputation: 201

as3 child as movieclip - won't accept functions

this follows on from my last question which I thought was answered but for some reason when I treat a child of my stage (display object) as a movieclip I can't then apply the usual functions that I want to:

var mc1:MovieClip = this.getChildByName("mc1") as MovieClip;
if(mc1) {
    mc1.useHandCursor = true;
    mc1.buttonMode = true;
    mc1.addEventListener(MouseEvent.CLICK, fillDriveway);
}

Any wisdom would greatly appreciated... and sorry for asking such a similar question as previous...

Thanks in advance.

EDIT: More code from the AS on this project for context:

import flash.display.*

ImageUploader.visible = false;

function showUploader(e:MouseEvent):void {
    ImageUploader.visible = true;
    ImageUploader.gotoAndPlay(2);
}

pic.addEventListener(MouseEvent.CLICK,addNewPoint);

var n:Number = 0;
var joinPointsArray:Array = new Array;

function addNewPoint(e:MouseEvent):void {
    n++;
    pointNo.text = String(n);
    if(n==1){
        var nextPoint:MovieClip = new mcstart();
        addChild(nextPoint);
        nextPoint.name = "mc"+pointNo.text;
        nextPoint.x = e.target.mouseX;
        nextPoint.y = e.target.mouseY;
    }else{
        var nextPoint2:MovieClip = new newPoint();
        addChild(nextPoint2);
        nextPoint2.name = "mc"+pointNo.text;
        nextPoint2.x = e.target.mouseX;
        nextPoint2.y = e.target.mouseY;
    }

    var joinPoints:MovieClip = new MovieClip();
    this.addChild(joinPoints);
    joinPointsArray.push(joinPoints);
    joinPoints.graphics.lineStyle(0.5,0xFF0000);
    joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
    for(var i:int=2; i<=n; ++i){
        joinPoints.graphics.lineTo(this.getChildByName("mc"+i).x, this.getChildByName("mc"+i).y);
    }
}

pic.addEventListener(MouseEvent.CLICK, addNewPoint);

function fillDriveway(eventObject:MouseEvent) {
    var joinPoints:MovieClip = new MovieClip();
    this.addChild(joinPoints);
    for(var p:int=0; p<(joinPointsArray.length); ++p) {
        joinPointsArray[p].alpha = 0;
    }
    this.getChildByName("mc1").alpha = 0;
    joinPoints.graphics.beginFill(0xFFFFFF, 0.7);
    joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
    for(var m:int=2; m<=n; ++m){
        joinPoints.graphics.lineTo(this.getChildByName("mc"+m).x, this.getChildByName("mc"+m).y);
    }
    joinPoints.name = "driveshape";
    filledDrive.text = "filled";
}

function undoit(eventObject:MouseEvent) {
    if(n > 0) {
        if(filledDrive.text.indexOf("filled") != -1) {
            this.removeChild(this.getChildAt(this.numChildren -1));
            filledDrive.text = "";
            }else{
            this.removeChild(this.getChildAt(this.numChildren -1));
            this.removeChild(this.getChildAt(this.numChildren -1));
            n--;
            pointNo.text = String(n);
        }
    }
}

function maskDrive(eventObject:MouseEvent) {
    if(filledDrive.text.indexOf("filled") != -1) {
        var finishA:MovieClip = new finishMC();
        this.addChild(finishA);
        finishA.x = 310;
        finishA.y = 100;
        finishA.mask = getChildByName("driveshape");
        finishA.gotoAndPlay(2);
    }
}

//BTN RollOvers
function btn1over(myEvent:MouseEvent) {
    btn1.gotoAndPlay(2);
}
function btn1out(myEvent:MouseEvent) {
    btn1.gotoAndPlay(11);
}
function btn2over(myEvent:MouseEvent) {
    btn2.gotoAndPlay(2);
}
function btn2out(myEvent:MouseEvent) {
    btn2.gotoAndPlay(11);
}
function btn3over(myEvent:MouseEvent) {
    btn3.gotoAndPlay(2);
}
function btn3out(myEvent:MouseEvent) {
    btn3.gotoAndPlay(11);
}
function undoover(myEvent:MouseEvent) {
    undo.gotoAndPlay(2);
}
function undoout(myEvent:MouseEvent) {
    undo.gotoAndPlay(11);
}

//BTN Calls
btn1HIT.addEventListener(MouseEvent.CLICK, fillDriveway);
btn1HIT.addEventListener(MouseEvent.ROLL_OVER, btn1over);
btn1HIT.addEventListener(MouseEvent.ROLL_OUT, btn1out);
btn1HIT.buttonMode = true;
btn1HIT.useHandCursor = true;
btn2HIT.addEventListener(MouseEvent.CLICK, maskDrive);
btn2HIT.addEventListener(MouseEvent.ROLL_OVER, btn2over);
btn2HIT.addEventListener(MouseEvent.ROLL_OUT, btn2out);
btn2HIT.buttonMode = true;
btn2HIT.useHandCursor = true;
btn3HIT.buttonMode = true;
btn3HIT.useHandCursor = true;
btn3HIT.addEventListener(MouseEvent.ROLL_OVER, btn3over);
btn3HIT.addEventListener(MouseEvent.ROLL_OUT, btn3out);
btn3HIT.addEventListener(MouseEvent.CLICK, showUploader);
undoHIT.addEventListener(MouseEvent.CLICK, undoit);
undoHIT.addEventListener(MouseEvent.ROLL_OVER, undoover);
undoHIT.addEventListener(MouseEvent.ROLL_OUT, undoout);
undoHIT.buttonMode = true;
undoHIT.useHandCursor = true;

var mc1:MovieClip = this.getChildByName("mc1") as MovieClip;
if(mc1) {
    mc1.useHandCursor = true;
    mc1.buttonMode = true;
    mc1.addEventListener(MouseEvent.CLICK, fillDriveway);
}

Upvotes: 0

Views: 2051

Answers (2)

bigtoothmedia
bigtoothmedia

Reputation: 201

Wahoo! Figured it out!

By popping

var mc1:MovieClip = this.getChildByName("mc1") as MovieClip;

at the end of my AS I was referring to the child "mc1" before it existed - it isn't created until the user clicks somewhere on the "pic" movieclip. So the solution was to bung my actions for "mc1" (including declaring it as a MovieClip) into a separate function:

function createstartEndMC():void {
    var startEnd:MovieClip = (this.getChildByName("mc1")) as MovieClip;
    startEnd.useHandCursor = true;
    startEnd.buttonMode = true;
    startEnd.addEventListener(MouseEvent.CLICK, fillDriveway);
}

and then to call this function AFTER "mc1" child is created:

function addNewPoint(e:MouseEvent):void {
    n++;
    pointNo.text = String(n);
    if(n==1){
        var nextPoint:MovieClip = new mcstart();
        addChild(nextPoint);
        nextPoint.name = "mc"+pointNo.text;
        nextPoint.x = e.target.mouseX;
        nextPoint.y = e.target.mouseY;
        createstartEndMC();
    }

Finally it works and "mc1" (or "startEnd" as I call it once it's created and made into an MC) finally behaves as a normal timeline MC should!

I'm so happy - thanks for all your guidance!

Cam

Upvotes: 0

giannis christofakis
giannis christofakis

Reputation: 8321

Are you sure the movieclip is placed on stage or actually converted to movieclip? Try stage.getChildByName(). Where did you place this code? Inside a frame or inside a main document class? To be sure you can check the childs are added on stage and see what their names are. You can use this code

        for ( var i :int = 0; i < this.numChildren; i++ )
        {
            babe = this.getChildAt( i );
            if ( babe is MovieClip)  {
                trace( babe.name);
            }
        }

I've also seen this, not sure if it works.

if (stage.contains(mc1)) {

}

Upvotes: 1

Related Questions