이것은 묻는 질문 here과 비슷합니다. 나는 "ShopEvent"사용자 정의 이벤트를 전달하고하지만 난 오류 을 얻고있다 "강제 형 변환이 실패 com.events.ShopEvent에 flash.events::[email protected]을 변환 할 수 없습니다"사용자 정의 구성 요소에서 사용자 정의 이벤트를 분리하는 Flex 4
참고 : 오류가 부모로부터 발생합니다 사용자 지정 구성 요소 (세 번째 코드 단편), 여기에 세부 정보를 추가했습니다.
이것은 내 사용자 지정 이벤트입니다. 첫 번째 상수를 참조하십시오. 사용자 정의 구성 요소에 이벤트 이름을 붙여 넣습니다.
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 이벤트를 수신하면 이벤트를 listener로 라우트합니다. meatadata 태그를 확인하십시오. 이벤트 등록이 올바르게 완료되었습니다.
그러나 오류 내 지식
menus[i].addEventListener(ShopEvent.MENU_SELECTED,function(e:ShopEvent):void{dispatchEvent(e);});
에서오고, 나는 코드에서 어떤 문제를 볼니까. 거기에 문제가 있습니까?
업데이트
놀랍게도, 나는 "새로운"shopwevent의 인스턴스가 해결할 문제, 을 생성하지만, 슬프게도, 나는 이벤트 객체의 속성이 모두 종료해야하는 경우. 이것이 flex의 한계가 아니기를 바랍니다.
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>
그 끔찍한지만 나를 위해 작동합니다. 복제본()에서 수행 한 작업은 'this'인스턴스를 반환하는 것입니다. 그러나 뭔가 잘못되어 가고있는 내 리스터 이벤트는 전혀 발사되지 않습니다. (새 인스턴스를 만들고 새 인스턴스를 전달하면 복제되지 않습니다.) –
설명서를 읽었습니다. 정말 어색한데, 나는 새 인스턴스를 만들고 모든 속성을 복사해야합니다. 왜이 AS (s)가 그런 식으로 구현 했습니까? –