hitTest无法正常工作

时间:2014-04-17 20:57:55

标签: actionscript-3 flash hittest

我不明白。 HitTest非常基本但不起作用。我希望我的影片剪辑Faller能够击中Test Touch1,但是我得到了错误1061.我想我完成了fallerThingsLeft toTest Touch1告诉我score_txt上的“HIT”,但它告诉我命中3秒 在它真正击中之前。我不明白。不能有人告诉我我做错了什么

import flash.display.Graphics;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
var objectSpawner: Timer;
var fallers: Array;


function initGame(): void {
    fallers = [];
    objectSpawner = new Timer(1000);
    objectSpawner.addEventListener(TimerEvent.TIMER, createEnemy);
    objectSpawner.start();
    addEventListener(Event.ENTER_FRAME, dropEnemies);
}
function createEnemy(e: TimerEvent): void {
    var enemy: Faller = new Faller();
    enemy.y = -stage.stageHeight;
    enemy.x = Math.random() * 380;
    MovieClip(enemy).cacheAsBitmap = true;
    addChild(enemy);
    fallers.push(enemy);
    drawConnectors();
}
function dropEnemies(e: Event): void {
    trace(fallers.length);
    for each(var mc: Faller in fallers) {
    mc.y += 10;
    if (mc.y > stage.stageHeight * 2)
        fallers.splice(fallers.indexOf(removeChild(mc)), 1);

    drawConnectors();
    }
}
function drawConnectors(): void {
    if (fallers.length == 0) return;
    var g: Graphics = this.graphics;
    g.clear();
    g.lineStyle(10,0xFFFFFF);
    var mc: Faller = fallers[0];
    g.moveTo(mc.x, mc.y);
    for each(mc in fallers) g.lineTo(mc.x, mc.y);


}

init()


function init():void
{
          var fallingThingsLeft:FallingThings = new FallingThings
          (stage.stageWidth / 2, stage.stageHeight);
          var fallingThingsRight:FallingThings = new FallingThings
          (stage.stageWidth / 2, stage.stageHeight);
          addChild(fallingThingsLeft);
          addChild(fallingThingsRight);
          fallingThingsRight.x = stage.stageWidth / 2;




    }

import flash.events.Event;

this.addEventListener( Event.ENTER_FRAME, handleCollision)

function handleCollision( e:Event ):void
{
    if(fallingThingsLeft.hitTestObject(Touch1))
       {
           output_txt.text = "HIT"
       }
       else
       {
           output_txt.text = "MISS"
       }
}

1 个答案:

答案 0 :(得分:0)

没有看到物体,我无法确定;但是,当开始使用hitTestObject时,常见的问题是理解命中测试是在边界框上执行的。见下图。这将使用hitTestObject注册一个'hit',因为边界框通过了命中测试。

enter image description here