0
votes

I don't understand. HitTest is pretty basic but it won't work. I want my movieclip Faller to hitTest Touch1 but I get error 1061. I tought I had it done when I did fallerThingsLeft to hitTest Touch1 to tell me "HIT" on the score_txt, but it tells me Hit like 3 sec before it really hits. I don't get it. can't someone tell me what im doing wrong

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
I don't get it either, the code you're showing doesn't have hitTest anywhere. - TreeTree
oups! now its there ! - user3529507

1 Answers

0
votes

Without seeing the objects, I can't be certain; however a common issue when getting started with hitTestObject is understanding the hit test is performed on the bounding box. See the image below. This will register a 'hit' with hitTestObject because the bounding boxes pass the hit test.

enter image description here