Why is a static method giving me an error - actionscript-3

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.

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

Dynamically selecting a static embedded image?

Here's my current situation:
I have images embedded on a class.
package system
{
public class Embedded
{
[Embed(source="assets/srcorangeboxidle.png")]
public static const btnSrcOrangeBoxIdle:Class;
[Embed(source="assets/srcorangeboxpressed.png")]
public static const btnSrcOrangeBoxPressed:Class;
[Embed(source="assets/hl1idle.png")]
public static const btnHL1Idle:Class;
[Embed(source="assets/hl1pressed.png")]
public static const btnHL1Pressed:Class;
public function Embedded(){}
}
}
Now on my main MXML file, I have the following.
<?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">
<fx:Script>
<![CDATA[
import system.Embedded;
protected function toggleButtonState(target:Object,pressed:Boolean=false):void
{
var baseImageName:String = target.id.toString();
if (!pressed) {
target.source = Embedded.[baseImageName+"Idle"];
} else {
target.source = Embedded.[baseImageName+"Pressed"];
}
}
]]>
</fx:Script>
<s:Image id="btnSrcOrangeBox" x="107" y="245" source="{Embedded.btnSrcOrangeBoxIdle}" mouseDown="toggleButtonState(btnSrcOrangeBox,true)" mouseUp="toggleButtonState(btnSrcOrangeBox,false)"/>
<s:Image id="btnHL1" x="107" y="355" source="{Embedded.btnHL1Idle}" mouseDown="toggleButtonState(btnHL1,true)" mouseUp="toggleButtonState(btnHL1,false)"/>
</s:Application>
As you can tell, the above codes don't seem to do the trick. I just want to dynamically select which Embedded.* class to select. Any help or hint will be greatly appreciated.
Remove the dot after the class name. The following should work:
target.source = Embedded[baseImageName + "Idle"];

changing component properties of SkinnableComponent in Actionscript

Flex 4 separates the visual components into the skins. So how do we access those visual elements from Skinnable component? Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<s:SkinnableComponent xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" skinClass="skins.brushedSkin"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.controls.TextInput;
private var txt:String;
public function setText(s:String) {
txt = s;
// the following line doesn't work
var input:TextInput = this.skin.getChildByName("msg") as TextInput;
input.text = s;
}
]]>
</fx:Script>
</s:SkinnableComponent>
I just need to set the text in the TextInput in the brushedSkin skin. But I have no idea how to do this in Flex 4.
First you must specify in your SkinnableComponent the so called Designer-Developer Contract.
Then you should wait for your component to finish its instantiation in order to access its skin parts.
In your particular case you change your code in the following way:
<?xml version="1.0" encoding="utf-8"?>
<s:SkinnableComponent xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" skinClass="skins.brushedSkin"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.controls.TextInput;
[SkinPart(required="true")]
public var input:TextInput;
private var txt:String;
public function setText(s:String) {
txt = s;
if (initialized)
input.text = txt;
}
]]>
</fx:Script>
</s:SkinnableComponent>
Then ensure that your skin class contains the following declaration (probably you just need to rename the msg-TextInput to input):
<s:TextInput id="input"/>

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.