Hit detection in flash as3
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开发者_StackOverflow中文版 id go about it? thanks
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
精彩评论