as3 child as movieclip - 不接受函数

时间:2012-04-20 11:36:40

标签: actionscript-3 flash

这是从我最后一个问题得出的结论,我认为这个问题得到了回答,但由于某些原因,当我将舞台的一个孩子(显示对象)视为一个动画片段时,我无法应用我想要的常用功能:

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

任何智慧都会非常感激...并且对于提出与之前类似的问题感到遗憾......

提前致谢。

编辑:此项目中来自AS的更多代码用于上下文:

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);
}

2 个答案:

答案 0 :(得分:1)

您确定将动画片段放置在舞台上还是实际转换为动画片段? 试试stage.getChildByName()。你把这个代码放在哪里了?在框架内或主文档类内?为了确保您可以检查孩子们是否已添加到舞台上并查看他们的名字。 您可以使用此代码

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

我也看过这个,不确定它是否有效。

if (stage.contains(mc1)) {

}

答案 1 :(得分:0)

嗬!想出来了!

弹出

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

在我的AS结束时我指的是孩子“mc1”才存在 - 直到用户点击“pic”动画片段的某个地方才会创建它。因此,解决方案是将我对“mc1”(包括将其声明为MovieClip)的操作添加到单独的函数中:

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

然后在创建“mc1”子项后调用此函数:

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();
    }

最后它的工作原理和“mc1”(或称为“startEnd”,因为我称它为创建并制成MC后)最终表现为MC应该正常的时间线!

我很高兴 - 感谢您的所有指导!

凸轮