0

本のActionscript教室のレッスン4の例に従っていますが、ステージにCLEARボタンを追加して変更しています。

すべての機能が機能するかどうかをテストするときはいつでも、ボタンの上に描画することができます。理想的には、ユーザーが描画しているとき、色はボタンの下に表示されている必要があります。

タイムラインには、背景、ボタン、アクションのレイヤーがあります。問題にすばやく対処できるように、以下のコーディングを追加しました。ありがとう!

package  {

import flash.display.MovieClip;

    public class Ellipse extends MovieClip {

        // constructor
        public function Ellipse(w:Number=40,h:Number=40,color:Number=0xff0000) {
            graphics.beginFill(color);
            graphics.drawEllipse(0, 0, w, h);
            graphics.endFill();
        }

    } // end class Ellipse

} // end package




import flash.events.MouseEvent;

var color:Number;
stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);

function startDrawing(e:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_MOVE, makeShapes);
color = Math.random() * 0xFFFFFF;
}

function stopDrawing(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, makeShapes);
}

function makeShapes(e:MouseEvent):void {
var ellipse:Ellipse = new Ellipse(10, 10, color);
stage.addChild(ellipse);
ellipse.x = mouseX;
ellipse.y = mouseY;
}


btnClear.addEventListener(MouseEvent.CLICK, clearBoard);

function clearBoard(e:MouseEvent)
{
    for (var i:int = stage.numChildren-1; i >= 1; i--) {
   stage.removeChildAt (i);
}
}
4

1 に答える 1

0

addChildディスプレイリストの一番上にアイテムを追加するので、ステージに省略記号を追加するときは、ボタンとムービーの前にアイテムを追加します。つまり、ムービー(ボタン付き)はインデックス0にありますが、シェイプはインデックス1以上で追加されます。1つの解決策は、代わりに次を使用して、映画の下にそれらを追加することaddChildAtです。

var shapeIndex:uint = 0;
function makeShapes(e:MouseEvent):void {
    var ellipse:Ellipse = new Ellipse(10, 10, color);
    stage.addChildAt(ellipse, shapeIndex); // add the shape starting at 0, and count up from there
    // this will keep the movie at the top of the stage's display list
    shapeIndex++;
    ellipse.x = mouseX;
    ellipse.y = mouseY;
}

別の解決策は、最初にコンテナクリップを作成してから、代わりにこのコンテナクリップにシェイプを追加することです。これにより、図形が表示される場所を簡単に制御できます。

var container : Sprite = new Sprite();
stage.addChildAt(container, 0); // add the container to the bottom of the stage
// now we can just easily add our shapes to the container, and they will all be behind the main movie.
function makeShapes(e:MouseEvent):void {
    var ellipse:Ellipse = new Ellipse(10, 10, color);
    container.addChild(ellipse);
    shapeIndex++;
    ellipse.x = mouseX;
    ellipse.y = mouseY;
}

そして、これは実際には画面をクリアするなどの他のことを簡単にします。コンテナクリップを削除して再作成するだけです。

function clearBoard(e:MouseEvent)
{
    stage.removeChild(container);
    container = new Sprite();
    stage.addChildAt(container, 0);
}
于 2013-03-20T21:54:03.633 に答える