2017-11-07 8 views
0

ムービークリップをステージに追加するボタンがあり、別のボタンをクリックして別のボタンを追加するとボタンが離れることはありません。同様の問題が掲載されているのを見たことがありますが、答えはどれも私のためには役に立たなかったAs3再生中に現在のクリップを削除する

btnBlue.addEventListener(MouseEvent.CLICK, fl_MouseOverHandler1); 
function fl_MouseOverHandler1(evt: MouseEvent): void { 
    var ballBlue: BallBlue = new BallBlue; 
    addChild(ballBlue); 
    addChildAt(ballBlue, 0); 
    ballBlue.x = 100; //sets the x position 
    ballBlue.y = 100; //sets the y position} 

    function onClick(evt:MouseEvent):void { */to remove clip if one is playing 
     removeChildAt(0) 
    } 
} 

btnRed.addEventListener(MouseEvent.CLICK, fl_MouseOverHandler2); 
function fl_MouseOverHandler2(evt: MouseEvent): void { 
    var ballRed: BallRed = new BallRed; 
    addChild(ballRed); 
    addChildAt(ballRed, 0); 
    ballRed.x = 100; //sets the x position 
    ballRed.y = 100; //sets the y position} 
    parent.BallBlue.removeMovieClip(); 

    function onClick(evt:MouseEvent):void { */to remove clip if one is playing 
     removeChildAt(0) 
    } 
} 
+0

'のvar ballBlue定義:BallBlue、それは'のremoveChild(ballBlue)と第二の機能でアクセスできますように、 'その関数の外に、'。他のオプションは、ボールを作成するときにボールの参照を保存することです(f.ex.はArrayにプッシュします)。関数内で定義する変数は、関数への参照がない場合は、同じ関数内でしかアクセスできません。 – kaarto

+0

あなたは、あなたが必要とするものではなく、あなたが意図したように機能しない関数の中にも関数を持っています。また、あなたはボールごとに 'addChild'を2回使っていますが、最初のもの(' addChild(ballBlue); ')は私が確信している仕事をします。 – kaarto

+0

あなたのコードが明らかに間違っている場所がいくつかありますが、あなたが投稿からスクリプトを削除したら何をしようとしているのかわからないので、意図した結果を記述することから始めるべきです。 – Organis

答えて

0
btnBlue.addEventListener(MouseEvent.CLICK, btnBlue_clickHandler); 
btnRed.addEventListener(MouseEvent.CLICK, btnRed_clickHandler); 

var ballBlue: BallBlue = new BallBlue(); 
var ballRed: BallRed = new BallRed(); 

function btnBlue_clickHandler(evt:MouseEvent):void { 
    removeAllBalls(); 
    addChild(ballBlue); 
    ballBlue.x = 100; //sets the x position 
    ballBlue.y = 100; //sets the y position 
} 

function btnRed_clickHandler(evt:MouseEvent):void { 
    removeAllBalls(); 
    addChild(ballRed); 
    ballRed.x = 100; //sets the x position 
    ballRed.y = 100; //sets the y position 
} 

function removeAllBalls():void { 
    if (contains(ballBlue)) 
    { 
     removeChild(ballBlue); 
    } 
    if (contains(ballRed)) 
    { 
     removeChild(ballRed); 
    } 
} 
関連する問題