1

私はいくつかのAS3を持っています(私はこれに慣れていません!)そしてコードを機能させる唯一の方法は非常にリテラルでそれを何度も複製することでしたので、誰かがよりエレガントなソリューションを提供できるかどうか疑問に思いましたか?

私はそれから学ぶことを約束します-forループからの整数を使用して名前を構築しようとしたときに動的ムービークリップがインスタンス名によって認識されないため、forループを機能させることができませんでした(それが理にかなっている場合) ?)..。

これが私の(かさばる)コードです。ここにある6つだけでなく、50回のクリックにも対応できるようにしたいと思います。

import flash.display.*
pic.addEventListener(MouseEvent.CLICK,addNewPoint);

var n:Number = 0;

function addNewPoint(e:MouseEvent):void {
    n++;
    pointNo.text = String(n);
    // Create a new MovieClip
    var nextPoint:MovieClip = new newPoint();

    // Add the new MovieClip to the MainTimeline
    // so that we can see it.
    addChild(nextPoint);
    nextPoint.name = "mc"+pointNo.text;

    // Set the location of the new MovieClip
    nextPoint.x = e.target.mouseX;
    nextPoint.y = e.target.mouseY;

    //Draw the line between points
    if (n == 1) {
        var joinPoints:MovieClip = new MovieClip();
        this.addChild(joinPoints);
        joinPoints.graphics.lineStyle(0.5,0xFF0000);
        joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
    }else if (n == 2) {
        var joinPoints:MovieClip = new MovieClip();
        this.addChild(joinPoints);
        joinPoints.graphics.lineStyle(0.5,0xFF0000);
        joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
    }else if (n == 3) {
        var joinPoints:MovieClip = new MovieClip();
        this.addChild(joinPoints);
        joinPoints.graphics.lineStyle(0.5,0xFF0000);
        joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
    }else if (n == 4) {
        var joinPoints:MovieClip = new MovieClip();
        this.addChild(joinPoints);
        joinPoints.graphics.lineStyle(0.5,0xFF0000);
        joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc4").x, this.getChildByName("mc4").y);
    }
    else if (n == 5) {
        var joinPoints:MovieClip = new MovieClip();
        this.addChild(joinPoints);
        joinPoints.graphics.lineStyle(0.5,0xFF0000);
        joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc4").x, this.getChildByName("mc4").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc5").x, this.getChildByName("mc5").y);
    }
    else if (n == 6) {
        var joinPoints:MovieClip = new MovieClip();
        this.addChild(joinPoints);
        joinPoints.graphics.lineStyle(0.5,0xFF0000);
        joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc4").x, this.getChildByName("mc4").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc5").x, this.getChildByName("mc5").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc6").x, this.getChildByName("mc6").y);
    }
}

pic.addEventListener(MouseEvent.CLICK, addNewPoint);

function fillDriveway(eventObject:MouseEvent) {
    var joinPoints:MovieClip = new MovieClip();
    this.addChild(joinPoints);
    if (n == 3) {
        var joinPoints:MovieClip = new MovieClip();
        this.addChild(joinPoints);
        joinPoints.graphics.lineStyle(0.5,0xFFFFFF);
        joinPoints.graphics.beginFill(0xFFFFFF, 0.2);
        joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
        this.getChildByName("mc1").visible=false;
        this.getChildByName("mc2").visible=false;
        this.getChildByName("mc3").visible=false;
    }else if (n == 4) {
        var joinPoints:MovieClip = new MovieClip();
        this.addChild(joinPoints);
        joinPoints.graphics.lineStyle(0.5,0xFFFFFF);
        joinPoints.graphics.beginFill(0xFFFFFF, 0.2);
        joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc4").x, this.getChildByName("mc4").y);
        this.getChildByName("mc1").visible=false;
        this.getChildByName("mc2").visible=false;
        this.getChildByName("mc3").visible=false;
        this.getChildByName("mc4").visible=false;
    }else if (n == 5) {
        var joinPoints:MovieClip = new MovieClip();
        this.addChild(joinPoints);
        joinPoints.graphics.lineStyle(0.5,0xFFFFFF);
        joinPoints.graphics.beginFill(0xFFFFFF, 0.2);
        joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc4").x, this.getChildByName("mc4").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc5").x, this.getChildByName("mc5").y);
        this.getChildByName("mc1").visible=false;
        this.getChildByName("mc2").visible=false;
        this.getChildByName("mc3").visible=false;
        this.getChildByName("mc4").visible=false;
        this.getChildByName("mc5").visible=false;
    }else if (n == 6) {
        var joinPoints:MovieClip = new MovieClip();
        this.addChild(joinPoints);
        joinPoints.graphics.lineStyle(0.5,0xFFFFFF);
        joinPoints.graphics.beginFill(0xFFFFFF, 0.2);
        joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc4").x, this.getChildByName("mc4").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc5").x, this.getChildByName("mc5").y);
        joinPoints.graphics.lineTo(this.getChildByName("mc6").x, this.getChildByName("mc6").y);
        this.getChildByName("mc1").visible=false;
        this.getChildByName("mc2").visible=false;
        this.getChildByName("mc3").visible=false;
        this.getChildByName("mc4").visible=false;
        this.getChildByName("mc5").visible=false;
        this.getChildByName("mc6").visible=false;
    }
}

btn1.addEventListener(MouseEvent.CLICK, fillDriveway);

function undoit(eventObject:MouseEvent) {
    if(n > 0) {
        this.removeChild(this.getChildAt(this.numChildren -1));
        this.removeChild(this.getChildAt(this.numChildren -1));
        n--;
        pointNo.text = String(n);
    }
}

undo.addEventListener(MouseEvent.CLICK, undoit);

**編集 - - - - - - - - - - - - - - - - - - - - - -

Eduardoのおかげで-完成したコードは次のようになります:**

import flash.display.*

pic.addEventListener(MouseEvent.CLICK,addNewPoint);

var n:Number = 0;

function addNewPoint(e:MouseEvent):void {
    n++;
    pointNo.text = String(n);
    // Create a new MovieClip
    var nextPoint:MovieClip = new newPoint();

    // Add the new MovieClip to the MainTimeline
    // so that we can see it.
    addChild(nextPoint);
    nextPoint.name = "mc"+pointNo.text;

    // Set the location of the new MovieClip
    nextPoint.x = e.target.mouseX;
    nextPoint.y = e.target.mouseY;

    //Draw the line between points
    var joinPoints:MovieClip = new MovieClip();
    this.addChild(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);
    joinPoints.graphics.lineStyle(0.5,0xFFFFFF);
    joinPoints.graphics.beginFill(0xFFFFFF, 0.2);
    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);
    }
}

btn1.addEventListener(MouseEvent.CLICK, fillDriveway);
4

1 に答える 1

4

次のようにして、ifを単純化できます。

var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFF0000);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
for(var i:int=2; i<=numpoints; ++i){
  joinPoints.graphics.lineTo(this.getChildByName("mc"+i).x, this.getChildByName("mc"+i).y);
}

これで最初のifのセットが処理され、2番目のセットも同様に処理できます。

于 2012-04-18T07:44:54.043 に答える