AS3 hitTest agaist copied pixels

Started by
10 comments, last by Tiblanc 12 years, 8 months ago
Hey there, I'm making this example of a working blitted button. When the mouse is over the copied pixels the hover picture copies over it. it recognizes the lack of pixels in the cut-out transparent shape, but the mouse is sensitive to the rest of the hidden tile sheet! How to hitTest against copied pixels?

Much thanks if any one could get me on the right track here!


package
{
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.events.Event;

public class BitmapButton extends Sprite
{
[Embed( source = "tile-set.png" )]
private var _Sheet:Class;
private var _sheet:BitmapData;

private var _canvas:BitmapData;

private var _buttonTile:Rectangle;
private var _buttonLoc:Point;

private var _hoverTile:Rectangle;
private var _hoverLoc:Point;

private var _mainRect:Rectangle;

public function BitmapButton()
{
render();

addEventListener( Event.ENTER_FRAME, detectMouse );
}

private function render():void
{
_sheet = new _Sheet().bitmapData;

_canvas = new BitmapData( 640, 480, false );
addChild( new Bitmap( _canvas ));

_buttonTile = new Rectangle( 0, 0, 200, 100 );
_buttonLoc = new Point( 80, 150 );

_hoverTile = new Rectangle( 0, 100, 200, 100 );
_hoverLoc = new Point( 80, 150 );

_canvas.copyPixels( _sheet, _buttonTile, _buttonLoc );
}

private function get mouseLoc():Point { return new Point( mouseX, mouseY ); }

private function detectMouse( event:Event ):void
{
if( _sheet.hitTest( _buttonLoc, 0, mouseLoc ) == true )

{ _canvas.copyPixels( _sheet, _hoverTile, _hoverLoc ); }

else if( _sheet.hitTest( _buttonLoc, 0, mouseLoc ) == false )

{ _canvas.copyPixels( _sheet, _buttonTile, _buttonLoc ); }
}
}
}
Advertisement
This may be because you copyPixels at point (80, 150) and your canvas (640, 480). I think the Sprite will extend to the size of the child Bitmap and catches mouse events.
Developer for Novus Dawn : a [s]Flash[/s] Unity Isometric Tactical RPG - Forums - Facebook - DevLog
When I made the canvas the size of the button and placed the points at ( 0, 0 ) so they would fill just the canvas, the mouse was still sensitive the the whole tile sheet, hidden parts and all. I'm not actually using a Sprite (as in a Sprite that is a one-frame MovieClip).

Well, I've got to hitTest against some bitmapData, and the only bitmapData (other than the canvas) that is available is the tile sheet ( _sheet ) . But I think what I really need to test against is the result of this :

_canvas.copyPixels( _sheet, _buttonTile, _buttonLoc );

No idea how. Not sure what is right to do. i just know that people make whole games by blitting the graphics so there must be a way to make menu buttons work.

It was nice you you to respond to my thread.

When I made the canvas the size of the button and placed the points at ( 0, 0 ) so they would fill just the canvas, the mouse was still sensitive the the whole tile sheet, hidden parts and all. I'm not actually using a Sprite (as in a Sprite that is a one-frame MovieClip).

Well, I've got to hitTest against some bitmapData, and the only bitmapData (other than the canvas) that is available is the tile sheet ( _sheet ) . But I think what I really need to test against is the result of this :

_canvas.copyPixels( _sheet, _buttonTile, _buttonLoc );

No idea how. Not sure what is right to do. i just know that people make whole games by blitting the graphics so there must be a way to make menu buttons work.

It was nice you you to respond to my thread.


I know this doesnt directly answer your question, but whenever using vanilla AS, I avoid the built in hitTest methods at all cost, I typically roll my own. On a side note though, if youre using bitmap graphics, use Flixel. Its awesome, it rocks, and its awesome. Trust me!
hmmm... I'm not sure what vanilla AS is... oh do tell me if you read this!

I'll check out Fixel, I've avoided going to a game engine because I'm afraid my game isn't standard and that if I don't understand every detail I won't be able to debug it.

Do you suppose it would work for a pixel art, non-tiled, role playing quest game with casual challenges including match three, memory match, quiz, maze chase, horizontal and vertical shooters and strategic battle using the model-view-controller design pattern, incorporating external data from and writing to JSON, and blitting mostly all the graphics ?

I spent two years making the art, story and music for a game and so far programming is by far the greatest challenge to me. I'll take some time to learn about Fixel maybe even before I get back to working on my sample of AStar pathfinding. Freakish riddle, there. Difficult to distill the tricky examples into a most basic scenario.
I see now what you did. You should do hitTest against _canvas and make it the size of your button. Better yet would be setting your event listeners on your Sprite and use Flash's built-in event system instead of reinventing the wheel.

About Flixel, it's designed for pixel art games in general. You should be able to do it all with it.
Developer for Novus Dawn : a [s]Flash[/s] Unity Isometric Tactical RPG - Forums - Facebook - DevLog
Thanks for the further recommendation about Fixel I will definitely be looking into it!

I did try making the canvas the same size as the button but that didn't make a difference. When you say Sprite I'm thinking like a one-frame movie clip, either with the art stored in the library or created in a class and referring to an art file containing one image located in an external assets folder... sorry if I misunderstand you, but I'm trying to work with bitmapData so I can have lots of images on a single tile sheet. I know this seems like total overkill for a single button, but right now, just at the very beginning of trying to make a game, I am only even making basic samples of functionality.
Your BitmapButton class extends Sprite(http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Sprite.html#methodSummary). Sprite is a class from the Flash API which extends EventDispatcher, so you can addEventListener on it for various events, like MouseEvent(http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/MouseEvent.html) CLICK, MOUSE_OVER, MOUSE_OUT and so on. If your sprite has the correct dimension and positioning in your stage, you won't need to worry about hitTest since Flash will tell you whenever the mouse moves over your button.
Developer for Novus Dawn : a [s]Flash[/s] Unity Isometric Tactical RPG - Forums - Facebook - DevLog
Tiblanc, It would really help if you could demonstrate what you mean by manipulating the code above. I've experimented considerably trying to interpret your advice and I'm not getting anywhere.

If the solution is light work and really obvious to you it would mean a lot to me to see how this plays out. Adobes MouseEvent page doesn't talk about detecting copied pixels.
Try placing this in your constructor :

addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);


And add this function :

private function onMouseOver(event:MouseEvent):void {
trace("onMouseOver");
}


I did not test it so there might be a compilation error, but what this will do is create an event listener for MOUSE_OVER events. When you move the mouse over the sprite, it should call onMouseOver which will output a trace in the console. You can place your button hovered code there. Next you should create a MOUSE_OUT event and place your code that resets the button. That way, you don't check it every frame and save yourself a hassle.
Developer for Novus Dawn : a [s]Flash[/s] Unity Isometric Tactical RPG - Forums - Facebook - DevLog

This topic is closed to new replies.

Advertisement