kakino
07-28-2009, 01:37 PM
Hello, I guess this is a fairly classical problem but I can't find the solution.
Starting from the example :
I a Flex appl, there are one component "myCompImage", one button "myButton", and a Label to display the results. The component contains only an image. "myCompImage" and "myButton" are siblings.
What I want :
button "myButton" listens to a click MouseEvent.
component "myCompImage" listens to the custom event DeplaceEvent
when the button is clicked, a custom event "DeplaceEvent.DEPLACE" is dispatched. The component must react and move down.
I have written this without success, the component does'nt move down...
1. Custom Event
package com.events
{
import flash.events.Event;
public class DeplaceEvent extends Event
{
public static const DEPLACE:String="deplace";
public function DeplaceEvent(type:String, bubbles:Boolean=true, cancelable:Boolean=false)
{
//TODO: implement function
super(type, bubbles, cancelable);
}
override public function clone():Event
{
return new DeplaceEvent(type, bubbles, cancelable);
}
}
}
2. A component with an image "test_compImage.mxml"
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="234" height="180">
<mx:Image x="10" y="10" width="214" height="160" id="monimage" source="../bin-debug/images/nenuphars.jpg"/>
</mx:Canvas>
3. The Flex application
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="456" height="340" creationComplete="initEvents()" xmlns:ns1="com.images.*" xmlns:ns2="*">
<mx:Script>
<![CDATA[
import com.events.DeplaceEvent;
import mx.controls.Alert;
import flash.events.*;
private function initEvents():void {
monTexte.text = "init ";
myCompImage.addEventListener(DeplaceEvent.DEPLACE, onDeplace);
myButton.addEventListener(MouseEvent.CLICK, onClick);
//addEventListener(DeplaceEvent.DEPLACE,onDeplace);
}
private function onClick(evt:MouseEvent):void {
var myEvent:DeplaceEvent= new DeplaceEvent(DeplaceEvent.DEPLACE);
monTexte.text = " clic " + myEvent.type;
dispatchEvent (myEvent);
}
private function onDeplace(evt:Event):void {
monTexte.text = " -- " + evt.type + " compris";
evt.target.y += 5;
}
]]>
</mx:Script>
<mx:Button x="275" y="139" label="déplace" width="104" id="myButton"/>
<mx:Label x="10" y="301" text="Label" width="266" id="monTexte"/>
<ns2:test_compImage x="10" y="67" id="myCompImage">
</ns2:test_compImage>
</mx:Application>
Notice : The custom event is well formed because when I uncomment the line :
addEventListener(DeplaceEvent.DEPLACE,onDeplace)
and then, when I click on the button, the whole application moves down...
But what about my small component ? Definitively deaf...
Can you help me please ? I've been looking for days.. Thanks.
Starting from the example :
I a Flex appl, there are one component "myCompImage", one button "myButton", and a Label to display the results. The component contains only an image. "myCompImage" and "myButton" are siblings.
What I want :
button "myButton" listens to a click MouseEvent.
component "myCompImage" listens to the custom event DeplaceEvent
when the button is clicked, a custom event "DeplaceEvent.DEPLACE" is dispatched. The component must react and move down.
I have written this without success, the component does'nt move down...
1. Custom Event
package com.events
{
import flash.events.Event;
public class DeplaceEvent extends Event
{
public static const DEPLACE:String="deplace";
public function DeplaceEvent(type:String, bubbles:Boolean=true, cancelable:Boolean=false)
{
//TODO: implement function
super(type, bubbles, cancelable);
}
override public function clone():Event
{
return new DeplaceEvent(type, bubbles, cancelable);
}
}
}
2. A component with an image "test_compImage.mxml"
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="234" height="180">
<mx:Image x="10" y="10" width="214" height="160" id="monimage" source="../bin-debug/images/nenuphars.jpg"/>
</mx:Canvas>
3. The Flex application
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="456" height="340" creationComplete="initEvents()" xmlns:ns1="com.images.*" xmlns:ns2="*">
<mx:Script>
<![CDATA[
import com.events.DeplaceEvent;
import mx.controls.Alert;
import flash.events.*;
private function initEvents():void {
monTexte.text = "init ";
myCompImage.addEventListener(DeplaceEvent.DEPLACE, onDeplace);
myButton.addEventListener(MouseEvent.CLICK, onClick);
//addEventListener(DeplaceEvent.DEPLACE,onDeplace);
}
private function onClick(evt:MouseEvent):void {
var myEvent:DeplaceEvent= new DeplaceEvent(DeplaceEvent.DEPLACE);
monTexte.text = " clic " + myEvent.type;
dispatchEvent (myEvent);
}
private function onDeplace(evt:Event):void {
monTexte.text = " -- " + evt.type + " compris";
evt.target.y += 5;
}
]]>
</mx:Script>
<mx:Button x="275" y="139" label="déplace" width="104" id="myButton"/>
<mx:Label x="10" y="301" text="Label" width="266" id="monTexte"/>
<ns2:test_compImage x="10" y="67" id="myCompImage">
</ns2:test_compImage>
</mx:Application>
Notice : The custom event is well formed because when I uncomment the line :
addEventListener(DeplaceEvent.DEPLACE,onDeplace)
and then, when I click on the button, the whole application moves down...
But what about my small component ? Definitively deaf...
Can you help me please ? I've been looking for days.. Thanks.