Actionscript Event Error - actionscript-3

I try to dispatch event from actionscript class. But I get the error: "Error #1034: Type Coercion failed: cannot convert flash.events::Event#9f849c1 to mx.events.FlexEvent."
Here is my code:
MXML Code:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init(event)">
<fx:Script>
<![CDATA[
import com.testing.package.MyASClass;
import mx.events.FlexEvent;
public var sv:MyASClass;
protected function init(event:FlexEvent):void
{
sv = new MyASClass();
sv.addEventListener("myevent",mainfunc);
sv.myfunc();
}
protected function mainfunc(event:FlexEvent):void
{
trace("receive event");
}
]]>
</fx:Script>
</s:WindowedApplication>
Actionscript file:
package com.testing.package
{
import flash.display.Sprite;
import flash.events.Event;
public class MyASClass extends Sprite
{
public function MyASClass()
{
}
public function myfunc():void
{
dispatch();
}
private function dispatch():void
{
dispatchEvent(new Event("myevent"));
}
}
}
Why the error occur? How can I fix it?
Thank you.

//Solution is need replace FlexEvent into Event
protected function mainfunc(event:Event):void{
trace("receive event");
}

Related

Flex Actionscript call parent function

I have an actionscript file in my flex project. I loaded the actionscript into flex via addElement()
MXML File:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
public var sv:Myastest;
protected function init(event:FlexEvent):void
{
sv = new Myastest();
addElement(sv);
sv.classfunc();
}
public function mainfunc():void
{
trace("mainfunc called");
}
]]>
</fx:Script>
</s:WindowedApplication>
ActionScript File:
package
{
import flash.events.Event;
import mx.core.UIComponent;
[SWF(frameRate="25", backgroundColor="#000000")]
public class Myastest extends UIComponent
{
public function Myastest()
{
trace("loaded..");
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
trace("added to stage");
}
public function classfunc():void
{
trace("classfunc called");
}
}
}
How can I call mainfunc() from the actionscript file? Thank you.
IN THE CLASSMANAGEREVENT
public class ManagerEvent
{
public static const EVENT_MAIN_CLASS_FUNC:String = UIDUtil.createUID();
FlexGlobals.topLevelApplication.addEventListener(EVENT_MAIN_CLASS_FUNC, executeMainClassFuncCommand)
public function executeMainClassFuncCommand(event:MainClassFunc):void
{
var cmd:FUNCTIONCommand = new FUNCTCommand();
cmd.execute(event);
}
}
CLASS EVENT
public class MainClassFunc extends Event
{
public function MainClassFunc()
{
super(ManagerEvent.EVENT_MAIN_CLASS_FUNC);
}
}
IN THE CHILD
var mainClassFunc:MainClassFunc = new MainClassFunc();
FlexGlobals.topLevelApplication.dispatchEvent(mainClassFunc);
To expand on RIAStar's comment, you could do this:
MXML File:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
public var sv:Myastest;
protected function init(event:FlexEvent):void
{
sv = new Myastest();
sv.addEventListener(Myastest.EVENT_NAME, mainfunc);
addElement(sv);
sv.classfunc();
}
public function mainfunc(e:Event):void
{
trace("mainfunc called");
}
]]>
</fx:Script>
</s:WindowedApplication>
ActionScript File:
package
{
import flash.events.Event;
import mx.core.UIComponent;
[SWF(frameRate="25", backgroundColor="#000000")]
public class Myastest extends UIComponent
{
public static const EVENT_NAME:String = "EVENT_NAME";
public function Myastest()
{
trace("loaded..");
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
trace("added to stage");
}
public function classfunc():void
{
trace("classfunc called");
//assuming you want the mainfunc to get called from here
dispatchEvent(new Event(EVENT_NAME));
}
}
}
Please note that you need to call removeEventListener before you set sv = null.

Create Custom Dialog Box using dispatch events,states and titlewindow in flex/AS3.0

Since a month Iam working on creating a custom dialog box, having parameters like message,state and modal(true/false)
Eg:
showAlert("Hi, how are you doing","Goodmorning", true);
I have learned how to dispatch event. but unable to dispatch the alertevent/popupManager using States.
Below is the code, I am struggling with.
Main.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
public function onclick(event:MouseEvent):void
{
this.dispatchEvent("Hi, How are you?", "Morning", true);
}
public function dispatchEvent(arg1:String, arg2:String, arg3:Boolean):void
{
var str:*=null;
str.message = arg1;
str.stateName = arg2;
str.modal = arg3;
this.dispatchEvent(new ShowAlert(ShowAlert.SHOW, true));
}
]]>
</mx:Script>
<mx:Button id="click" click="onclick(event)"/>
</mx:Application>
ShowAlert.as
package
{
import flash.events.*;
public class ShowAlert extends Event
{
public var _stateName:String;
public var _closable:Boolean;
public var _message:String;
public var _isModal:Boolean;
public static const SHOW:String="show";
public function flash.events.(arg1:String, arg2:Boolean=false, arg3:Boolean=false)
{
super(arg1, arg2, arg3);
trace("arg1: "+arg1+"\t arg2: "+arg2+"\t arg3: "+arg3);
}
public function set message(arg1:String):void
{
this._message = arg1;
}
public function get message():String
{
return _message;
}
public function get modal():Boolean
{
return _isModal;
}
public function get stateName():String
{
return _stateName;
}
public function set stateName(arg1:String):void
{
this._stateName = arg1;
}
public function set modal(arg1:Boolean):void
{
this._isModal = arg1;
}
public override function clone():flash.events.Event
{
return new ShowAlert(type);
}
}
}
I was unable to write the custom titlewindow using states, so I dint post that.
Please let me know, how to make this happen.
Below is the Sample code, specifying my problem
main.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public var info:IModuleInfo;
public var loadalert:DisplayObject;
import mx.modules.ModuleManager;
import mx.modules.IModuleInfo;
import mx.events.ModuleEvent;
public function init():void
{
Alert.show("This is forst alert.");
}
public function Loadalerrt():void
{
trace("loadalertmodule");
info = ModuleManager.getModule("../bin-debug/alerrtmod.swf");
info.addEventListener(ModuleEvent.READY, modEventHandler);
info.load();
}
public function modEventHandler(event:ModuleEvent):void
{
trace("modeventHandler");
loadalert=info.factory.create() as DisplayObject;
can1.addChild(loadalert);
}
]]>
</mx:Script>
<mx:Button label="Alert in Application" id="b1" click="init()" x="29" y="21"/>
<mx:Button label="Load Module" id="b2" click="Loadalerrt();" x="10" y="92"/>
<mx:Canvas id="can1" x="409" y="57" backgroundColor="cyan"/>
</mx:Application>
alerttmod.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public function alertshow():void
{
Alert.show("This is second alert, You Can see it covers whole application, What I want is its scope should be limited to this specific module, not to whole application ");
}
]]>
</mx:Script>
<mx:Button label="Alert in Module" id="b1" click="alertshow()" x="163" y="100"/>
</mx:Module>
Now I see your problem. I think it is not possible to restrict the modal area of the Alert. I can suggest you to fake the modal behaviour by desabling the modules elements while your dialog is being shown.
May be it can help you...
Here are two components to demonstrate it:
//your module with another Alert
<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
public function alertshow():void
{
Alert.show("This is second alert...");
}
public function alertshow2():void
{
var customAlert:CustomAlert = new CustomAlert();
customAlert.addEventListener("CLOSED", onCustomAlertClosed);
this.enabled = false;
PopUpManager.addPopUp(customAlert, this, false);
PopUpManager.centerPopUp(customAlert);
}
private function onCustomAlertClosed(evt:Event):void
{
this.enabled = true;
}
]]>
</mx:Script>
<mx:Button label="Alert in Module" id="b1" click="alertshow()" x="163" y="100"/>
<mx:Button label="CustomAlert in Module" id="b2" click="alertshow2()" x="163" y="150"/>
<mx:Canvas width="100%" height="100%" backgroundColor="0xbbbbbb" alpha="0.8" visible="{!this.enabled}"/>
</mx:Module>
//simple CustomAlert as a Panel
<?xml version="1.0" encoding="utf-8"?>
<mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="300" height="200">
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
protected function button1_clickHandler(event:MouseEvent):void
{
PopUpManager.removePopUp(this);
this.dispatchEvent(new Event("CLOSED"));
}
]]>
</mx:Script>
<mx:Button x="112" y="128" label="Close" click="button1_clickHandler(event)"/>
</mx:Panel>

Why is a static method giving me an error

Call to a possibly undefined method test through a reference with static type Class.
here is my class
package com.singleton.sample{
public class SampleSingleton{
public static function test( ):void{
trace('hello world')
}
}
}
and here is my mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init()">
<mx:Script>
<![CDATA[
import com.singleton.sample.SampleSingleton;
public function init():void{
SampleSingleton.test() // error on this line
}
]]>
</mx:Script>
</mx:Application>
Please ignore the singleton references in the naming as I stripped down my class to this and it still doesn't work.
[EDIT]
import com.singleton.sample.SampleSingleton;
public function init():void{
SampleSingleton.test(); // this gives me the error
com.singleton.sample.SampleSingleton.test(); // this works
}
The Application file is named SampleSingleton i bet so you have a name collision. Rename the application.

Drag and Drop in Spark tilelist

I am trying to initiate a drag&drop on a spark tilelist so the user can re-order the list manually.
The problem is : each time i drap and drop, the dropped/dragged entry is duplicated.
My main application is :
<?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" minWidth="955" minHeight="600"
initialize="initApp()">
<fx:Script source="com/init.as" />
</s:Application>
Included as file is :
import com.Sequence;
import mx.collections.ArrayCollection;
import mx.core.ClassFactory;
import mx.core.FlexGlobals;
import spark.components.List;
import spark.layouts.TileLayout;
public var main:List=new List()
public var ok: ArrayCollection = new ArrayCollection( [
{label:"1-redblue", data:"redblue"},
{label:"2-ncview", data:"ncview"},
{label:"3-greyscale", data:"greyscale"},
{label:"4-alg2", data:"alg2"},
{label:"5-alg", data:"alg"},
{label:"6-occam", data:"occam"},
{label:"7-rainbow", data:"rainbow"},
{label:"8-sst_36", data:"sst_36"},
{label:"9-occam_pastel-30", data:"occam_pastel-30"},
{label:"10-ferret", data:"ferret"}
]);
// ActionScript file
public function initApp(){
var lay:TileLayout=new TileLayout()
var ae:ClassFactory=new ClassFactory(Sequence)
main.layout=lay
main.dataProvider=ok
main.dragEnabled=true
main.dropEnabled=true
main.width=FlexGlobals.topLevelApplication.width
main.height=FlexGlobals.topLevelApplication.height
main.itemRenderer=ae
this.addElement(main);
}
And my item renderer looks like :
package com
{
import mx.controls.TextArea;
import mx.events.FlexEvent;
import spark.components.BorderContainer;
import spark.components.supportClasses.ItemRenderer;
public class Sequence extends ItemRenderer
{
private var borderC:BorderContainer=new BorderContainer()
private var labeli:TextArea=new TextArea()
private var d:Object
public function Sequence()
{
super();
this.addElement(borderC)
this.addEventListener(FlexEvent.CREATION_COMPLETE,doIT)
borderC.width=borderC.height=100
labeli.width=labeli.height=100
borderC.addElement(labeli)
}
override public function set data(d:Object):void{
this.d=d
}
override public function get data():Object{
return d;
}
private function doIT(e:FlexEvent):void{
labeli.text =String(d.label);
}
}
}
The property that allows to drag/drop within the same component (without duplicating) is :
allowDragMove
if set to true, the duplication does not occurs.
#gpasse got me on the right track.
The following fields need to be set to true for a Spark List in Flex 4.6.
dragEnabled="true"
dragMoveEnabled="true"
dropEnabled="true"

Failing to import a class in ActionScript

Here is the error I get:
1046: Type was not found or was not a compile-time constant: fbAPI.
Here is my MXML:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="startGame();">
<mx:Script>
<![CDATA[
import fbAPI;
public function startGame():void {
var fbAPI:fbAPI = new fbAPI(); // breaks on this line
fbAPI.fbLogin();
}
]]>
</mx:Script>
</mx:Application>
And here is my fbAPI.as stub that doesn't seem to get imported:
package {
public class fbAPI {
import mx.controls.Alert;
public function fbLogin():void {
Alert.show('test');
}
}
}
Try putting your import statements above your class and also just rename the instance name of the fbapi in your mxml real quick.
Edit: nevermind, I forgot in AS3 you don't need a constructor.
Make sure you put the fbAPI.as file in the same location as your mxml file.