Skip to main content
Known Participant
September 22, 2018
Question

is it possible to add a hit test to object class, like a tile, if so how would approach this

  • September 22, 2018
  • 1 reply
  • 335 views

this is the way that I do a hit test inside a time line actionscript, but it takes a lot processor if there is a lot of tiles.  so then I was wondering if I could skip the loop and add it dirctly to the tiles class.  But I don't think variables are possible to pass back and fourth. 

for (var r=0;  r<tileholder.length;r++){

if(tileholder.hitTestPoint(player.x +10,player.y, true)){

health--

//trace(health)

moveSpeed = 1

}

that would be inside of an ENTER_FRAME

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
September 22, 2018

you wouldn't do usually add a hittest to an tile class (assuming the tile class is the class of a single tile).

you can usually streamline a loop like that by looping through the tiles in a rectangle 'near' player.

Known Participant
September 22, 2018

so I would put all tiles in multiple arrays and put all those arrays in another array? and use hit test to pull out an array?  then use hittest again to pull out the tile?

kglad
Community Expert
Community Expert
September 22, 2018

i'd use a 2d array so it's easy (computationally) and fast (computationally) to determine which array elements to check for a hittest.

eg,

var w:Number=tile.width;

var h:Number=tile.height

var tileA:Array=[ [tile at (0,0), tile at (w,0), tile at (2*w,0),..,last tile in top row], [tile at (0,h), tile at (w,h),..., last tile in 2nd row],...,[tile in bottom row left-most position, ..., right-most tile in bottom row] ];