as3 以普通影片剪辑的形式访问子项(gotoAndPlay 等)

Posted

技术标签:

【中文标题】as3 以普通影片剪辑的形式访问子项(gotoAndPlay 等)【英文标题】:as3 access child as normal movie clip (gotoAndPlay etc.) 【发布时间】:2012-05-01 00:36:33 【问题描述】:

仍然遇到 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 to 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】:

您是否尝试将其投射到影片剪辑中?

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

    mc.gotoAndPlay();

【讨论】:

或者只是MovieClip(this.getChildByName("mc1")).gotoAndPlay(1); 所以这应该有效吗?因为我不想... var mc1:MovieClip = this.getChildByName("mc1") as MovieClip;如果(mc1) mc1.useHandCursor = true; mc1.buttonMode = true; mc1.addEventListener(MouseEvent.CLICK, fillDriveway); 什么不起作用?是不是进入 if 块?单击影片剪辑时是否未触发事件?如果事件处理程序添加到 mc1,您是否尝试调试并查看 mc1 是否?

以上是关于as3 以普通影片剪辑的形式访问子项(gotoAndPlay 等)的主要内容,如果未能解决你的问题,请参考以下文章

在 AS3 中向子影片剪辑添加/删除事件侦听器

如何使用 as3 同时访问所有影片剪辑(以及影片剪辑中的影片剪辑......)?

删除所有数组影片剪辑上的子项

用flash as3语言如何将影片剪辑存入一个数组以及如何访问?

Flash AS3 从影片剪辑访问全局变量

As3 如何翻转影片剪辑以面对运动方向?