views:

28

answers:

3

I have a custom component Comp that gets its position changed as a result of interactions with other components, etc. I check for changes in its position by constantly checking its position at fixed intervals. I'm looking for a better solution (maybe an event-based solution) to monitor its position so I don't have to check constantly myself.

The 2 possibilities are

  • the component itself is self-aware of its position on stage and reports to the main application (via an event) when it knows that its position changed. Is that possible? Are components self-aware of their position or changes to their position? and what event could I piggyback on when that happens?

  • the other option is that the main application can monitor the position of the component and takes action when that position changes. Is that possible? Can the main application detect changes in the position of its components, and again what event could possibly be used here?

If not, how could I detect when that component changes position without having to constantly check for it myself.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
               xmlns:local="*">

    <local:Comp id="comp" x="100" y="100" />

</s:Application>
+3  A: 

Have you tried to listen to the move event?

splash
It works, but not 100% of the time. I think when multiple moves happen very quickly in succession, the last few events don't fire or don't get caught.
Kamo
+2  A: 

I second the early suggestion to listen to the move event

http://livedocs.adobe.com/flex/3/langref/mx/core/UIComponent.html#event:move

If you want to be more specific, the x and y values will dispatch changed events. So you could also listen to xChanged or yChanged if you wantd to perform different actions on the x or y movement.

www.Flextras.com
It works, but not 100% of the time. I think when multiple moves happen very quickly in succession, the last few events don't fire or don't get caught.
Kamo
Can you quantify "it". You mean the move event doesn't work all the time? That seems suspect to me. Can you provide a runnable sample?
www.Flextras.com
+1  A: 

Maybe you can play with data binding. The BindingUtils interface allows you to set a function on when a watched property is changing: http://livedocs.adobe.com/flex/3/langref/mx/binding/utils/BindingUtils.html#bindSetter()

itarato