Question

I have a custom Flex 4.5 component (to be used in a List) -

Game

Game.mxml (represents a clickable playing table in a card game):

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer 
    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:comps="*"
    width="160" height="160" 
    autoDrawBackground="false"
    creationComplete="init(event)">

    <fx:Metadata> 
        [Event(name="pref_event", type="PrefEvent")] 
    </fx:Metadata>

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            private var _data:XML;

            [Bindable("dataChange")]
            public override function get data():Object {
                return _data;
            }

            public override function set data(value:Object):void {
                _data = value as XML;
                if (null == _data)
                    return;

                gameid = _data.@id;

                for (var i:uint = 0; i < 3; i++) {
                    this['_user' + i].data = _data.elements('user')[i];
                }

                dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
            }

            public function init(event:FlexEvent):void {
                _rect.filters = Util.SHADOW;
                addEventListener(MouseEvent.CLICK, handleClick);
            }

            private function handleClick(event:MouseEvent):void {
            trace("Clicked: " + gameid);
                dispatchEvent(new PrefEvent(PrefEvent.GAME_CLICKED, gameid, true));
            }

            public function set gameid(str:String):void {
                _gameid.text = '#' + str;
            }

            public function get gameid():String {
                return _gameid.text.substring(1);
            }
        ]]>
    </fx:Script>

    <s:Rect id="_rect" x="20" y="20" width="120" height="120" radiusX="16" radiusY="16">
        <s:stroke>
            <s:SolidColorStroke color="#4F69B5" weight="4"/>
        </s:stroke>     
        <s:fill>
            <s:SolidColor color="#CCDDEE" />
        </s:fill>
    </s:Rect>

    <s:Label id="_gameid" x="0" y="75" width="{width}" fontSize="16" textAlign="center" color="#FFFFFF" />

    <comps:User id="_user0" x="56"  y="4" scaleX=".3" scaleY=".3" interactive="false" visible="false" />
    <comps:User id="_user1" x="108" y="60" scaleX=".3" scaleY=".3" interactive="false" visible="false" />
    <comps:User id="_user2" x="4"   y="60" scaleX=".3" scaleY=".3" interactive="false" visible="false" />

</s:ItemRenderer>

and I'm trying to dispatch a custom event from it - PrefEvent.as:

package {
    import flash.events.Event;

    public class PrefEvent extends Event {
        public var str:String;
        public static const GAME_CLICKED:String = 'game_clicked';
        public static const CARD_CLICKED:String = 'card_clicked';
        public static const CARD_PLAYED:String  = 'card_played';

        public function PrefEvent(type:String, n:String, bubbles:Boolean = false, cancelable:Boolean = false){
            super(type, bubbles,cancelable);
            str = n;
        }

        public override function clone():Event {
            return new PrefEvent(type, str, bubbles, cancelable);
        }

        public override function toString():String {
            return str;
        }
    }
}

And here is finally my test code - GameTest.mxml:

<?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:comps="*"
    width="700" height="525" 
    creationComplete="init()">

    <fx:Script>
        <![CDATA[
            public function init():void {
                game.data = <game id="8946">
                                <user id="OK353118989212"/>
                                <user id="OK351923295875"/>
                            </game>;
            }       

            private function gameClicked(event:PrefEvent):void {
                trace("game clicked: " + event);
            }           
        ]]>
    </fx:Script>

    <comps:Game id="game" x="0" y="0" pref_event="gameClicked(event)" />

</s:Application>

But I never see the "game clicked: " trace. Does anybody please know why?

I'm probably missing something minor, because I can see the "Clicked: 8946" trace.

Was it helpful?

Solution

Problem:

In Game.mxml File has "pref_event" Event

[Event(name="pref_event", type="PrefEvent")]

but your dispatching PrefEvent.GAME_CLICKED ("game_clicked").

Solution:

You must dispatch the right event for this.

dispatchEvent( new PrefEvent("pref_event",gameid, true) );  

OTHER TIPS

[Event(name="pref_event", type="PrefEvent")] != new PrefEvent(PrefEvent.GAME_CLICKED, gameid, true)

The name of the events are not the same.

You should write this in your Game.mxml file :

<fx:Metadata> 
        [Event(name="game_clicked", type="PrefEvent")] 
</fx:Metadata>

And this in your main file :

<comps:Game id="game" x="0" y="0" game_clicked="gameClicked(event)" />

See here for further informations...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top