これは質問hereに似ています。注カスタムコンポーネントをカスタムコンポーネントから分離するFlex 4
:エラーが親からスローされます。私は、カスタムイベント「ShopEvent」を派遣していますが、私はエラーを取得しています「com.events.ShopEventにflash.events::[email protected]を変換することができないタイプの強制が失敗しました」カスタムコンポーネント(3番目のコードスニペット)、私はそこに詳細を追加しましたこれは私のカスタムイベントです。最初の定数を見て、私はカスタムコンポーネントにイベント名を貼り付けてコピーします。
package com.events
{
import flash.events.Event;
public class ShopEvent extends Event
{
public static var MENU_SELECTED:String = "menuSelected";
public static var SUBMENU_SELECTED:String = "submenuSelected";
public static var ITEM_SELECTED:String = "itemSelected";
public static var NAV_NEXT:String = "navNext";
public static var NAV_PREVIOUS:String = "navPrevious";
public static var NAV_LAST:String = "navLast";
public static var NAV_FIRST:String = "navFirst";
public static var CLOSE:String = "close";
public var menuIdx:int;
//public var menuType:String;
public var menuId:int;
public var menuName:String;
public var itemId:int;
public function ShopEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}
カスタムコンポーネント。メタデータタグを確認します。イベントが正しく登録されました
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="72" height="82"
mouseChildren="false"
creationComplete="init()"
click="onClick()"
buttonMode="true">
<s:layout>
<s:VerticalLayout horizontalAlign="center"/>
</s:layout>
<fx:Script>
<![CDATA[
import com.events.ShopEvent;
import mx.controls.Image;
public var menuId:int;
[Bindable]
public var menuText:String;
[Bindable]
public var bmp:Bitmap;
private function init():void{
//img.addChild(bmp);
}
private function onClick():void{
var e:ShopEvent = new ShopEvent(ShopEvent.MENU_SELECTED);
e.menuId = menuId;
e.menuName = menuText;
dispatchEvent(e);
}
]]>
</fx:Script>
<fx:Metadata>
[Event(name="menuSelected", type="com.events.ShopEvent")]
</fx:Metadata>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label text="{menuText}" fontWeight="bold" fontSize="12" width="44"/>
<mx:Image id = "img" width="57" height="47" source="{bmp}"/>
</s:Group>
親のカスタムコンポーネント。これは上記のカスタムコンポーネントの親コンポーネントです。これはmenuSelectedイベントをリッスンし、イベントをlistenerresにルーティングします。肉体タグを確認してください。イベント登録が正常に行われました。
しかし、エラーが私の知識で
menus[i].addEventListener(ShopEvent.MENU_SELECTED,function(e:ShopEvent):void{dispatchEvent(e);});
で来ている、私はコード内の任意の問題を参照してくださいいけません。それに間違いはありますか?
更新
驚くべきことに、私は「新しい」shopweventのインスタンスが解決する問題、を作成するが、悲しいことに、私はイベントオブジェクトのすべてのプロパティを閉じる必要があります。私はこれがフレックスの限界ではないことを願っています。
menus[i].addEventListener(ShopEvent.MENU_SELECTED,function(e:ShopEvent):void{dispatchEvent(new ShopEvent(ShopEvent.MENU_SELECTED));});
完全なコード
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="720" height="605"
creationComplete="init()" xmlns:shop="UI.shop.*" xmlns:hasu="UI.shop.hasu.*"
>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Metadata>
[Event(name="navNext", type="com.events.ShopEvent")]
[Event(name="navPrevious", type="com.events.ShopEvent")]
[Event(name="menuSelected", type="com.events.ShopEvent")]
[Event(name="submenuSelected", type="com.events.ShopEvent")]
[Event(name="itemSelected", type="com.events.ShopEvent")]
[Event(name="close", type="com.events.ShopEvent")]
</fx:Metadata>
<fx:Script>
<![CDATA[
import com.events.ShopEvent;
private const MAX_SLOTS:int = 6;
public var menus:Vector.<ShopMenuItemView>;
public var itemSlots:Vector.<ShopItemSlotView> = new Vector.<ShopItemSlotView>(MAX_SLOTS);
private function init():void{
trace("BEGIN:UI.shop.hasu.ShopView.init");
initSlots();
}
private function initSlots():void{
for (var i:int = 0;i<itemSlots.length;i++){
var slot:ShopItemSlotView = new ShopItemSlotView();
itemSlots[i] = slot;
itemSlotsContainer.addElement(slot);
}
}
public function initMenus():void{
trace("BEGIN:UI.shop.hasu.ShopView.initMenus");
for (var i:int = 0;i < menus.length;i++){
menuContainer.addElement(menus[i]);
menus[i].addEventListener(ShopEvent.MENU_SELECTED,function(e:ShopEvent):void{dispatchEvent(e);});
//menus[i].addEventListener(ShopEvent.MENU_SELECTED,function(e:ShopEvent):void{dispatchEvent(new ShopEvent(ShopEvent.MENU_SELECTED));});
}
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout />
</s:layout>
<s:VGroup name="top">
<hasu:ShopPlayerAttributesView id="attribsComp"/>
<s:Group id="menuContainer" name="menus">
<s:layout>
<s:HorizontalLayout />
</s:layout>
</s:Group>
</s:VGroup>
<s:Group>
<s:layout>
<s:HorizontalLayout />
</s:layout>
<s:Button label="<" />
<s:Group id = "itemSlotsContainer" name="items">
<s:layout>
<s:TileLayout requestedColumnCount="3" requestedRowCount="3"/>
</s:layout>
</s:Group>
<s:Button label=">" />
</s:Group>
</s:Group>
そのpittyは私のために働く。私がクローン()で行ったことは単にインスタンス 'this'を返すことです。しかし何かが間違っている、私のリスターイベントは全く発射していない。 (新しいインスタンスを作成し、新しいインスタンス(クローンされていない)を渡す場合) –
私はドキュメントを読んでいます。私は新しいインスタンスを作成し、すべてのプロパティをコピーする必要があります。なぜこのAS(s)がそれをそのように実装したのですか? –