Reputation: 235
I will try to explain the issue as good as possible. What I want to do is to check for collision between to objects. Also the bullet and the enemy, if it was 1 of the bullet and 1 of the enemy it would be no problem, but after one day on google and tryed like every solutions I can't get it to work, or I find a solution that's absolutely terrible explained. Im going to make it easyer. I have MainClass.as that document class. It's one class for the Enemy and one for the Bullet. As I hope everyone understand I wan't to check if the bullet hit's the Enemy. Or the other way. Since the bullet's are been made while clicking for example it is not possible to direct refere to the bullet. What I have tryed is using sprite container, it does work if i place the bullet on the enemy just to check, but if I place one under and on over that's NOT hit the Enemy it's still says that the enemy got hit. Note, the Enemy are dragged on stage, since im going to have multiple of them. I could also add them with code. Ive tryed to use Array to shuffle between all the enemys but I can't get it to work.
I know this was much text. Hope someon are willing to help me out, it is so frustrating...
Best regards HFherasen!:):)
Upvotes: 0
Views: 223
Reputation: 5497
Well, if you don't use a physics engine, like Box2DFlash, it can be frustrating.
First, you have to know the speed/frame of the bullet, because if it goes beyond the target in one frame, simple detection won't work. Than, you should have a velocity and direction of the bullet.. For simplicity, lets use one point of it. (the head of the bullet, or the middle) The bullet usually moves a lot faster than the targets, so you can assume the targets fixed. The target is a rectangle, or polygon possibly rotated.
Now you have a target, and a line for the bullet (from the point and direction). http://keith-hair.net/blog/2008/08/04/find-intersection-point-of-two-lines-in-as3/ You can use this (converted to as3), with all sides of the target (or only the ones facing to a bullet), and if one matches, the shot will hit a target. If your bullet is slower, you can have the position change of the frames as your first line. Than you'll know, when it hits a target. This is pretty fast, and works well. Hope it helped.
Upvotes: 1