0

Hi i have a snake game that uses a timer to incremement the snakes size, and ive been trying to figure out hit detection on it. and have got to this stage

for (var i = 1; i < snake.length; i++){ //this is where I am trying to make the hit
                if (Math.floor(snake[0].x) == Math.floor(s.x) && Math.floor(snake[0].y) == Math.floor(s.y) ){
                    trace("hit");
                }
            }

is this how id go about it? thanks

DIM3NSION
  • 1,681
  • 5
  • 20
  • 38
  • try using hitarea on the sprite containing the snake – The_asMan Mar 31 '11 at 00:18
  • How would i go about this, i just want it to hit detect the snake not the walls. any help and you would be a life saver – DIM3NSION Mar 31 '11 at 22:40
  • the same way as described below assuming your snake is made up of many joining sprites or MovieClips you should be hittesting on each one every enter frame – The_asMan Apr 01 '11 at 00:29
  • http://www.foundation-flash.com/tutorials/as3hittesting/ – The_asMan Apr 01 '11 at 00:53
  • thanks for the reply, unfortunately i haven't got 2 objects that i can see to directly call a hit test on. Im trying to hit test part of an array with another part. As as soon as the game starts sprites are added to the array. Therefore i need to check the first sprite in the array with all other parts of the array. Here's my code http://ignitethatdesign.com/FlashLeaderBoard/Snake.as – DIM3NSION Apr 01 '11 at 08:57
  • In your fillGrid function you are creating sprites and drawing in them. Those are the sprites you need to do your hittestObject. – The_asMan Apr 01 '11 at 15:23
  • When you on create the sprites store a reference to them in an Array and test the Array in your testForHits function. – The_asMan Apr 01 '11 at 15:26

1 Answers1

0

There are multiple ways to do it, I am telling you the simplest one. When there are two sprites (or MoveiClips) you can check their hit by using

sprite1.hitTestObject( sprite2 );

This is the simplest way to achieve what you are looking for. So initially put four simple sprites as walls and let snake crawl in between them and keep checking the "hitTestObject" on the four walls. This way

this.addEventListener( Event.ENTER_FRAME, enterFrameHandler );

private function enterFrameHandler( e:Event ):void
{
     if( snake.hitTestObject( sprite1 ) ) { // do something }
     // repeat above if with all four walls
}

See here http://ashwani.50webs.com/snake.jpg

Chris
  • 54,599
  • 30
  • 149
  • 186
coderbanna
  • 123
  • 1
  • 11
  • hey that will work for the walls thanks :) how would i go about doing a hit test on the snake itself? the snakes size is determined by an array btw – DIM3NSION Mar 31 '11 at 11:45
  • also with that code i get an error : 1046: Type was not found or was not a compile-time constant: Event. – DIM3NSION Mar 31 '11 at 11:54
  • you need to import Event import flash.events.Event; – coderbanna Mar 31 '11 at 15:59
  • Hi coderbanna how would i do a hit test on the snake itself? so like when the head hits its body it fires of a trace – DIM3NSION Mar 31 '11 at 22:22
  • hi dimension, your snake should be made up of some fix bone parts (some rectangle moviclip) and you need to check hit detection with each part to any other, if any part will hit other part, you got your snake dead. aight. – coderbanna Apr 01 '11 at 11:05