How can I check for an a hitTest between an object and a class or a class and another class in actionscript3?

StackOverflow https://stackoverflow.com/questions/20687871

  •  19-09-2022
  •  | 
  •  

Question

This is all in actionscript 3.0 and is being code in flash cs5.5 Im making a push/pull game in which a player pushes a block around. The levels will eventually be very large and have numerous instances of each class, therefore it is impractical to name each symbol with an instance name on stage. I have this engine.as file i wrote, but i cant figure out why it wont work. Heres the entire class, but what im trying to use is hitTestClass Function; package {

import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;

public class Engine extends MovieClip
{
    private static var _instance:MovieClip;

    public function Engine()
    {
        stop();
        _instance = this;
    }

    public static function add(object:Object):void
    {
        if (object!=DisplayObject) return;
        _instance.addChild(object as DisplayObject);
    }

    public static function remove(object:Object):void
    {
        if (object is Array) {
            removeList(object as Array);
            return;
        }
        if (!(object is DisplayObject) || object.parent!=_instance) return;
        _instance.removeChild(object as DisplayObject);
    }

    public static function removeList(objects:Array):void
    {
        while (objects.length>0) remove(objects.pop());
        //trace(objects.length);

    }

    public static function getByName(name:String):Object {
        if (_instance) {
            for (var i:int = 0; i < _instance.numChildren; i++) 
            {
                if (_instance.getChildAt(i).name==name) {
                    return _instance.getChildAt(i);
                }
            }
        }
        return null;
    }

    public static function getAllByName(name:String):Array {
        var list:Array = new Array();
        if (_instance) {
            for (var i:int = 0; i < _instance.numChildren; i++) 
            {
                if (_instance.getChildAt(i).name==name) {
                    list.push(_instance.getChildAt(i));
                }
            }
        }
        return list;
    }

    public static function getByClass(className:Class):Object {
        if (_instance) {
            for (var i:int = 0; i < _instance.numChildren; i++) 
            {
                if (_instance.getChildAt(i) is className) {
                    return _instance.getChildAt(i);
                }
            }
        }
        return null;
    }

    public static function getAllByClass(className:Class):Array {
        var list:Array = new Array();
        if (_instance) {
            for (var i:int = 0; i < _instance.numChildren; i++) 
            {
                if (_instance.getChildAt(i) is className) {
                    list.push(_instance.getChildAt(i));
                }
            }
        }
        return list;
    }

    public static function hitTestName(object:DisplayObject, name:String):Object
    {
        var list:Array = getAllByName(name);
        return hitTestList(object, list);
    }

    public static function hitTestClass(object:DisplayObject, className:Class):Object
    {
        var list:Array = getAllByClass(className);
        return hitTestList(object, list);
    }

    public static function hitTestList(object:DisplayObject, list:Array):Object
    {
        var hits:Array = new Array();
        for (var i:int=0; i<list.length; i++) {
            if (object != list[i] && object.hitTestObject(list[i])) {
                hits.push(list[i]);
            }
        }
        return hits;
    }       

}

}

Throughout my game i use update functions that are inside the class of each "block", and here is where they check for hit tests. Here's an example from the wallBlock class that makes sure the char cant run through the wallBlock. function ableToCheckSides() {

        if ((this).x-_root.mcChar.x<63.5 && (this).x-_root.mcChar.x>-63.5)
        {
            //trace("booty");
            //ableToCheckXSides=false;
            if((this).y-_root.mcChar.y >=-63 && (this).y-_root.mcChar.y<0 && ableToCheckXSides==false)
                   {
                    //ableToCheckXSides=false;   
                    //_root.mcChar.x=_root.mcChar.x;

                    if(hitTestObject(_root.mcChar)==true)
                        {
                            _root.mcChar.y=(this).y+63.5;

                        }



                     //if char is moving down and hits the top side of tile

                   }
                 else if((this).y-_root.mcChar.y<=63 && (this).y-_root.mcChar.y>0 && ableToCheckXSides==false)
                   {
                    //ableToCheckXSides=false;
                    //_root.mcChar.x=_root.mcChar.x;
                    if(hitTestObject(_root.mcChar)==true)
                        {
                            _root.mcChar.y=(this).y-63.5;

                        }



                     //if char is moving up and hits the bottom side of tile

                   }

        }


        if ((this).y-_root.mcChar.y<63.5 && (this).y-_root.mcChar.y>-63.5)
        {
            //trace("YYYYYY");

            ableToCheckXSides=true;
            if((this).x-_root.mcChar.x >=-63 && (this).x-_root.mcChar.x<0)
                   {
                    //trace("inside function");
                    //ableToCheckYSides=false;
                    if(hitTestObject(_root.mcChar)==true)
                        {
                            _root.mcChar.x=(this).x+63.5;
                        }


                     //if char is moving left and hits the right side of tile

                   }
                   else if((this).x-_root.mcChar.x<=63 && (this).x-_root.mcChar.x>0)
                   {
                    //ableToCheckYSides=false;
                    if(hitTestObject(_root.mcChar)==true)
                        {
                            _root.mcChar.x=(this).x-63.5;

                        }


                     //if char is moving right and hits the left side of tile   
                   }
        }
        else 
        {
            ableToCheckXSides=false;
        }

Here is where the problem comes into play, in the same "update" function that occurs every frame i have tried to use my hitTestClass function to see if the crateBlock hits the wallBLock, but it will never return true.(If i change if(hits.length>0) to if(hits) it always returns true.)

var hits = Engine.hitTestClass(this, crateBlock);
        if (hits.length>0) 
        {
            Engine.remove(hits);
            trace("hitting crate");
        }
Was it helpful?

Solution

You should not use HitTestObject, you should CollisionTestKit, which is a library available here at Google code:

https://code.google.com/p/collisiondetectionkit/

HitTestObject doesn't work too well on its own. This library lets you do much more sophisticated stuff.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top