Failing to import a class in ActionScript - actionscript-3

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.

Related

Code behind for a flex application

I'm working on Flex project and having problems with "connecting" the code-behind to the mxml file (It actually worked before in another project). Both files are in the default package.
Hydw.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" minWidth="955" minHeight="600" xmlns="*">
<s:TextArea id="txt_log" x="34" y="171" width="225" height="217"/>
</s:Application>
Hydw.as:
package
{
import flash.events.*;
import flash.external.*;
import flash.media.*;
import mx.controls.TextArea;
import mx.core.*;
import mx.events.*;
import spark.components.*;
public class Hydw extends spark.components.Application
{
public var txt_log:spark.components.TextArea;
public function Hydw ()
{
super();
addEventListener(FlexEvent.CREATION_COMPLETE, this.creationCompleteHandler);
}
private function creationCompleteHandler(param1:FlexEvent) : void
{
WriteToLog("creationCompleteHandler");
}
public function WriteToLog(s:String) : void
{
txt_log.text += s + "\n";
}
I run the application (after releasing) and I see nothing in the TextArea. Why?
By the way, I'm having trouble with the debugging for now, so I can't tell where's the failure exactly.
Obviously it didn't work. There need to make some changes in ActionScript and mxml file.
First: Remove package and class from ActionScript file like:
import mx.events.FlexEvent;
public function creationCompleteHandler(param1:FlexEvent) : void
{
WriteToLog("creationCompleteHandler");
}
public function WriteToLog(s:String) : void
{
txt_log.text += s + "\n";
}
Because It is in default package there isn't required to defined package and class.
Second:
Remove public var txt_log:spark.components.TextArea; from as file. Because it will conflict txt_log with id of textArea in mxml file.
Third:
Remove addEventListener(FlexEvent.CREATION_COMPLETE, this.creationCompleteHandler); from as file and give creation complete event in mxml file. like:
<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"
creationComplete="creationCompleteHandler(event)">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script source="Hydw.as" />
<s:TextArea id="txt_log" x="34" y="171" width="225" height="217"/>
</s:Application>
And another thing is you forget to include as file inside mxml. like:
<fx:Script source="Hydw.as" />
Hope you understand and help to move forward.
This is what you want
Hydw.mxml
<?xml version="1.0" encoding="utf-8"?>
<abstract:Hydw 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:abstract="test.pack.abstract.*"
minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:TextArea id="txt_log" x="34" y="171" width="225" height="217"/>
</abstract:Hydw>
and your Hydw.as:
package test.pack.abstract
{
import mx.events.FlexEvent;
import spark.components.Application;
import spark.components.TextArea;
[Bindable]
public class Hydw extends Application
{
public var txt_log:TextArea;
public function Hydw()
{
super();
addEventListener(FlexEvent.CREATION_COMPLETE, init);
}
public function init(evt:FlexEvent):void
{
}
}
}
any visual component used in .mxml code you want to use in .as class
must be declared as public binded variable in your .as class or simply declare your .as class as [Bindable]
That's All

Actionscript Event Error

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");
}

Embed Sound in CSS in flex

I am trying to use sound in flex. I got the output using urlrequest, as I have less sound files to use, and need not to load everytime i call it. So I tried to put that in css and use it, but I am getting an error : TypeError: Error #1007: Instantiation attempted on a non-constructor.
Below is my code
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Style>
draw
{
url:"Assets/Sound/Active.mp3";
}
</mx:Style>
<mx:Script>
<![CDATA[
import flash.media.Sound;
import flash.media.SoundTransform;
internal var sound:Sound;
internal var soundVolume:SoundTransform;
public function playSound():void
{
var SoundClass:Class;
try
{
SoundClass = StyleManager.getStyleDeclaration("draw").getStyle("url") as Class;
soundVolume = new SoundTransform(1, 0);
trace("sound : "+sound);
sound = new SoundClass() as Sound;
sound.play(0, 0, soundVolume);
}
catch(E:Error)
{
trace("E "+E);
}
}
]]>
</mx:Script>
<mx:Button click="playSound()" label="Discard"/>
</mx:Application>
draw {
url:Embed(source="Assets/Sound/Active.mp3");
}
Refer this

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"