Reputation: 6898
I have something really weird going on in my project (Flash CS5.5 and AIR 3...)
I need to check (for my own good reasons) whether the cursor is located within the bounds of a particular object. I'm using a simple code to do this...
if(mouseX > hsSlot1.x && mouseX < (hsSlot1.x + hsSlot1.width) && mouseY > hsSlot1.y && mouseY < (hsSlot1.y + hsSlot1.height))
{
//My code
}
This, theoretically, should work perfectly! However, there is an offset (and one I did NOT set, mind you!) of about 16 between the hsSlot1's visual position (where it appears to start on the screen) and hsSlot1.x. There is a similar offset on the y axis, but I haven't measured it yet.
The only thing I can think of for this is that hsSlot1 is an instance of a button (one with only one state at that) that was scaled to fit. However, I don't think that is it, because the numbers show that the entire object is merely offset at its current size.
What the devil is going on?
EDIT: I found another way of doing this that wasn't working at first. However, an answer would still be appreciated, as this type of thing may be a problem later as well...
Upvotes: 0
Views: 615
Reputation: 39476
First couple of things that come to mind:
hsSlot1
is not exactly top-left.hsSlot1
as a direct child then it may be within another container that is positioned slightly off.Why not try this (within the object hsSlot1
itself):
Dispatch an event from hsSlot1
when the mouse is "over" it:
if(mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height)
{
dispatchEvent(
new Event("CUSTOM_ROLLOVER")
);
}
And listen for it from elsewhere:
hsSlot1.addEventListener("CUSTOM_ROLLOVER", _reaction);
function _reaction(e:Event):void
{
trace('Glorious success.');
}
It still seems like you have graphics somewhere that aren't justified top-left to the registration point. This dodgy diagram I just drew up might help you understand what I mean and why it would affect your code the way it does:
Upvotes: 2
Reputation: 11139
The simple answer is that you're doing this the hard way, because an object's x
and y
and width
and height
properties don't necessarily correspond to its bounds. As Marty notes this is partly because an object's registration point (the origin of the object's local coordinate space) does not necessarily have to be at the object's corner. Further, depending on what you're doing scope-wise, the method you're using might need to account for whether the object is scaled, rotated, and so forth.
Solutions:
The quick and easy way to do what you want is with DisplayObject.hitTestPoint()
. That tells you whether a given (x,y) falls within the bounding box of your clip. Make sure to pass false
for the third parameter if you just want to check against the object's bounding box. (Passing true will check the x,y against the object's actual shape.) To avoid scope issues, you specify the (x,y) in global coordinate space, so one easy way to test the mouse location is to do:
myObject.hitTestPoint( stage.mouseX, stage.mouseY, false)
Or you can of course transform local coordinates to global space before you compare.
The robust way to know everything about an object's bounds is to use DisplayObject.getBounds()
. Just be aware that you explicitly specify a scope to use for getBounds, so you need to have a clear idea of what scope you need to know about.
Upvotes: 1