2

I simply added a sprite in AS3:

Sprite myspr = new Sprite();
myspr.addChild(mybitmap);
addChild(myspr);

Then I added an event. I did hitTestPoint for checking mouse is over my sprite or not.

stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseCheck);

private function mouseCheck(evt:MouseEvent):void {
    var xx:int = stage.mouseX;
    var yy:int = stage.mouseY;
    
    if(myspr.hitTestPoint(xx, yy, true)) {
        ...
        // I'm checking mouse over here.
    }
    
    evt.updateAfterEvent();
}

Problem is: hitTestPoint gives true when mouse comes to full boundary box. But it should give true only if mouse comes on transparent isometric sprite.

Is there a solution for this, thanks in advance.

Community
  • 1
  • 1
mhmtemnacr
  • 185
  • 3
  • 18
  • +1 for pic. I will read you question later – ZuzEL Jul 21 '13 at 09:00
  • Is myBitmap an image? I think shapeflag work with shapes which are vector in nature e.g. a shape drawn using tools in Flash Professional, a shape drawn using graphic directives like beginFill, lineTo, etc. – catholicon Jul 21 '13 at 09:54
  • @catholicon mybitmap is a Bitmap object. Its bitmapData is a transparent png. – mhmtemnacr Jul 21 '13 at 10:08
  • I haven't used Flash Professional, but I think you might be able to ipmort an image in a Shape. That shape might need some editing, but then shapeFlag should work. BTW, if it's a simple shape, then it'd be simpler to draw it instead. – catholicon Jul 21 '13 at 10:19
  • @catholicon is there another way to solve? It's impossible because I'm loading image via actionscript3 LoaderMax, not flash. And it's a huge sprite array, not only one sprite. These sprites creates an isometric land, like in isometric city management games. I want to detect mouse hovers which sprite – mhmtemnacr Jul 21 '13 at 10:50
  • Paras's answer below should give you the intended result. You can also check out http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/ – catholicon Jul 22 '13 at 01:23

2 Answers2

0

There's a few ways I usually do hit testing.

1) The easiest way is to use a an already made class that you can find online. Some people much smarter than me have created complex classes that allow for much better pixel to pixel interaction. The ones listed by Paras are all good. The problem with these is, for newer users, it can be hard to understand all the code and how to implement them. Usually it is simple once you understand what is going on though. You just replace your hit test with the class file and then enter in the correct arguments.

2) Another method is to actually go into the symbol, create a new layer, and then draw a rectangle(just turn the alpha down to 0%) where you want the hit test to work. This may seem like a stupid method, after all we are just confined to a square once again. BUT, it will actually work MUCH better than you'd expect. Just draw the square maybe slightly smaller than the height and width of your character you're detecting the hit test on, and you should be good to go. Give it an instance name (the hit square that is) and then just perform the hitTest with that square instead of the actual sprite. It works wonderfully and is a very simple solution. For what you're explaining though, this sounds like it might not work. This method is more from a gamer standpoint. It looks good when attacking and getting hit by enemies, but isn't necessarily exact. Also, if you want to do this with two characters (maybe a large attack hitting an enemy) simply draw a hit box for both sprites. This is probably a little more basic than using a pre-made pixel perfect hit detection test, but it works extremely well and takes only a few minutes.

spaderdabomb
  • 942
  • 12
  • 28