简体   繁体   English

如何在actionscript3中检查对象与类或类与另一个类之间的hitTest?

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

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. 这一切都在actionscript 3.0中实现,并且正在Flash CS5.5 Im中进行编码。 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. 我写了这个engine.as文件,但是我不知道为什么它不起作用。 Heres the entire class, but what im trying to use is hitTestClass Function; 这是整个类,但是我想使用的是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. 这是wallBlock类的一个示例,该示例确保字符不能贯穿wallBlock。 function ableToCheckSides() { 函数enabledToCheckSides(){

        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.) 问题在这里发挥了作用,在我尝试使用我的hitTestClass函数查看帧crateBlock是否命中wallBLock的每一帧中都发生了相同的“更新”函数,但是它永远不会返回true。(如果我更改if( hits.length> 0)到if(hits),则始终返回true。)

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

You should not use HitTestObject, you should CollisionTestKit, which is a library available here at Google code: 您不应该使用HitTestObject,而应该使用CollisionTestKit,这是一个可在Google代码中找到的库:

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

HitTestObject doesn't work too well on its own. HitTestObject本身不能很好地工作。 This library lets you do much more sophisticated stuff. 该库使您可以执行更复杂的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM