let's say I have two shapes wrapped in Movieclip container. How can I detect if one shape is OVER another one? more precisely, whether one shape is covering a part/all of the other shape?
So far I've managed to do it by comparing coordinates of both shapes, but I'm wondering if there is some built-in function or an easier way to do it. Thanks
DisplayObject.hitTestObject(obj:DisplayObject):Boolean
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#hitTestObject()
If you need finer grain hit testing and are willing to work with BitmapData objects (you can draw a DisplayObject to BitmapData using BitmapData.draw(dispObj));
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#hitTest()
Here are a couple ways you could do this:
flash.display.DisplayObject.hitTest()
or
flash.display.DisplayObject.getRect()
ex: sprite1.getRect(stage).intersects(sprite2.getRect(stage));
You can use hitTestObject http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#hitTestObject%28%29
trace(shape1.hitTestObject(shape2));
But keep in mind that this is a simplistic test if just the bounds of the objects overlap. If you want pixel-perfect collision testing you'll have to use some 3rd-party library such as http://www.freeactionscript.com/2011/08/as3-pixel-perfect-collision-detection/
Related
I'm trying to make a 2D game in Java, and I'm using Libgdx. But, I don't know how to change the animation and velocity in water, for instance, changing the movement from walking to swimming.
Can anyone help me with the code and methods please?
Assuming that you use rectangular areas for your water and your sprites, you might want to look at checking if a rectangle (your water) contains another rectangle (your sprite). If you use non-rectangle shapes, you should find the appropriate methods to check if the hitbox of your sprite is inside your water.
If your water contains your sprite, you can handle (update, render, etc.) it differently.
I need a library which, fed with a bitmap, returns me an array of rectangles with coordinates and dimensions of the different areas found in the image.
I'll give a graphic example:
From this:
I want this:
Or from this:
I want this:
Is there such a library?
If I want to write one on my own where can I start to inform myself about it?
To my knowledge, the best you'll find are image filters, and color conversion methods, but not the kind of complicated edge detection you're looking for.
Of course, your query supersedes the canny edge detection, and is focused on image boundaries, but I've found no material on that even beyond AS3.
How do i know if keyframe is blank or not with actionscript 3
Thanks for help.
If you mean a frame in a layer in Flash IDE, the one with white circle, the answer is you can't. Layers in AS3 are not saved. They are merely converted to depth order.
What you can find out is if this particular frame doesn't contain any children (when all layers are combined) as it was already mentioned using numChildren property. If you have any shapes, bitmaps or movieclips on stage they all will be listed as children.
But you also might have something drawn in your sprite's graphics object which makes it non-empty in some way. If you want to check that you have to draw it in a BitmapData and examine its content.
As far as I know there is no real concept of keyframes in AS3, so I think that would be hard. You might be able to play to the frame you want to check, and see if getChildAt(0) throws a RangeError - but that is hackish at best, and probably won't work for content that isn't movieclips.
You can use numChildren getter on target clip to know how many clips is inside at moment .
EDIT:
You can also try use getBounds or getRect functions and when Rectangle width and height are equal 0 , thats mean clip is empty.
I'm trying to create a multi-level dungeon adventure in Flash CS4. The layout is an instance created of a symbol called Level, within the symbol are multiple wall subsymbols (wall), instances of Wall. There is a collision routine to stop the player walking through the walls, called from Wall.As.
Level is drawn about the centre point (0,0).
When I create an instance on the stage of Level (level), the collision tester is using the xy coordinates for the walls drawn about 0,0, not the "real" xy where it's appearing on the stage.
So what I need to know, is how to "update" the xy for each wall subsymbol with the live stage information, overriding the XYs drawn in the parent. It has to be updated unfortunately (I can't keep it static), as the levels are big so have to scroll.
Thanks for your advice.
With all due respect forget your approach, you're reinventing the wheel for nothing and probably to end up getting worse performance. What you need is pixel-perfect collision detection and probably including basic physics so already we're talking a huge amount of work. If you want to build levels in a design way for a game, use this, it'll blow your mind how awesome/easy/cool this is:
http://www.gotoandlearn.com/play.php?id=135
Its always a guess when trying to answer questions like this, as there are a lot of unknowns. That being said, in programming, there are always more than a few ways to solve a problem. Examine your collision detection routine - if you worked with hitTestPoint, and the point that was being tested (mouseX,Y or your main actor) with localToGlobal, you likely wouldn't need to test for the x,y variables of your collision objects. Read up on those two subjects and this question might be rendered moot.
At any rate, you could update relative coordinates in your Wall.as instance by leveraging globlaToLocal:
public function get curLoc():Point
{
return globalToLocal(new Point(this.x, this.y));
}
and retrieve them from your parent class as a point you can then test against:
trace(_wall.curLoc);
Hope that helps
I suppose you could accomplish what you're trying to do by manipulating the transform property of the wall symbols, but honestly I would concur with Ascension Systems and just abandon your collision testing routine for something different.
You may not need to go all out with a physics engine for your game, in which case just use hitTestObject to do the collision detection.
what would be your recommendation for drawing shapes (rects, circles...) onto BitmapData, and how to effectively switch between colors.
Is there any way to get graphics context from BitmapData so I could easily paint shapes using graphics.draw...()?
Why do you want to use a BitmapData? Not sure what you're after, but after reading a couple of your questions it seems you're a kind of fighting against how flash works. I think you'll make things much easier for yourself if you use what's available already. BitmapData objects are meant mainly to manipulate pixels and don't expose methods for drawing shapes. A Graphics object (available through Sprite, Shape, etc) on the other hand, allows you to draw vector shapes easily.
So, the best approach for this, I guess, would be using the drawing API to do what you want and then, if needed, convert the graphic to a BitmapData.