Reputation: 808
I'm developing a flash game called Frog, at the moment my code for collision between my frog and fly doesn't work as expected. I'd like to have it so it works something similar to the method linked below (last example).
Collision detection methods, hitTest and hitTestObject alternatives
Any help would be appreciated.
function hitTargetFunction():void {
for (i = 0; i < insectsMC.length; i++) {
for (j = 0; j < insectsMC[i].length; j++) {
if (frogMC.hitTestObject(insectsMC[i][j])) {
trace('Target: ' + insectsMC[i][j].name);
score += 1;
trace('Score: '+score);
insectsMC[i][j].x = 0 - insectsMC[i][j].width * 2;
}
}
}
}
Upvotes: 0
Views: 301
Reputation: 76
Creating a movieclip/sprite inside the frog movieclip will probably be the best thing for you in this situation. PixelPerfectCollision is very awesome and useful(however on larger games, expensive) but the collision would then be with any piece of the frog. You could of course break apart your frog movieclip and just do PixelPerfectCollision on just the tongue. But if this is for coursework I'd stick to just doing hitTest() on a display object of some kind inside the the frog.
Upvotes: 1
Reputation: 12431
If you're not allowed to use a third-party library for your coursework you should have a look at this excellent tutorial on pixel-level collision detection with ActionScript 3.0.
The general idea is that you create BitmapData copies of your vector graphics and use the hitTest method of that class to check for a collision at a pixel-level.
Upvotes: 0
Reputation: 8121
Might I suggest trying out the fantastic Collision Detection Kit?
I'm sure it will do everything you need and more. It's also AS3 which is what your code sample is written in, so I hope you find it useful.
Upvotes: 2