as3访问子项作为普通影片剪辑(gotoAndPlay等)

时间:2012-04-19 13:23:20

标签: actionscript-3 flash

仍然遇到了as3的障碍!这次我使用addChild从我的库中创建了一个MC的实例,现在需要访问这个孩子,好像它是时间轴上的普通MC,但无法让它工作。

如果我通过实例名称来引用它,例如:

this.getChildByName("mc1").gotoAndPlay(2);

我得到的错误是它不能对displayObject执行此操作。

如果我正常引用它,好像它只是时间轴上的MC,如:

mc1.gotoAndPlay(2);

然后它无法找到它(因为我认为它是动态创建的。

任何指导都会一如既往地受到高度赞赏......

如果您想要一些上下文,我可以弹出一些代码。

非常感谢!

凸轮

编辑:感谢您的回复 - 我不能让它工作吗?

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

编辑:请求的代码:(我想要的一个例子是mc1 onClick到gotoAndPlay)

import flash.display.*

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 nextPoint:MovieClip = new newPoint();
        addChild(nextPoint);
        nextPoint.name = "mc"+pointNo.text;
        nextPoint.x = e.target.mouseX;
        nextPoint.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);
        }
    }
}

undo.addEventListener(MouseEvent.CLICK, undoit);

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

//BTN Calls
btn1.addEventListener(MouseEvent.CLICK, fillDriveway);
btn1.addEventListener(MouseEvent.ROLL_OVER, btn1over);
btn1.addEventListener(MouseEvent.ROLL_OUT, btn1out);
btn1.buttonMode = true;
btn1.useHandCursor = true;
btn2.addEventListener(MouseEvent.CLICK, maskDrive);
btn2.addEventListener(MouseEvent.ROLL_OVER, btn2over);
btn2.addEventListener(MouseEvent.ROLL_OUT, btn2out);
btn2.buttonMode = true;
btn2.useHandCursor = true;
btn3.buttonMode = true;
btn3.useHandCursor = true;
btn3.addEventListener(MouseEvent.ROLL_OVER, btn3over);
btn3.addEventListener(MouseEvent.ROLL_OUT, btn3out);

1 个答案:

答案 0 :(得分:3)

您是否尝试将其投射到MovieClip?

var mc:MovieClip = this.getChildByName("mc1") as MovieClip;
if(mc)
{
    mc.gotoAndPlay();
}