0

ドロップ場所がfishBowlクラスオブジェクトであるかどうかを確認し、ドラッグされたオブジェクトをfishBowlオブジェクトに配置するか、fishBowlに当たらない場合は元の作成場所にジャンプする、少しインタラクティブなドラッグアンドドロッププログラムを作成しようとしています物体。

私が抱えていた問題は、if ステートメントがドロップ位置をクラスのオブジェクトとして認識せず、2 日間さまざまな方法でコーディングした後、それを機能させることができないことです。

これは、子クラスが固有の J_Objects クラスです。

package classes {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.display.MovieClip;

    public class J_Objects extends Sprite {
        private var xSpeed:int = 0;
        private var ySpeed:int = 2;
        private var topPadding:int = 250;
        private var drag:Boolean = false;
        private var tempSpeedX:int = xSpeed;
        private var tempSpeedY:int = ySpeed;


        public function J_Objects(_x:int, _y:int) {
            // constructor code
            this.x = _x;
            this.y = _y;
            this.buttonMode = true; //add hand cursor on mouse hover
            this.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
            this.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
        }

        public function move():void {
            if (this.xSpeed <= -1) {
                    this.scaleX = -1;
                }
                else if (this.xSpeed >= 1) {
                    this.scaleX = 1;
                }
            //Check if at the left or right and change direction if so
            if ((this.x - this.width/2 <= 0) || (this.x + this.width/2 >= stage.stageWidth)) {
                this.xSpeed *= -1;
                //this.scaleX *= -1;


            }//Also check if that top or bottom and if so change direction
            else if ((this.y - this.topPadding - this.height/2 <= 0) ||  (this.y + this.height/2 >= stage.stageHeight)) {
            this.ySpeed *= -1;
            }
            this.x += this.xSpeed;
            this.y += this.ySpeed;
            }

            /* Drag and Drop
            Makes the specified symbol instance moveable with drag and drop.
            */
            public function clickToDrag(evt:MouseEvent):void
            {
                this.startDrag();
                pickedUp();
                xSpeed = 0;
                ySpeed = 0;

                //var dragEvent:Event = new Event('DRAG_OBJECT', true );
                //this.dispatchEvent(dragEvent);

            }

            public function releaseToDrop(evt:MouseEvent):void
            {
                this.stopDrag();

                //fishBowl.object();
                if (evt.currentTarget.hitTestObject(evt.target.name == "fishBowl")) {
                    trace("Fishbowl got hit");
                }
                else {
                    xSpeed = tempSpeedX;
                    ySpeed = tempSpeedY;
                    putDown();
                    trace("Dropped objects was: ", this.name);
                }
                /*if (evt.target.hitTestObject(evt.target.name) == fishBowl) {
                    trace("Fishbowl got hit");
                }
                else {
                    xSpeed = tempSpeedX;
                    ySpeed = tempSpeedY;
                    putDown();
                    trace("Dropped objects was: ", this.name);
                }*/


            }

            public function pickedUp():void {
                //var tempX:int = 0;
                //var tempY:int = 0;
                //tempX = this.x;
                //tempY = this.y;
                //this.x = 
            }

            public function putDown():void {
                //pickedUp();
                //this.x = tempX;
                //this.y = tempY;
            }


    }

}

ifステートメントでいくつかの異なる方法を試しましたが、ブール値をクラスに変更できない、静的変数などにアクセスできないなどの結果が得られます。

子オブジェクトのコードは次のとおりです。

package classes {

    import flash.display.MovieClip;



    public class J_GoldFish extends J_Objects {
        var tempX:int = 0;
        var tempY:int = 0;


        public function J_GoldFish(_x:int, _y:int) {
            // constructor code
            super(_x, _y);
            tempX = _x;
            tempY = _y;
        }

        override public function putDown():void {
            this.x = tempX;
            this.y = tempY;
        }
    }

}

package classes {

    import flash.display.MovieClip;



    public class J_BlueFish extends J_Objects {
        var tempX:int = 0;
        var tempY:int = 0;

        public function J_BlueFish(_x:int, _y:int) {
            // constructor code
            super(_x, _y);
            tempX = _x;
            tempY = _y;
        }

        override public function putDown():void {
            this.x = tempX;
            this.y = tempY;
        }
    }

}


package classes {

    import flash.display.Sprite;



    public class J_FishBowl extends Sprite {



        public function J_FishBowl(_x:int, _y:int) {
            // constructor code
            this.x = _x;
            this.y = _y;
        }
    }

}

そしてメインクラス:

package  {
    import flash.display.Sprite;
    import classes.*;
    import flash.events.Event;
    import flash.events.MouseEvent;


    public class Main extends Sprite {
        private var goldFish:J_GoldFish;
        private var blueFish:J_BlueFish;
        private var bird:J_Bird;
        private var fishBowl:J_FishBowl;
        private var pass:String = "";


        public function Main() {
            // constructor code
            this.goldFish = new J_GoldFish(146.80, 372.75);
            this.blueFish = new J_BlueFish(59.95, 335.05);
            this.bird = new J_Bird(497.35, 48.45);
            this.fishBowl = new J_FishBowl(269.30, 319.50);
            this.fishBowl.name = "fishBowl";
            this.goldFish.name = "goldFish";
            this.blueFish.name = "blueFish";
            this.bird.name = "bird";
            addChild(this.fishBowl);
            addChild(this.goldFish);
            addChild(this.blueFish);
            addChild(this.bird);

            stage.addEventListener(Event.ENTER_FRAME, animate);
            //this.addEventListener(MouseEvent.MOUSE_OVER, sendObject);
            //stage.addEventListener('DRAG_OBJECT', stopAnimate);

        private function animate(evt:Event):void {
        //make the fish animate (move)
        this.goldFish.move();
        this.blueFish.move();
        this.bird.move();

        }

        private function stopAnimate(evt:Event):void {
            //var target:J_Objects = (evt.target as J_Objects); //definde the target incase I want to change properties         
            //stage.removeEventListener(Event.ENTER_FRAME, animate);
        }

    }

}
4

1 に答える 1

1

あなたの問題は実際には2つあります。

次のコード行では:

if (evt.currentTarget.hitTestObject(evt.target.name == "fishBowl"))

hitTestObject括弧が間違った場所にあるため、評価から派生したブール値でメソッドを呼び出そうとしていますevt.target.name == "fishBowl"

エラーを取り除くには、次のように変更する必要があります。

if (evt.currentTarget.hitTestObject(evt.target))

ただし、これは主な問題を解決しません。これは、 target(および、ターゲット内に含まれる表示オブジェクトの種類に応じてcurrentTarget)evtがドロップされたオブジェクトになるという事実です。 MOUSE_UP イベント。

ドロップされたオブジェクトが配置されたオブジェクトを見つけようとしているので、dropTargetプロパティを確認する必要があります。

修正されたreleaseToDrop関数は次のとおりです。

public function releaseToDrop(evt:MouseEvent):void
{
    this.stopDrag();

    if (this.dropTarget && this.dropTarget.name == "fishBowl")
    {
        trace("Fishbowl got hit");
    }
    else 
    {
        xSpeed = tempSpeedX;
        ySpeed = tempSpeedY;
        putDown();
        trace("Dropped objects was: ", this.name);
    }
}
于 2013-02-17T21:36:05.367 に答える