Skip to main content

Microsoft Silverlight

Answered Question MouseLeave and multiple layersRSS Feed

(1)

MartJ
MartJ

Member

Member

12 points

9 Posts

MouseLeave and multiple layers

I'm programming an isometric game with mulitple layers. Everything went fine, but now I ran into a problem which I cannot solve.

 The game uses a series of tiles. Every tile has a base background (grass). Every tile also has an MouseEnter event, and when it is fired a structure is added to the canvas and drawn on top of the tile. The structure is a building with a lot of transparant background, it's bigger then the base tile and covers the base tile completely. When the MouseLeave occurs, the structure is removed from the canvas.

Well, problem now is when I Hover over the structure, the MouseLeave event of the base tile is fired. Even though the mouse actually is still on the right base tile. Somehow, MouseLeave is triggered when something with a bigger ZIndex is on top of the base tile. The result is a flickering graphical element.

I Tried some solutions like setting booleans when the relevant structure gets MouseEnter or MouseLeave and this works for most work but there are some exceptions. I don't understand why I can't get the position of the mouse with MouseEventArgs when the MouseLeave event is fired. That would solve it I guess. I tried using the MouseMove of the canvas and setting a point Property but this makes everything very slow.

 Please can someone post a workaround/solution. I'm pretty stuck here Sad

swildermuth
swildermuth

Star

Star

8320 points

1,546 Posts

Re: MouseLeave and multiple layers

Canvas.ZOrder was added later in the development cycle, if you use the natural order instead of Canvas.ZOrder, does it get better?  (If you're already doing that, could you provide a simple example of what you mean?)

(If this has answered your question, "Mark as Answer")

Shawn Wildermuth
C# MVP, MCSD, Speaker and Author

Silverlight 3 Workshop
Miami, FL: Oct 12-14th
Portlant, OR: Dec 2-4th
Atlanta, GA: Dec 7-9th
http://silverlight-tour.com

MartJ
MartJ

Member

Member

12 points

9 Posts

Re: MouseLeave and multiple layers

 

I already draw in the natural order, the problem exists somehwhere else.

 Imagine two rectangles like this

Now, when there is an MouseLeave event attached to the green rectangle, the event will also be fired when I hover over the blue rectangle, even when it is the part where the blue rectangle is on top the green rectangle. This is a simple representation of my problem. I cannot solve it by changing the drawing order because then the green layer will seem on top of the blue layer (like in there is grass on top of a building in my project :P)

Psychlist1972
Psychlis...

Contributor

Contributor

6040 points

973 Posts

MicrosoftModerator
Answered Question

Re: MouseLeave and multiple layers

Your overlaying piece gets in between the mouse and your underlying tile. If you hook the MouseEnter on your building, you'll very likely see it fire right when you get the mouse leave on your tile.

If you set the IsHitTestVisible to false on your building, does that take care of the issue with the tile?

Pete

Silverlight.net Moderator
MVP: Silverlight, Silverlight Insider
Author: Silverlight in Action, 2nd Edition
POKE 53280,0 - My Blog

Opinions are my own. Oh, and I don't work for Microsoft.

MartJ
MartJ

Member

Member

12 points

9 Posts

Re: MouseLeave and multiple layers

IsHitTestVisible was precisely the thing I was looking for, think I missed it in the documentation Confused

 Thnx

Higgorama
Higgorama

Member

Member

5 points

5 Posts

Re: Re: MouseLeave and multiple layers

This helped me as well - thank you for the answer!

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities