Actionscript 3,如何在两个或更多类之间使用hitTestObject?

时间:2013-02-17 16:30:39

标签: actionscript-3 flash-cs6

我正在尝试创建一个小的交互式拖放程序,它将检查放置位置是否为fishBowl类对象,并将拖动的对象放在fishBowl对象中,否则将跳回原始创建的位置(如果不是击中了fishBowl对象。

我遇到的问题是if语句不能将丢弃位置识别为类的对象,经过两天的编码后,我无法使其工作。

这是我的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语句中尝试了几种不同的方法但我得到了:不能将Boolean更改为类,不能访问静态变量等等。

这些是子对象的代码:

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);
        }

    }

}

1 个答案:

答案 0 :(得分:1)

您的问题实际上是双重的。

在以下代码行中:

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

您的括号位于错误的位置,因此,您试图在评估hitTestObject

的布尔值上调用方法evt.target.name == "fishBowl"

为了摆脱你的错误,你必须将它改为:

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

这不会解决你的问题,但是,target(并且,取决于目标中包含的显示对象类型,{{1} currentTarget的{​​}}将是被删除的对象,因为它是已调度MOUSE_UP事件的对象。

由于您试图找到放置了已删除对象的对象,因此您需要查看evt属性。

以下是更正的dropTarget功能:

releaseToDrop