Search code examples
actionscript-3flash-cs6

Actionscript 3, how to use hitTestObject between two or more classes?


I am trying to create a little interactive drag and drop program that will check if the drop location is the fishBowl class object and either places the dragged object in the fishBowl object or will jump back to the original created location if it does not hit the fishBowl object.

The problem that I have been having is that the if statement does not recognize the drop location as an object of a class and after two days of coding different ways I can not get it to work.

This is my J_Objects class where the child classes inherent from:

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


    }

}

I have tried several different ways in the if statement but I get the: can not change Boolean to class, can not access static variable etc etc.

These are the code for the child objects:

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

}

And then the main class:

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

    }

}

Solution

  • Your issue is actually twofold.

    In the following line of code:

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

    Your parentheses are in the wrong place, and therefore, you're attempting to call the method hitTestObject on the boolean derived from evaluating evt.target.name == "fishBowl"

    In order to get rid of your error, you'd have to change it to something like:

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

    This would not solve you main problem, however, which is the fact that the target (and, depending on what sort of display objects are contained within the target, currentTarget) of evt will be the object that was dropped as it is the object that has dispatched the MOUSE_UP event.

    Since you are attempting to find the object on which the dropped object has been placed, you'll want to look at the dropTarget property.

    Here is the corrected releaseToDrop function:

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