Actionscript 3 I need someon to help me with Collision Detecting(hiTestObject) - actionscript-3

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!:):)

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.

Related

dragging object along and within path only

I'm having a terrible time trying to code in Flash for a small game level.
I'm trying to drag an object within a path using a mouse pointer. And whenever the object hits the path boundary it shouldn't go beyond. Can anyone help me. I'm a noob in flash actionscript 3.
Any help would be much much appreciated !Thanks in advance.
I think what you're looking for is termed "Bitmap Hit Testing." It goes beyond the level of the regular hit test checking.
In regular hit testing (like obj.hitTestObject(otherobj)), Flash is checking the "boundary boxes" against each other. For instance, if you have two green circles you're trying to hit test against each other, they will register as colliding even if the corners which appear white are touching. There's an invisible bound box surrounding the circle that's colliding with the other invisible bound box surrounding the other circle.
Bitmap hit testing, on the other hand, is significantly slower and complex, but much more accurate. It is actually checking pixel per pixel whether the two images are colliding. In this case then, bitmaps can be irregular shapes with say, a path within, like this:
I would recommend looking at http://www.freeactionscript.com/tag/using-bitmapdata-hittest/ for a demo to make sure that this is what you're looking for, and then http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/ for an explanation, both by Mike Chambers.
I surmise that your process would be a conversion of both of your clips (the dragged object and the "walls" of the path) into BitmapData (do some quick research), and then a simple method call of the BitmapData.hitTest for your checking. Let me know if you have any questions or need any additional clarification :-)

ActionScript: Separating child from parent on Stage

I'm primarily a C++ programmer trying my hand at Flash and ActionScript3 for game development. I'm finding myself a little confused/constricted by the whole Parent/Child relationship, and was wondering if anybody here could give a little insight.
I'm currently making an Astro Panic clone, where each EnemyShip object also contains an array of EnemyShots. My problem is this: when one of the players' shots collides with an EnemyShip, the ship is supposed to disappear. However, at the moment, if the shot collides with one of a ship's shots, this also causes the entire corresponding EnemyShip (as well as all it's previous shots) to disappear. On top of that, it seems that the entire area between an EnemyShip and it's shot(s) trigger a collision. For example, in the following image, if the player's shot reaches anywhere within the red rectangle, the game will register it as the enemy having been hit:
Obviously I can't have this, but I'm unsure how to deal with it. Is there any way to separate child objects from parents on the stage, or is that just not how ActionScript works?
If I were you I would create a clip for all the shots. This means there would be 3 principal clips: the player, the enemies and the shots. This way you could achieve shot-ship and shot-shot interactions.
I hope this helps.
You should separate the model (Ships, Shots, Enemies) from their actual representation. Although a shot might be associated with a ship, the visual representations might share the same parent.
So basically I'm trying to say that you can flat the display hierarchy.

Moving character on a specific stage Actionscript 3

I'm making a game with ActionScript 3.0. There's is one thing I can't figure out:
I have a big circle on the background. On this circle I want to let a character randomly move. But this character can only move on this circle, so it can't go to the background. Where do I need to start with this, how can i specify a specific stage like this?
P.S. I can't upload a picture yet, I've not enough "reputation points", otherwise I would have uploaded some pictures to get my question more clearly.
A general idea around which you can evolve is using hitTestPoint() method on your stage using current player's coordinates as the point being tested, and if this method returns false, this means the player has left the stage, and you need to put him back. Using previous location is the failsafe method, but you might also invent some other more smart way to do this.
The manual on hitTestPoint()

Efficient collision detection in AS3

I have a problem with a game that I'm doing. I basically have objects that are in a map and I have to check for each of them if they collide with the walls (and then do something). Since was working with AS2, I thought about doing the same way: I drew a picture with only the walls, so with only rectangles and everything else in between is transparent (does not exist, then the floor for example). In AS2 I put the image to the screen, let's call it wall, and then I did a hitTest to wall with every object. That is for instance, the object was actually on the image, since that the transparent parts were part of it, but the function was testing only on the visible parts, and so with the walls. So it worked.
Now in AS3 there is no HitTest but hitTestObject, which I used, and I do for example wall.hitTestObject(object). The problem is that this function is as if it doens't see the transparencies, and the objects while not touching the walls collide with them!
I found the PixelPerfectCollisionDetection that actually solves the problem but it is huge and heavy so in my case, with so many objects to be tested (at least 60) at each frame, the game slows down a lot!
What I need is a function like hitTestObject (i don't need a lot of accuracy!) that take care of the transparent parts of an image.
How can I do?
As mentioned in the comments, physics/game libraries will have this code built-in for you and should work out of the box.
But if you want to build it yourself, or even introduce your own optimizations, the first step (which is very inexpensive) is checking for bounds collision using entirely built-in functionality of DisplayObject.getBounds and Rectangle.intersects (though you must do so in a consistent coordinate space, i.e. the stage):
if (obj1.getBounds(stage).intersects(obj2.getBounds(stage)) {
// Cheap bounds intersection is true, now do pixel-perfect detection...
}
Then if the bounds check is true, perform the pixel-perfect collision detection.
It seems that BitmapData.hitTest is your best bet - see a blog post by Mike Chambers.
Prior to this method, if you're interested in neat techniques, there was a method outlined by Grant Skinner in his blog. It's quite a clever algorithm using built-in bitmap routines (aka, fairly fast), creating a BitmapData only as large as the overlapping region (or even scaling that down), and drawing the two objects into specific channels of the bitmapdata, then using BitmapData.getColorBoundsRect() to determine if there are any pixels touch. I'm guessing BitmapData.hitTest is faster, but it'd be fun to compare.
I ran into the same problem and to be honest i found the easy way to get rid of that is just generating a "mask" layer for the collisions. You can always place this under your background so it doesn't show, or change the transparencies and whatsoever. Do this in Flash, and after "covering" with rectangles (or whatever) the collisions, just select them all and make that a movie clip.
I'm guessing since you made the symbol in Flash, it obviously knows that even if the symbol consists of several individual drawings or whatever, it's not just an image.
For me this worked fine .

hitTestObject() collide somehow not working how it should

Or that's what I think, at least.
I just began with AS3 and I'm trying to do a little game where one ball moves on its own and the other one is handled by the player. So far, so good. That works.
What isn't working is hitTestObject(); it just returns true when it's like 2 cm from the other object. Here's a picture so you can see: http://dl.dropbox.com/u/37057843/coll2.jpg
I've read that hitTestObject just creats a rectangle around the objects and then tests for collisions on those rectangles, is it because of that?
If you need any piece of code I'll deliver. I know there are some other opensource libraries/engines like Box2D to solve this in a better way, but I don't want to jump to engines directly.
Thanks for reading!
Yes, hitTestObject will use the clips bounding box.
Check out this link, it has a lot of great information as well as a class that will do what you're looking for
http://sierakowski.eu/list-of-tips/39-collision-detection-methods-hittest-and-hittestobject-alternatives.html
Direct link to the package
http://code.google.com/p/collisiondetectionkit/