AS3 Access of undefined property error - actionscript-3

I have code that reads a JSON file to import the information about a level in a game I'm making. The code was running and compiling fine until suddenly every time I tried to build, FlashDevelop started erroring "build failed" without actually giving an error.
I ran the code through the mxmlc compiler, to give me an error message, and it returned an error saying "Error: access of undefined property JSON" in line:
var level:Object = JSON.decode(json);
This is confusing because the JSON library is clearly included at the top of the file, "import com.adobe.serialization.json.JSON;", and this error started occurring completely on it's own, which is odd...
package
{
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.ByteArray;
import net.flashpunk.Entity;
import com.adobe.serialization.json.JSON;
public class LevelParser
{
public static function GetLevelByID(ID:int, source:Class):Level
{
// Store new entity
var populated:Vector.<Entity> = new Vector.<Entity>();
// Parse file into string
var bytes:ByteArray = new source();
var json:String = bytes.readUTFBytes(bytes.length);
var level:Object = JSON.decode(json);
// Find correct level
...
EDIT: Strangely if I comment out the line to call it, and this function, the code compiled fine in mxmlc, but in Flashdevelop is says "Build Failed" with no error

This is actually a reference error.
As of AS3 SDK 4.5, JSON became a native class within the SDK. In previous versions, you would have to download the adobe serialize classes to access JSON - which may be your issue.
When using the mxmlc, it will compile with all the included libraries specified in your sdk flex-config.xml. In FlashDevelop, it will only use the classes you include.
Solution: add the adobe serialize class to your project

Related

Embedding png throws Error #1065: Variable FlexVersion is not defined

I've found several articles relating to variable FlexVersion and error 1065, but nothing seems to help.
EDIT:I've attempted to implement SEVERAL guides on embedding images into flashDevelop with the same result. Everything works correctly until I try to add the embedded image, then I get the above error.
Has no one here seen this error?
My Class (which I've stripped down to nothing in order to pinpoint the issue):
package {
import flash.display.Sprite;
import flash.display.Bitmap;
public class JMouse extends Sprite {
[Embed(source = "../lib/jacobsLadder.png")]
private var Picture:Class;
//private var pic:Bitmap = new Picture(); // THIS LINE
public function JMouse() {
init();
}
private function init():void {
}
}
throws the error when the line "THIS LINE" is not commented out. I've tried adding "as Bitmap" to the end of this line without luck.
I am calling this class from my document class:
jMouse = new JMouse();
the file, jacobsLadder.png, is in my lib folder, and I used FlashDevelop to "Generate Embed Code."
I am using FlashDevelop 5.0.1.3 for .NET 3.5
Any Ideas?
EDIT: I also tried this (and similar variations), as per the suggestion:
"The type of code you can run at variable declaration scope is limited. Creating an instance of Picture requires decoding and so will fail at that point. Instead create your instance of Picture within an instance methods or the constructor."
[Embed(source = "../lib/jacobsLadder.png")]
public static var Picture:Class;
public function JMouse() {
var pic:Bitmap = new Picture();
init();
}
But I get the same error.
The type of code you can run at variable declaration scope is limited. Creating an instance of Picture requires decoding and so will fail at that point. Instead create your instance of Picture within an instance methods or the constructor.
ANSWER: Load the bitmapdata with a loader.
Although the OP (me) was asking how to embed the file, the mysterious problem of "Error #1065: Variable FlexVersion is not defined" issue is apparently an insurmountable one, and I hope that anyone else who may come across this problem may find solace in the following (courtesy of John Mark Isaac Madison et al):
How do you load a bitmap file into a BitmapData object?
While not the an answer, per se, it at least allows the OP to continue on in his work as an alternative to lighting his house on fire.

Flash doesnt recognise JPGEncoder

When I run this code
var jpgEncoder:JPGEncoder = new JPGEncoder(100);
It returns this error
Type was not found or was not a compile-time constant: JPGEncoder.
Call to a possibly undefined method JPGEncoder.
Flash doesn't recognise "JPGEncoder" as a keyword.
Can someone help me please?
JPGEncoder is not a keyword, but a class you'll need to import to utilize.
Download JPGEncoder from as3corelib and import it into your program using
import com.adobe.images.JPGEncoder;

Error: Call to a possibly undefined method startTest

I'm new to flash development. I got an actionscript (3.0) code to work with, which I'm trying to compile using mxmlc.exe
import flash.display.*;
startTest();
function startTest() {
// Create objects to other classes to start display
}
When I compile it with the below command
mxmlc.exe Main.mxml
Where the above .as file is included, I keep getting the following error
Error: Call to a possibly undefined method startTest.
There in no class or package is defined in the .as file.
Please provide some inputs here.

Explicit reference to actionscript class in top level package

If you define a class in actionscript that has the same name as a class in the top level package (e.g. Array) there seems to be no way of explicitly referencing the class in the top level package.
UPDATE: This issue is only appearing in Flash Builder 4.7 with the new ASC 2.0 compiler, using Flash Builder 4.6 with an 'old' Flex SDK it works.
Example:
package
{
import flash.display.Sprite;
import mypackage.Array;
public class AS3Problem extends Sprite
{
public function AS3Problem()
{
var myOwnArray:mypackage.Array = new mypackage.Array();
// The line below will cause a compile error
// 'Ambiguous reference to Array'
var flashArray:Array = new Array();
}
}
}
I know the simple solution to this problem is to not create classes with a name that is the same as an as3 top level package class/function, but I'm intrigued as to how this could be 'fixed' in some way by explicitly referring to this package or some other means.
For those interested, I happened to accidentally import 'Array' from hamcrest-as3 while writing tests which caused a problem like this.
Try removing this line
import mypackage.Array;
so you do not import this custom Array and you always use the fully qualified name, I did not tested it but this should fix it.
EDIT:
I tested this and yes you have to add this import line, it will not work , but I can't reproduce the error, I can use and custom Array and the global Array fine with no issue/error or warrning.
EDIT2:
This also worked for me, can you test this too?
var globalArray:Class=(getDefinitionByName('Array') as Class);
var arr2:*=new globalArray();
arr2.push("test");
trace(arr2);

AS3 text to file

I'm making a game in cs3 using as3. I've seen dozens of tutorials, but none of them are working for me.
I found the simplest code I could, and it still gives me an error "1061: Call to a possibly undefined method save through a reference with static type flash.net:FileReference."
here's the code I'm using:
var file:FileReference = new FileReference();
file.save("this is a text", "file.txt")
Are you importing FileReference? If not, you'll need:
import flash.net.FileReference;
..inside your package declaration (or at the top of your block of code if you're coding on the time line)
With that import included, your code works for me.