Skip to main content
Known Participant
April 23, 2014
Question

Bounding Box Problem

  • April 23, 2014
  • 4 replies
  • 1799 views

I'm making a game where there falling random dots all connected with the next one, creating a long zigzag line. I've try to do a hitTestObject  but the bounding box of "fallingThingsLeft" makes the hitTest with Touch1 inaccurate. What can I do to make this to work???

This topic has been closed for replies.

4 replies

Inspiring
April 26, 2014

What is the expected behavior?

1. Do you want hit test to performed on entire panel where all objects inside FallingThings instance are hit tested (dots and lines) or you need a hit test for dots only?

2. What do you need to do once hit is detected?

Also, where is the code that implements hittest? Did I miss it?

Known Participant
April 26, 2014

Yes all dots and lines (on both side) and when a button is hit we need to press it and if not it's game over.

Inspiring
April 26, 2014

I see at least one potential conflict in the vision. Lines/connectors can hit several adjacent buttons at the same time and remain in the state of been hitting buttons for some time. While line is moving down over button(s) - hit test will be true as long as pane is over/touching the button.

On the other hand, user should have a reasonable amount of time to press the button. This is significant because of the need for a certain interval between the moment hit test becomes true and button click produces a win - otherwise game may become user unfriendly to the extend of being useless/unwinnable.

Basically, I am yet to envision all valid use cases. I sense that depending on entire complex of usability aspects your approach may deem unfeasible.

As a side note, Ned's suggestion to look into bitmapdata hittest appears like the most robust one given everything else is in place.

Amy Blankenship
Legend
April 25, 2014

Instead of doing that, why not just read the size of the box (once), then chart a course to random x points within the box, with the y always going up?

Known Participant
April 25, 2014

Sorry but im not sure to understand..??

Amy Blankenship
Legend
April 25, 2014

Rather than send it off in a direction and change that direction once it hits the box, why not just set the path so it is always within the box?

Ned Murphy
Legend
April 25, 2014

sorry for losing sight of your other posting...  What you should do is look into using bitmapdata hitTest.  Search Google using terms like "bitmapdata hittest" and you will find a number of results that can help explain it.  Here is one of the results...

http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/

Known Participant
April 24, 2014

And here is my code:

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;

var score = 0;

score_txt.text = score;

import flash.events.Event;

this.addEventListener( Event.ENTER_FRAME, handleCollision)

function handleCollision( e:Event ):void

{

if(fallingThingsLeft.hitTestObject(Touch1))

   {

   trace("HIT")

   updateScore();

   }

   else

   {

   trace("MISS")

   }

}

}

function updateScore ():void

{

score_txt.text += 1;

}

}