【问题标题】:As3 hittesting doesn't really work (nested)As3 hittesting 并没有真正起作用(嵌套)
【发布时间】:2014-03-11 03:09:26
【问题描述】:

我有一辆坦克。

我有一个英雄(可控角色)。

坦克有一个嵌套的动画剪辑,它的表面积非常薄。

然而,当它甚至没有碰到英雄时,它就会检测到碰撞。

public class tank_sight extends MovieClip
{
    private var _root:MovieClip;

    public function tank_sight() 
    {
        addEventListener(Event.ADDED, beginClass);
    }

    private function beginClass(event:Event):void
    {
        _root = MovieClip(root);
        addEventListener(Event.ENTER_FRAME, loop);
    }

    private function loop(event:Event):void
    {
        if(this.hitTestObject(_root.hero.hitbox))
        {
            this.gotoAndStop(2);
            trace("HIT");
            fire();
        }
        else
        {
            this.gotoAndStop(1);
        }
    }

    private function fire():void
    {
        var shell:Shell = new Shell(x, y, rotation - 180);
        _root.addChild(shell);
    }
}

怎么了?没看懂。

编辑:视线在旋转,所以这可能就是原因。我尝试在播放器类上使用此代码:

    point = _root.tanks.barrel.sight.localToGlobal(new Point());

        if(this.hitTestPoint(point.x, point.y, false))
                {
                    trace("HIT");
                }

但它不起作用..它永远不会追踪“HIT”,除非我在某些时候站在某个奇怪的位置。

【问题讨论】:

    标签: actionscript-3 nested collision movieclip hittest


    【解决方案1】:

    hitTestObject 也适用于嵌套对象(显示具有不同父对象的对象),您应该检查游戏的逻辑,并进行一些测试。

    简单示例:

    var squareHolder:Sprite = new Sprite();
    var squareInner:Shape = new Shape();
    var hitHolder:Sprite = new Sprite();
    var hitCircle:Shape = new Shape();
    
    hitCircle.graphics.beginFill(0x990000);
    hitCircle.graphics.drawCircle(0, 0, 20);
    
    squareInner.graphics.beginFill(0x009900);
    squareInner.graphics.drawRect(0, 0, 40, 40);
    
    addChild(squareHolder);
    squareHolder.addChild(squareInner);
    squareHolder.x = 200;
    squareHolder.y = 100;
    squareInner.x = 50;
    squareInner.y = 50;
    
    stage.addChild(hitHolder);
    hitHolder.addChild(hitCircle);
    hitCircle.transform.matrix = new Matrix(1, 0.5, 0.5, 1, 30, 30);
    
    stage.addEventListener(MouseEvent.MOUSE_MOVE, function (e:MouseEvent):void {
        hitHolder.x = e.stageX;
        hitHolder.y = e.stageY;
    
        if (hitCircle.hitTestObject(squareInner)) {
            trace("Ding!");
        }
    });
    

    无论您对hitCircle(可见=假、透明填充、转换)做什么,它仍然可以工作。

    【讨论】:

    • 你的意思是我的游戏逻辑,你到底是什么意思?我无法弄清楚我编写的代码有什么问题......它应该可以工作!
    猜你喜欢
    • 1970-01-01
    • 2012-01-13
    • 2016-12-25
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    相关资源
    最近更新 更多