首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在两个或多个类之间使用hitTestObject?

如何在两个或多个类之间使用hitTestObject?
EN

Stack Overflow用户
提问于 2013-02-18 00:30:39
回答 1查看 4.1K关注 0票数 0

我正在尝试创建一个小的交互式拖放程序,它将检查拖放位置是否为fishBowl类对象,并将被拖动的对象放置在fishBowl对象中,或者如果没有命中fishBowl对象,将跳回最初创建的位置。

我一直遇到的问题是if语句不能将drop位置识别为类的对象,并且经过两天的不同方式编码后,我无法让它工作。

这是我的J_Objects类,其中的子类继承自:

代码语言:javascript
复制
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语句中尝试了几种不同的方法,但得到的结果是:不能将布尔值更改为类,不能访问静态变量等。

下面是子对象的代码:

代码语言:javascript
复制
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;
        }
    }

}

然后是主类:

代码语言:javascript
复制
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);
        }

    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-18 05:36:05

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

在以下代码行中:

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

您的圆括号放在了错误的位置,因此,您试图对从evt.target.name == "fishBowl"求值派生的布尔值调用hitTestObject方法

为了消除您的错误,您必须将其更改为如下所示:

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

然而,这并不能解决您的主要问题,即evttarget (以及,取决于目标中包含的显示对象的类型,currentTarget)将是被删除的对象,因为它是调度MOUSE_UP事件的对象。

由于您正在尝试查找放置了拖放对象的对象,因此您需要查看dropTarget属性。

下面是修正后的releaseToDrop函数:

代码语言:javascript
复制
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);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14923295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档