0

I'm trying to make a vieport in which you can pan by dragging the background image around.

Here is what I've done: http://students.info.uaic.ro/~tudor.berechet/0sandbox/aperture/science/testing/fatChance.html

Note that I've hidden the mouse cursor and replaced it with 2 custom ones.

The problem is, the event attached to the background, which is a touch event, fires even when the mouse is over a sprite/movieclip (the monkey or the stats) that's in front of that background. And the stats isn't even in the gameLayer (where the monkey and the bg and the mouse cursors are). Try to drag the stats around, and see that the background and everything else pans with it (this is unwanted). Also, I need to remove the custom cursor and show the regular cursor whenever the mouse is NOT on the background (i.e. when it's on the monkey or stats or any new sprites I may/will have in the future).

Now I haven't used flash in a while, and I'm no expert in it to begin with, but is this normal behaviour for events to fire regardless if their sender is buried under other stuff?

How would I go about changing this behaviour?

PS: I've attached the source to the .swf in the link above.

[EDIT] Also, in case this has anything to do with anything, I'm using Starling for my GameLayer.

[EDIT2] It appears that I haven't made myself very clear. I only want the stage to be draggable IFF the mouse is Directly on the background. Whenever there's a sprite between the mouse and the background, the background event should NOT fire so I can change the cursor and interact with said sprite.

Spectraljump
  • 4,189
  • 10
  • 40
  • 55
  • I can't find the source? – Lars Blåsjö Dec 06 '11 at 21:42
  • @Lars Right click > View Source, or go here: http://students.info.uaic.ro/~tudor.berechet/0sandbox/aperture/science/testing/srcview/index.html – Spectraljump Dec 06 '11 at 22:10
  • 1
    Since you mention being new to flash, have you tried playing with the mouseChildren and mouseEnabled properties of your movieclips / sprites? – Frank Dec 07 '11 at 02:42
  • 1
    Do a monkey.mouseEnabled = false like Frank said +1. – ToddBFisher Dec 07 '11 at 03:00
  • @Frank, ToddBFisher, I don't want to make the stats or the monkey immune to events. They will be interactive themselves. `mouseChildren`, `mouseEnabled` or `touchable` just make the sprites uninteractive. Why would I want that? – Spectraljump Dec 07 '11 at 18:43
  • have you tried monkey.mouseChildren = false? – Frank Dec 07 '11 at 18:55
  • @Frank, `mouseChildren` disables the interactivity of the target's children. Nor the background or the monkey have any children or are related in any way. Also, there seems to not be any implementation of this property inside `starling.display.Sprite;`. (But I have tried it outside Starling.) – Spectraljump Dec 07 '11 at 19:40

1 Answers1

2

If you want events to not pass through the monkey you can use stopPropagation(). So add (or update) an event listener to the monkey, and edit it so that e.stopPropagation() is the first line of the event handler.

private function _touchHandler(e:TouchEvent):void
{
    e.stopPropagation();
    // any other code for touching your monkey :P ...
}

Do the same for any other events you want to stop in their tracks.

kreek
  • 8,774
  • 8
  • 44
  • 69
  • Thank you KreeK for mentioning `stopPropagation()`. I was so stupid to not even think of the word "propagation"! :) It works fine for the monkey. But it acts weird for the `stats`. The hand (fake)cursor still moves around underneath `stats` and the mouse pointer doesn't show. However if you drag `stats`, it drags without dragging the background along with it (and the hand stays behind). BUT once you drag the background, dragging `stats` again will resume it's faulty ways of dragging both itself and the background with it. I updated my link and sources. `Stats` is in `StarlingIntro.as`. – Spectraljump Dec 07 '11 at 19:51