2012-04-19 41 views
3

與as3仍然遇到障礙!這一次,我用addChild從我的庫中創建了一個MC實例,現在需要訪問這個孩子,就好像它是時間線上的一個正常MC,但無法使其工作。as3訪問孩子爲正常影片剪輯(gotoAndPlay等)

如果我指的是它的實例名稱,如:

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); 
+0

是的代碼請。 – Torious 2012-04-19 13:38:46

回答

3

你嘗試將其轉換爲影片剪輯?

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

或者只是'MovieClip(this.getChildByName(「mc1」))。gotoAndPlay(1);' – shanethehat 2012-04-19 14:25:02

+0

哇!這很簡單...謝謝你們! – bigtoothmedia 2012-04-19 15:06:42

+0

所以這項工作?因爲我不想...... var mc1:MovieClip = this.getChildByName(「mc1」)as MovieClip; (mc1){ \t mc1.useHandCursor = true; \t mc1.buttonMode = true; \t mc1.addEventListener(MouseEvent.CLICK,fillDriveway); } – bigtoothmedia 2012-04-20 09:19:18