How to use external actionscript classes in FlashBuilder (thought I knew) - actionscript-3

I'm trying to implement CSVLib in an Air application and am getting an error that seems wholly illogical to me.
"1120: Access of undefined property csv."
and
"1120: Access of undefined property completeHandler."
The only thing I can think is it's not importing the csv class properly, or the class itself is broken somehow? I know my import path is correct because I typed it out directly based on automatic hinting. The code below is copied directly from the how-to wiki on the csv lib site.
Or is there something special you need to do to get external actionscript classes to work in flashbuilder?
<fx:Script>
<![CDATA[
import com.shortybmc.*;
import com.shortybmc.data.parser.CSV;
var csv:CSV = new CSV();
csv.addEventListener (Event.COMPLETE, completeHandler);
csv.load (new URLRequest('example-2.csv'));
function completeHandler (event: Event)
{
trace ( csv.data.join('\r') );
// do something ...
}
]]>
</fx:Script>

In this case, the problem is somewhere else. The fx:Script tag is within a MXML file, which represents a class definition.
Your error happens, because you have code within the class definition (i.e. outside of a method). You can write this instead for example:
<fx:Script>
<![CDATA[
import com.shortybmc.*;
import com.shortybmc.data.parser.CSV;
private var csv:CSV;
private function init ():void
{
csv = new CSV();
csv.addEventListener (Event.COMPLETE, completeHandler);
csv.load (new URLRequest('example-2.csv'));
}
private function completeHandler (event: Event):void
{
trace ( csv.data.join('\r') );
// do something ...
}
]]>
</fx:Script>
Then you need to make sure that the init method is actually called; you can do this in the complete handler of your MXML object.

Related

ActionScript: making a variable `[Bindable]` causes crashes

I have this singleton that I'm using as a wrapper for global variables and constants, but as soon as I make some [Bindable] I get a crash on start up w/a bunch of red text in my console.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at BrandGlobals$/get COLOUR_EVERYTHING_BACKGROUND()[C:\MyProject\src\BrandGlobals.as:14]
at BrandGlobals$cinit()
at global$init()[C:\MyProject\src\BrandGlobals.as:2]
at _mainWatcherSetupUtil/setup()
at main/initialize()[C:\MyProject\src\main.mxml:0]
at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::childAdded()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:2131]
at mx.managers::SystemManager/initializeTopLevelWindow()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3400]
at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::docFrameHandler()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3223]
at mx.managers::SystemManager/docFrameListener()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3069]
BrandGlobals:
package {
public final class BrandGlobals {
[Bindable]public static var COLOUR_EVERYTHING_BACKGROUND:uint = 0xE010FF;
If I remove that [Bindable] and turn var to const there's no problem (except the obvious problem of not being able to set the variable outside of this file) but this doesn't work. Also, making the whole class [Bindable] instead of this one didn't work. When I hover my mouse over the COLOUR_EVERYTHING_BACKGROUND definition, it says "<exception thrown by getter>". 'Don't know what to think about that.
I might have guessed it was because it has no package, but I'm using another similar singleton which has [Bindable] variables and seems to work fine.
I never did get that [Bindable] twaddle.
I'm using the Flex 3.5 SDK.
I tried Brian's suggestion below, but it gave me pretty much the same error. I even tried:
{
_COLOUR_EVERYTHING_BACKGROUND = 0xE010FF;
trace("Var set."); //Breakpoint here
bLoadedFerCryinOutLoud = true;
}
[Bindable]private static var _COLOUR_EVERYTHING_BACKGROUND:uint;
private static var bLoadedFerCryinOutLoud:Boolean = false;
public static function get COLOUR_EVERYTHING_BACKGROUND():uint {
trace("Returning EVERYTHING background");
if (bLoadedFerCryinOutLoud)
return _COLOUR_EVERYTHING_BACKGROUND;
else return 0xFFFFFF;
}
What's more, if I put a breakpoint at that trace("Var set.");, Flash Builder complains that a break is not possible, because there is no executable code there.
I also noticed that in that call stack that I'm shown when this crash happens during a set and it seems to be the one that sets _COLOUR_EVERYTHING_BACKGROUND. But the only place where it is set is:
public static function SetBackground(oApp:UBIApplication):void {
_COLOUR_EVERYTHING_BACKGROUND = oApp.nBackgroundColour;
}
and breakpoints indicate that this is never called.
The documentation on using the tag has the following to say:
Using static properties as the source for data binding
You can use a static variable as the source for a data-binding expression. Flex performs the data binding once when the application starts, and again when the property changes.
You can automatically use a static constant as the source for a data-binding expression. Flex performs the data binding once when the application starts. Because the data binding occurs only once at application start up, you omit the [Bindable] metadata tag for the static constant. The following example uses a static constant as the source for a data-binding expression:
<fx:Script>
<![CDATA[
// This syntax casues a compiler error.
// [Bindable]
// public static var varString:String="A static var.";
public static const constString:String="A static const.";
]]>
</fx:Script>
<!-- This binding occurs once at application startup. -->
<s:Button label="{constString}"/>
Edit: You need to make sure that your variable is initialized before you try to read it. A static initializer is the way to go:
package {
public final class BrandGlobals {
{
_COLOUR_EVERYTHING_BACKGROUND = 0xE010FF;
trace("Var set."); //Breakpoint here
}
[Bindable]private static var _COLOUR_EVERYTHING_BACKGROUND:uint;
public static function get COLOUR_EVERYTHING_BACKGROUND():uint {
trace("Returning EVERYTHING background"); //Breakpoint here
return _COLOUR_EVERYTHING_BACKGROUND;
}
Putting in breakpoints in the places specified will let you verify that things are executing in the expected order
It turns out that the problem was assigning COLOUR_EVERYTHING_BACKGROUND to a static const elsewhere in the code, as a temporary measure. Hopefully I'll remember that assigning [Bindable]s to static consts is bad and if I don't, I'll remember the meaning of that particular cryptic reaction Flash Builder had. I'm starting to choke StackOverflow w/my questions about cryptic error messages.

How do I use Actionscript "Classes" with a Flex MXML file?

so far I've been completely stumped by this and I've been a few days at it, and most links I've followed and searches I've done have led me to nothing.
I want to make a simple game, with a mouse interface, but I also wanted to add a preloader. I was initially using minibuilder, as it is cross-platform and I am on Linux, but all the tutorials I saw to add a preloader seemed to be incompatible with it.
Thus I moved to just using the Flex compiler and a text-editor directly, but I haven't had much luck, and even the preloader (which seems to be the only think that actually works) is a mere copy from a tutorial that, by chance, worked.
Ideally, I'd want to merely use the MXML file to point to the preloader - having a CustomPreloader.as file for the preloader - and to start the Actionscript classes, possibly using FlashPunk along with my code to help.
This is code so far, for each of the files except CustomPreloader.as, as the preloader is already working: (Note: All files are in ~/ASClasses/src )
File: ASClasses.mxml
--------------------
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
backgroundColor="#333333"
creationComplete="init();"
width="800" height="500"
frameRate="60"
preloader="CustomPreloader"
>
<mx:Script>
<![CDATA[
//This part is mostly for testing purposes
//========================================
import mx.controls.Alert;
public function init():void {
Alert.show("The first function works.");
}
//And this is what I actually wanted to do
//========================================
import Application;
//Whenever I uncomment the following line, some error is thrown and the init function stops working at all.
//public var myApp:Application = new Application;
//addChild(myApp);
]]>
</mx:Script>
</mx:Application>
File: Application.as
--------------------
package{
import flash.display.Shape;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.display.Sprite;
public class Application extends Sprite{
public function Application(){
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.frameRate = 60;
rotationX = -45;
var s:Shape = new Shape;
s.x = 400;
s.y = 200;
s.rotation = 90;
addChild(s);
}
addEventListener('enterFrame', function(e:Event):void{
s.rotation += 2.5;
} );
}
}
However, uncommenting the line required to add Application.as seems to just throw an error, so I think that I am either missing some code or that I did something wrong.
Is there anyone who could teach me more about this, please? And although I'd like to say that I have some experience with Actionscript, at this point I've already stressed myself out so much about being unable to do this that I'd rather, if it's not too much to ask, be explained in a simple way, assuming I have no previous knowledge.
Additionally, if there are any full-blown simple tutorials for making a simple/simplistic game/demo this way, I'd appreciate that as well, as most tutorials I've seen so far only document Flex and Actionscript, and easily get complicated before I've actually managed to do anything.
Thanks in advance.
Edit 1: Also, it may be worth mentioning that the way it currently is it still manages to throw the Alert after loading.
Try
this.rawChildren.addChild(myApp);
Instead of
addChild(myApp);
Application extends Container, And When you add a child to Container, the child should implement the IUIComponent interface. Because the sprite class doesn't implements the interface, it will give you an error.
Here is some info about this Container addChild
EDIT
put the adding application code into a function,Like
public function init():void {
Alert.show("The first function works.");
var myApp:Application = new Application();
this.rawChildren.addChild(myApp);
}

How to convert a two-file preloader and game swf with Document class into single swf?

I have a game that I've made that runs in two files, one the preloader and one the swf. When I use these two files together they work fine, however some flash portals require only one SWF, so I'm trying to find the shortest-path way to convert it to use only one.
The game swf uses a Document Class that includes the package, class constructor, and tons of functions, and runs fine. I have lots of movieclips on the stage that I refer to in my code. The code is something like this (abridged):
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
...
public class Game extends MovieClip {
var EnemyArray:Array;
var HeroArray:Array;
...
public function Game() { // class constructor
EnemyArray = new Array();
addEventListener(Event.ENTER_FRAME,onEnterFrame);
mainRestartButton.addEventListener(MouseEvent.CLICK, RestartLevel);
...
}
public function KeyPressed(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
{
attack();
...
}
} // End of class
} // End of package
I also have another swf, my preloader, which has the following code in frame 1:
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.events.Event;
var myLoader:Loader = new Loader();
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoading);
var myURL:URLRequest = new URLRequest("Game.swf");
myLoader.load(myURL);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
function onLoading(event:ProgressEvent):void {
var loaded:Number = event.bytesLoaded / event.bytesTotal;
percent_txt.text = "Loading: " + (loaded*100).toFixed(0) + "%";
}
function onComplete(event:Event):void {
myLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoading);
myLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
this.addChild(myLoader);
}
I've been through the gamut of solutions online and none of them seem to work with the way that my code is set up, such as: How to create Preloader in AS3
I tried his option 1 by taking all the code in my Document Class other than the package and constructor and pasted it into frame 2. However it returns "Error #1009: Cannot access a property or method of a null object reference." for any references to items on the stage, such as:
mainRestartButton.addEventListener(MouseEvent.CLICK, RestartLevel);
As he mentions, "This works well if you organized your project in a way that the most of the assets (images, etc) are in the Flash IDE Library and are not loaded on the first frame (you can check that in each library item's properties)." My project is NOT organized in such a way.
What is the easiest way for me to reorganize my FLA/code so they can have a preloader in a single swf?
Thanks so much for your time and help!
I use FlashDevelop (no FLAs) so I'm not sure if it'll work for you but I use the solution described here.

AS3Eval library is complaining about "Variable compile is not defined"

I'm working with FlashBuilder 4.6 and using AS3Eval v0.3 library from: http://eval.hurlant.com/
The library is fully working in one of my Flex 4.6.0 projects but not in the other (same library linkage "Merge into code" of EvalES4.swc library).
In the first project following code works:
private var compiler:CompiledESC = new CompiledESC;
public function compile(code: String) : ByteArray {
return compiler.eval(code);
}
In the other project, it fails with:
ReferenceError: Error #1065: Variable compile is not defined.
The error is referring to following line in the AS3Eval library (that EvalES4.swc file)
var compile:Function = getDefinitionByName("ESC::compile") as Function;
Looks like the library has problem to fully load Tamarin ESC in the other project.
I've checked that both projects (they are using the same library... but still checked) load the Tamarin ESC successfully via
// inside CompiledESC.as
private function loadESC():void {
var a:Array = [
new debug_abc as ByteArray,
new util_abc as ByteArray,
new bytes_tamarin_abc as ByteArray,
new util_tamarin_abc as ByteArray,
new lex_char_abc as ByteArray,
new lex_scan_abc as ByteArray,
new lex_token_abc as ByteArray,
new ast_abc as ByteArray,
new parse_abc as ByteArray,
new asm_abc as ByteArray,
new abc_abc as ByteArray,
new emit_abc as ByteArray,
new cogen_abc as ByteArray,
new cogen_stmt_abc as ByteArray,
new cogen_expr_abc as ByteArray,
new esc_core_abc as ByteArray,
new eval_support_abc as ByteArray,
new esc_env_abc as ByteArray,
]
ByteLoader.loadBytes(a, true);
}
These ByteArray classes are embedded inside the SWC using [Embed] (looking through the library code) and all ByteArrays are in both cases initialized and loaded via ByteLoader.
So far, I have not found any clue why compiling is working for my first project but not for the other. Does anybody has similar experiences or some hints what may cause the problem?
Best,
Jakub
Ha!
I was too desperate, solution found.
It appears, that CompiledESC cannot be used in the same frame where it was created.
So in the other words, something like this won't work:
public function compile(code: String) : ByteArray {
var compiler:CompiledESC = new CompiledESC();
return compiler.eval(code);
}
But following code will work (application loads, initializes CompileESC and then user clicks the RUN! button, which happens in different frame from the one where CompileESC was created):
<?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"
>
<fx:Script>
<![CDATA[
import com.hurlant.eval.CompiledESC;
import mx.events.FlexEvent;
private var compiler:CompiledESC = new CompiledESC();
protected function button1_clickHandler(event:MouseEvent):void
{
compiler.eval("trace(\"hello!\")");
}
]]>
</fx:Script>
<mx:Button label="RUN!" click="button1_clickHandler(event)" />
</s:Application>
I do not know exactly why, but I suspect it has something to do with the way Flash is loading its bytecode. When you try to insert some code into VM and use it in the same frame you're asking for troubles because your code has just not been loaded yet. You have to "yield" the thread and wait for another frame, somewhere in-between Tamarin ESC code gets loaded.
Note that the same applies for compiled code being loaded via ByteBuffer by you! Whenever you do something like
ByteLoader.load(compiler.eval(myAS3Code));
Do not expect that myAS3Code will be executed right away. Again, the code will be loaded whenever you "yield" the thread and let VM to truly load your code.
Hope this helps someone in the same situation.
Best,
Jakub
You should check if the permissions are the same of first project. You're merging (if I understood right) a library from outside, is this allowed?

Saving XML file in AS3 is possible

var xml:XML = <myXml>
<item prop="1" />
<item prop="2" />
</myXml>;
I need to save as xml file in local harddisk(project directory).
Is it possible to save in as3 itself?
I threw this together, and sure enough you can save to .XML using the following as a minimalist example.
package com.hodgedev.xmlcreator
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.ByteArray;
import flash.net.FileReference;
/**
* ...
* #author Brian Hodge (brian#hodgedev.com)
*/
public class Main extends Sprite
{
private var _xml:XML;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//Calling the save method requires user interaction and Flash Player 10
stage.addEventListener(MouseEvent.MOUSE_DOWN, _onMouseDown);
_xml= <xml>
<test>data</test>
</xml>;
}
private function _onMouseDown(e:MouseEvent):void
{
var ba:ByteArray = new ByteArray();
ba.writeUTFBytes(_xml);
//ba.
var fr:FileReference = new FileReference();
fr.addEventListener(Event.SELECT, _onRefSelect);
fr.addEventListener(Event.CANCEL, _onRefCancel);
fr.save(ba, "filename.xml");
}
private function _onRefSelect(e:Event):void
{
trace('select');
}
private function _onRefCancel(e:Event):void
{
trace('cancel');
}
}
}
There are some things to note.
You require Flash Player 10 to use the save method of the FileReference class.
In order to do anything that INVOKES a prompt, Flash requires user interaction like keyboard or mouse input.
In the above I listen for MouseEvent.MOUSE_DOWN on the stage to serve as the USER INTERACTION which is required to invoke the save prompt.
I setup a basic XML structure within the code (this will typically come from and external source and will work fine both ways.
A ByteArray is created and the XML is written to the ByteArray.
The save method of the FileReference class requires a ByteArray and default save name be passed as the two parameters.
I hope this helps.
if you want to store it locally (on the client PC) , you can use a local shared object. Refer to this tutorial
I'm sorry, your question isn't very clear.
Are you asking if you can save a file to the hard drive from within a compile SWF written in AS3?
Or are you asking if you can include a raw XML file in your AS3 project without needing to write it out as a variable?
If you meant the former, no -- not without Adobe AIR. You can save data locally as a SharedObject, but not as an arbitrary file in the file system.
If the latter, then yes -- you must embed the file just as you would embed another resource (such as an image or a sound). However, it looks like there might be a bug in Flash that makes this non-trivial to figure out how to do.
This link might be of help to you.
[Embed(source='../../../../assets/levels/test.xml', mimeType="application/octet-stream")]
public static const Level_Test:Class;
And then to parse the XML:
var ba:ByteArray = (new Levels.Level_Test()) as ByteArray;
var s:String = ba.readUTFBytes( ba.length );
xml = new XML( s );
Apologies if neither of those questions are what you were actually asking.
Cheers!