与一名男子的球碰撞

时间:2013-12-07 12:23:16

标签: actionscript-3 flash collision-detection

我正在做一个游戏,你应该躲避从屏幕左侧传来的弹跳球。我遇到的问题是球与人之间的碰撞。我希望球的“底部”击中该男子,因此它产生了“压碎”该男子的效果,从而结束了比赛。我正在使用adobe flash专业CC(Creative Cloud)。

编辑:我在游戏中添加了一个新的CollisionTest.as类,并遵循“完美像素”碰撞测试功能,但它似乎不起作用?有什么想法吗?

package 
{
import flash.display.BitmapData;
import flash.display.DisplayObjectContainer;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.display.MovieClip;
import flash.geom.ColorTransform;
import flash.display.Stage;

public class CollisionTest 
{
    // vars
    private var _returnValue:Boolean;

    private var _onePoint:Point;
    private var _twoPoint:Point;

    private var _oneRectangle:Rectangle;
    private var _twoRectangle:Rectangle;

    private var _oneClipBmpData:BitmapData;
    private var _twoClipBmpData:BitmapData;

    private var _oneOffset:Matrix;
    private var _twoOffset:Matrix;

    /**
     * Simple collision test. Use this for objects that are not rotated.
     * @param   clip1   Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
     * @param   clip2   Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
     * @return  Collision True/False
     */
    public function simple(clip1:DisplayObjectContainer, clip2:DisplayObjectContainer):Boolean
    {
        _returnValue = false;

        _oneRectangle = clip1.getBounds(clip1);
        _oneClipBmpData = new BitmapData(_oneRectangle.width, _oneRectangle.height, true, 0);
        _oneClipBmpData.draw(clip1);

        _twoRectangle = clip2.getBounds(clip2);
        _twoClipBmpData = new BitmapData(_twoRectangle.width, _twoRectangle.height, true, 0);
        _twoClipBmpData.draw(clip2);

        _onePoint = new Point(clip1.x, clip1.y)
        _twoPoint =  new Point(clip2.x, clip2.y)

        if (_oneClipBmpData.hitTest(_onePoint, 255, _twoClipBmpData, _twoPoint, 255))
        {
            _returnValue = true;
        }

        return _returnValue;
    }

    /**
     * Complex collision test. Use this for objects that are rotated, scaled, skewed, etc
     * @param   clip1   Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
     * @param   clip2   Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
     * @return  Collision True/False
     */
    public function complex(clip1:DisplayObjectContainer, clip2:DisplayObjectContainer):Boolean
    {
        _returnValue = false;

        _twoRectangle = clip1.getBounds(clip1);
        _oneOffset = clip1.transform.matrix;
        _oneOffset.tx = clip1.x - clip2.x;
        _oneOffset.ty = clip1.y - clip2.y;  

        _twoClipBmpData = new BitmapData(_twoRectangle.width, _twoRectangle.height, true, 0);
        _twoClipBmpData.draw(clip1, _oneOffset);        

        _oneRectangle = clip2.getBounds(clip2);
        _oneClipBmpData = new BitmapData(_oneRectangle.width, _oneRectangle.height, true, 0);

        _twoOffset = clip2.transform.matrix;
        _twoOffset.tx = clip2.x - clip2.x;
        _twoOffset.ty = clip2.y - clip2.y;  

        _oneClipBmpData.draw(clip2, _twoOffset);

        _onePoint = new Point(_oneRectangle.x, _oneRectangle.y);
        _twoPoint = new Point(_twoRectangle.x, _twoRectangle.y);

        if(_oneClipBmpData.hitTest(_onePoint, 255, _twoClipBmpData, _twoPoint, 255))
        {
            _returnValue = true;
            trace("collision detected!");
        }

        _twoClipBmpData.dispose();
        _oneClipBmpData.dispose();

        return _returnValue;
    }

}
}

我输入的两个参数是球和人,碰撞根本不起作用。我试过调试代码,但我找不到问题。但是,当我在球和不可移动的容器之间制作两个参数时,碰撞是否有效?

是不是因为我的男人移动了碰撞不起作用?我应该在上面的代码中更改什么?

提前谢谢你。

0 个答案:

没有答案