Error #2007: Parameter text must be non-null - actionscript-3

in my program, the user has to input a number between
10 and 99 and the program will convert the number into words. The program somewhat works; however, when I input a number between 20 and 99, say for example, 45, the program will print out, "Forty fiveForty." This is when "Error #2007: Parameter text must be non-null," appears in the output section. I also can't seem to make numbers from 11 to 19 to work. Instead of showing the number in words, the result says "Error." Kindly edit my code in order for the program to function correctly and print out a number between 10 and 99 correctly.
// This line makes the button, btnConvert wait for a mouse click
// When the button is clicked, the convertNumber function is called
btnConvert.addEventListener(MouseEvent.CLICK, convertNumber);
// These lines make the textinputs wait for a mouse click
// When any of these components are clicked, the clearLabels function is called
txtinNumber.addEventListener(MouseEvent.CLICK, clearLabels);
// Declare Global Variables
var num:int; // number from 10 - 99
var tensDigit:int; // the tens digit
var onesDigit:int; // the ones digit
var teensDigit = [11, 12, 13, 14, 15, 16, 17, 18, 19];
// This is the convertNumber function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function convertNumber(e:MouseEvent):void
{
getData();
if (num < 10 || num > 99){
lblOutput.text = "Invalid number. Enter a number between 10 and 99 inclusive.";
}
else{
lblOutput.text = "";
if (num >= 20) {
tensDigit = Math.floor(num / 10);
onesDigit = num % 10;
tens();
ones();
}
else{
tensDigit = Math.floor(num / 10);
onesDigit = num % 10;
teens();
}
}
lblOutput.text =
tens();
lblOutput.text += onesDigit
ones();
}
// This is the getData function
// It gets the number from the user
function getData()
{
// complete the code here
num = int(txtinNumber.text);
}
// This is the tens function
// It outputs the word representation of 20, 30, 40,..,90
function tens()
{
if (tensDigit == 2 && tensDigit < 3)
{
lblOutput.text += "Twenty";
}
else if (tensDigit == 3 && tensDigit < 4)
{
lblOutput.text += "Thirty";
}
else if (tensDigit == 4 && tensDigit < 5)
{
lblOutput.text += "Forty";
}
else if (tensDigit == 5 && tensDigit < 6)
{
lblOutput.text += "Fifty";
}
else if (tensDigit == 6 && tensDigit < 7)
{
lblOutput.text += "Sixty";
}
else if (tensDigit == 7 && tensDigit < 8)
{
lblOutput.text += "Seventy";
}
else if (tensDigit == 8 && tensDigit < 9)
{
lblOutput.text += "Eighty";
}
else if (tensDigit == 9 && tensDigit < 10)
{
lblOutput.text += "Ninety";
}
else
{
lblOutput.text += "Unknown."
}
}
// This is the ones function
// It outputs the word representaion for any number from 1 - 9 inclusive
function ones()
{
if (onesDigit == 1)
{
lblOutput.text += " one"
}
else if (onesDigit == 2)
{
lblOutput.text += " two"
}
else if (onesDigit == 3)
{
lblOutput.text += " three"
}
else if (onesDigit == 4)
{
lblOutput.text += " four"
}
else if (onesDigit == 5)
{
lblOutput.text += " five"
}
else if (onesDigit == 6)
{
lblOutput.text += " six"
}
else if (onesDigit == 7)
{
lblOutput.text += " seven"
}
else if (onesDigit == 8)
{
lblOutput.text += " eight"
}
else if (onesDigit == 9)
{
lblOutput.text += " nine"
}
}
// This is the teens function
// It outputs the word representation for any number from 10 - 19 inclusive
function teens()
{
if (teensDigit == 10)
{
lblOutput.text += "Ten"
}
else if (teensDigit == 11)
{
lblOutput.text += "Eleven"
}
else if (teensDigit == 12)
{
lblOutput.text += "Twelve"
}
else if (teensDigit == 13)
{
lblOutput.text += "Thirteen"
}
else if (teensDigit == 14)
{
lblOutput.text += "Fourteen"
}
else if (teensDigit == 15)
{
lblOutput.text += "Fifteen"
}
else if (teensDigit == 16)
{
lblOutput.text += "Sixteen"
}
else if (teensDigit == 17)
{
lblOutput.text += "Seventeen"
}
else if (teensDigit == 18)
{
lblOutput.text += "Eighteen"
}
else if (teensDigit == 19)
{
lblOutput.text += "Nineteen"
}
else
{
lblOutput.text = "Error."
}
}
// This is the clearLabels function
// e:MouseEvent is the click event experienced by the textInput
// void indicates that the function does not return a value
function clearLabels(e:MouseEvent):void
{
lblOutput.text = "";
}

You're referencing the teensDigit as if it were an int, but you created an array. Change your teens() function to
if (num == 10) {
and so on, rather than teensDigit ==
Alternatively, you could simply use a lookup table. Below is a simplified version of what you were trying to do which can be compiled on a clean project.
convertNumber(7) // Outputs: seven
convertNumber(13) // Outputs: Thirteen
convertNumber(56) // Outputs: Fifty six
function convertNumber(i:int):void {
var tens:int = Math.floor(i / 10);
var ones:int = (i > 9 && i < 20) ? i : i % 10;
trace(lookup.double[tens] + lookup.single[ones])
}
var lookup:Object = {
"single":[
"",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen"
],
"double":[
"",
"",
"Twenty ",
"Thirty ",
"Forty ",
"Fifty ",
"Sixty ",
"Seventy ",
"Eighty ",
"Ninety ",
]
}

Related

2 same errors in one script

I have 2 errors:
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
BlowfishPong_fla::MainTimeline/countTime()[BlowfishPong_fla.MainTimeline::frame79:75]
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
BlowfishPong_fla::MainTimeline/airScore()[BlowfishPong_fla.MainTimeline::frame79:131]
Look at this code:
timedScore = 0;
var goalScore: int = Math.floor(Math.random() * 101) + 20;
var speedSeconds = 0;
var speedMinutes = 0;
keyNum = 0;
var OxygenTime = 0;
var OxygenMaxTime = 5;
var goalKey: int = Math.floor(Math.random() * 10) + 3;
var ballSpeedXTimed: int = -3;
var ballSpeedYTimed: int = -2;
var cpuPaddleSpeedTimed: int = 3;
stopwatch.play();
oxygenGauge.stop();
var FiftyTimed: Boolean = false;
var plzStopTimed: Boolean = false;
helpContent_Timed.visible = false;
stopwatch.addEventListener(Event.ENTER_FRAME, countTime);
oxygenGauge.addEventListener(Event.ENTER_FRAME, airScore);
goalScore_txt.text = String(goalScore);
timedScore_txt.text = timedScore;
key_txt.text = keyNum + "/" + goalKey;
stage.addEventListener(Event.ENTER_FRAME, loopTimed);
function updateTextFieldsTimed(): void {
goalScore_txt.text = String(goalScore);
timedScore_txt.text = timedScore;
key_txt.text = keyNum + "/" + goalKey;
}
function calculateBallAngleTimed(paddleY: Number, ballY: Number): Number {
var ySpeed: Number = 5 * ((ballY - paddleY) / 25);
return ySpeed;
}
function countTime(e: Event): void {
if (stopwatch.currentFrame == 61) {
speedSeconds++;
if (speedSeconds > 59) {
speedSeconds = 0;
speedtimerSec_txt.text = "0" + speedSeconds;
speedMinutes++;
if (speedMinutes > 10) {
speedtimerMin_txt.text = "" + speedMinutes;
} else {
speedtimerMin_txt.text = "0" + speedMinutes;
}
if (speedMinutes > 59) {
stopwatch.stop();
gotoAndPlay("gameover_Timed");
}
} else {
if (speedSeconds >= 10) {
speedtimerSec_txt.text = "" + speedSeconds;
} else {
speedtimerSec_txt.text = "0" + speedSeconds;
}
}
}
}
function airScore(timedScore): void {
if (timedScore == 50 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
OxygenTime++;
} else if (timedScore == 150 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
oxygenGauge.frameRate = 1.5;
OxygenTime++;
} else if (timedScore == 300 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
OxygenTime++;
} else if (timedScore == 450 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
OxygenTime++;
} else if (timedScore == 600 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
OxygenTime++;
}
if (oxygenGauge.currentFrame == 505) {
character_TiedUp.gotoAndStop(3);
oxygenGauge.stop();
oxygenGauge.gotoAndStop(1);
}
}
function loopTimed(event: Event): void {
if (plzStopTimed == false) {
playerPaddle.y = mouseY;
keyPong.x += ballSpeedXTimed;
keyPong.y += ballSpeedYTimed;
//check left and right boundaries
if (keyPong.x <= keyPong.width / 2) {
keyPong.x = keyPong.width / 2;
ballSpeedXTimed *= -1;
keyNum++;
BingTimed.play();
if (goalKey == keyNum) {
key_txt.textColor = 0x00FF00;
}
updateTextFieldsTimed();
} else if (keyPong.x >= stage.stageWidth - keyPong.width / 2) {
keyPong.x = stage.stageWidth - keyPong.width / 2;
ballSpeedXTimed *= -1;
BingTimed.play();
if (keyNum > 0) {
keyNum--;
}
if(goalKey < keyNum) {
key_txt.textColor = 0xFFFFFF;
}
updateTextFieldsTimed();
}
if (keyPong.y <= keyPong.height / 2) {
keyPong.y = keyPong.height / 2;
ballSpeedYTimed *= -1;
gameBounceTimed.play();
} else if (keyPong.y >= stage.stageHeight - keyPong.height / 2) {
keyPong.y = stage.stageHeight - keyPong.height / 2;
ballSpeedYTimed *= -1;
gameBounceTimed.play();
}
if (cpuPaddle.y < keyPong.y - 10) {
cpuPaddle.y += cpuPaddleSpeedTimed;
} else if (cpuPaddle.y > keyPong.y + 10) {
cpuPaddle.y -= cpuPaddleSpeedTimed;
}
if (playerPaddle.y - playerPaddle.height / 4 < 0) {
playerPaddle.y = playerPaddle.height / 4;
} else if (playerPaddle.y + playerPaddle.height / 4 > stage.stageHeight) {
playerPaddle.y = stage.stageHeight - playerPaddle.height / 4;
}
if (playerPaddle.paddle.hitTestObject(keyPong) == true) {
if (ballSpeedXTimed > 0) {
ballSpeedXTimed *= -1;
ballSpeedYTimed = calculateBallAngleTimed(playerPaddle.y, keyPong.y);
timedScore++
gameHitTimed.play();
airScore(timedScore);
if (goalKey == keyNum) {
if (goalScore == timedScore) {
stage.removeEventListener(Event.ENTER_FRAME, loopTimed);
gotoAndStop("gameover_Timed");
return;
}
}
updateTextFieldsTimed();
}
} else if (cpuPaddle.paddle.hitTestObject(keyPong) == true) {
if (ballSpeedXTimed < 0) {
ballSpeedXTimed *= -1;
ballSpeedYTimed = calculateBallAngleTimed(cpuPaddle.y, keyPong.y);
timedScore++;
gameHitTimed.play();
airScore(timedScore);
if (goalKey == keyNum) {
if (goalScore == timedScore) {
stage.removeEventListener(Event.ENTER_FRAME, loopTimed);
gotoAndStop("gameover_Timed");
return;
}
}
updateTextFieldsTimed();
}
}
}
}
Can anyone how to fix these?

AS3 - Numbers won't display in dynamic text field

I am writing a program that my brother needs for a project he is working on. It "solves" a password and then shows the number of loops the program goes through before figuring out the solution. I've gotten everything working but the program won't show the numbers in the dynamic text field. It only shows the letters.
Here's the code:
import flash.events.MouseEvent;
button1.addEventListener(MouseEvent.CLICK, solvePassword);
function solvePassword(e:MouseEvent):void
{
var pass:Number = Number(inputText.text);
var attempts:Number = 0;
var i:String = pass.toString();
var passLength:Number = i.length;
var n:Number = 0;
while(n != pass)
{
if(passLength == 1)
{
n += 1;
attempts += 1;
}
if(passLength == 2)
{
n += 1;
if(n < 10) { n = 10 }
attempts += 1;
}
if(passLength == 3)
{
n+=1;
if(n < 100) { n = 100 }
attempts += 1;
}
if(passLength == 4)
{
n+=1;
if(n< 1000) { n = 1000 }
attempts += 1;
}
if(passLength == 5)
{
n+=1;
if(n < 10000) { n = 10000 }
attempts += 1;
}
if(passLength == 6)
{
n+=1;
if(n < 100000) { n = 100000 }
attempts += 1;
}
if(passLength == 7)
{
n+=1;
if(n < 1000000) { n = 1000000 }
attempts += 1;
}
}
i = "Attempts: " + String(attempts);
trace(i);
aBox.text = i;
Any info would help, thank you!

actionscript break if statement to check next one

I'm making a bot for a simple tic tac toe game. Here is the problem, at botCheck function, when row 0 col 0 and row 0 col 1 is "O", then the "X" appear at the row 0 col 2, and when I tried to make another combination of "O" (ex. row 0 col 0 and row 1 col 1), it should check at different if statement, but my code seems to still check only at the first if statement which will keep tracing ("D") and stuck at that part.
So here is the question, at else{trace("D");} , is there any code that I can replace with to skip the first if statement and go to the second one so it can check for the other combinations of "O" ?
P.S. I've tried to use continue; but it only goes to the next loop and still stuck at the first if statement.
Thanks!
package
{
public class Bot
{
private var n:int = 0;
private var rndm:int;
private var v:int;//vertical
private var h:int;//horizontal
private var moving:Boolean = false;;
private var _main:Main;
public function Bot(main:Main):void
{
_main = main;
}
private function randomNumber(min:Number, max:Number):Number
{
return Math.floor(Math.random()*(1+max-min)+min);
}
public function botMove():void
{
botCheck();
if(n == 0 && moving == false)
{
rndm = randomNumber(0,2);
v = rndm;
rndm = randomNumber(0,2);
h = rndm;
if(_main.squareArray[v][h] == 0)
{
_main.squareArray[v][h] = 1;
_main.xoArray[v][h].push(_main.xo);
moving = true;
fillX();
n = 0;
_main.xo = "O";
}
else
{
botMove();
}
}
if(n == 1 && moving == false)
{
if(_main.squareArray[v][h] == 0)
{
_main.squareArray[v][h] = 1;
_main.xoArray[v][h].push(_main.xo);
moving = true;
fillX();
n = 0;
_main.xo = "O";
}
}
}
private function fillX():void
{
_main.x_ = new X ;
_main.xoContainer.addChild(_main.x_);
_main.x_.x = _main.gameWidth / _main.lebar *(1+(h))+ _main.gameWidth/(_main.lebar*4);
_main.x_.y = _main.gameHeight / _main.panjang * (1+(v))+_main.gameHeight/(_main.panjang*4);
_main.x_.width = _main.square.width / 2;
_main.x_.height = _main.square.height / 2;
}
private function botCheck():void
{
for(var a:int = 0; a<_main.panjang;a++)
{
if(n != 0)
{
break;
}
for(var b:int = 0;b<_main.lebar;b++)
{
if(_main.xoArray[a][b]=="O" && _main.xoArray[a][b+1] == "O")
{
if(b+2 < _main.lebar && _main.xoArray[a][b+2] == 0)
{
n = 1;
v = a;
h = b+2;
moving = false;
break;
}
else if(_main.xoArray[a][b-1] == 0)
{
n = 1;
v = a;
h = b-1;
moving = false;
break;
}
else
{
trace("D");
}
}
else if(_main.xoArray[a][b] == "O" && _main.xoArray[a+1][b] =="O")
{
if(a+2 < _main.panjang && _main.xoArray[a+2][b] == 0)
{
n = 1;
v = a+2;
h = b;
moving = false;
break;
}
else if(a != 0)
{
if(_main.xoArray[a-1][b] == 0)
{
n = 1;
v = a-1;
h = b;
moving = false;
break;
}
}
else
{
}
}
else if(_main.xoArray[a][b]=="O" && _main.xoArray[a+1][b+1] == "O")
{trace("B");
if(a+2 < _main.panjang && b+2 < _main.lebar && _main.xoArray[a+2][b+2] == 0)
{
n = 1;
v = a+2;
h = b+2;
moving = false;
break;
}
else if(a != 0)
{
if(_main.xoArray[a-1][b-1] == 0)
{
n = 1;
v = a-1;
h = b-1;
moving = false;
break;
}
}
else
{
}
}
else if(_main.xoArray[a][b+2]=="O" && _main.xoArray[a+1][b+1] == "O")
{trace("A");
if(a+2 < _main.lebar && _main.xoArray[a+2][b] == 0)
{
n = 1;
v = a+2;
h = b;
moving = false;
break;
}
}
else if(_main.xoArray[a][b] == "O" && _main.xoArray[a+1][b-1] == "O")
{
if(a != 0 && b+1 < _main.lebar && _main.xoArray[a-1][b+1] == 0)
{
n = 1;
v = a-1;
h = b+1;
moving = false;
break;
}
}
else
{
n = 0;
moving = false;
}
}
}
}
}
}
If you would like to check through all the if statements within the for loop, simply remove the "else"s from the other if statements.
Thus, your first if statement is fine, but the next if statement would go from
else if(_main.xoArray[a][b] == "O" && _main.xoArray[a+1][b] =="O")
to simply
if(_main.xoArray[a][b] == "O" && _main.xoArray[a+1][b] =="O")
(just omitting the else).
Repeat that for all of the other outermost "else if"'s in your loop.
Now, I see at the bottom you have a final "else" that sets n to 0 and moving to false. If you make the changes listed above, you may remove that else block, and instead put those statements in the else block (n = 0; moving = false) at the very beginning of the for loop. Therefore, unless any of the if statements are triggered, n will equal 0 and moving will equal false, which is the intended result.
You can also get rid of all of the empty else {} blocks within the for loop; they don't contribute anything anyway. Let me know if you have any questions :)

No keyboard input AS3

So I'm trying to get keyboard input from the user to move a character. It works in another program that I was using, and I copy pasted, but it isn't working in this one. It gives me Line 87, Column 38 1119: Access of possibly undefined property EVENT_FRAME through a reference with static type Class. I can't seem to figure out what the issue is.
This is the buttonClick function that is used to when I hit the start button.
public function buttonClick(ev:MouseEvent):void
{
createGameScreen();
this.mcLink.gotoAndPlay("Idle");
this.mcLink.x=50;
this.mcLink.y=200;
this.mcLink.scaleX=this.mcLink.scaleY=3;
this.stage.addEventListener(Event.EVENT_FRAME, this.enterFrameHandler, false, 0, true);
}
This is the event handler function for the keyboard input.
public function enterFrameHandler($e:Event):void
{
if (this.mcLink)
{
if (KeyboardManager.instance.isKeyDown(KeyCode.DOWN))
{
if (this.mcLink.y + this.mcLink.height > this.stage.stageHeight || this.mcLink.y - this.mcLink.height <= 0)
{
this.mcLink.y += -15;
mcLink.gotoAndPlay("Idle");
return;
}
this.mcLink.y += _nHeroMovementSpeed;
mcLink.gotoAndPlay("Down");
}
else if (KeyboardManager.instance.isKeyDown(KeyCode.UP))
{
if (this.mcLink.y + this.mcLink.height > this.stage.stageHeight || this.mcLink.y - this.mcLink.height <= 0)
{
this.mcLink.y += 15;
mcLink.gotoAndPlay("Idle");
return;
}
this.mcLink.y -= _nHeroMovementSpeed;
mcLink.gotoAndPlay("Up");
}
if (KeyboardManager.instance.isKeyDown(KeyCode.LEFT))
{
if (this.mcLink.x + this.mcLink.width > this.stage.stageWidth || this.mcLink.x - this.mcLink.width <= 0)
{
this.mcLink.x += 15;
mcLink.gotoAndPlay("Idle");
return;
}
this.mcLink.x -= _nHeroMovementSpeed;
mcLink.gotoAndPlay("Left");
}
else if (KeyboardManager.instance.isKeyDown(KeyCode.RIGHT))
{
if (this.mcLink.x + this.mcLink.width > this.stage.stageWidth || this.mcLink.x - this.mcLink.width <= 0)
{
this.mcLink.x += -15;
mcLink.gotoAndPlay("Idle");
return;
}
this.mcLink.x += _nHeroMovementSpeed;
mcLink.gotoAndPlay("Right");
}
}
}
Did you mean Event.ENTER_FRAME?
this.stage.addEventListener(Event.ENTER_FRAME, this.enterFrameHandler, false, 0, true);
// ^^^^^^^^^^^ EVENT_FRAME isn't a known Event.

Problem using JSON.as

My code
import JSON;
var j = "{fef:34}";
var json = new JSON();
trace(json.parse(j)); // undefined !!!
It doesn't work; the trace will return undefined. I'm compiling for Flash 8 in ActionScript 2. The file JSON.as is in the same folder (see below).
When debugging it says value for json after json = new JSON() is undefined.
JSON.as (from JSON.org)
class JSON {
var ch:String = '';
var at:Number = 0;
var t,u;
var text:String;
function stringify(arg):String {
var c, i, l, s = '', v;
switch (typeof arg) {
case 'object':
if (arg) {
if (arg instanceof Array) {
for (i = 0; i < arg.length; ++i) {
v = stringify(arg[i]);
if (s) {
s += ',';
}
s += v;
}
return '[' + s + ']';
} else if (typeof arg.toString != 'undefined') {
for (i in arg) {
v = arg[i];
if (typeof v != 'undefined' && typeof v != 'function') {
v = stringify(v);
if (s) {
s += ',';
}
s += stringify(i) + ':' + v;
}
}
return '{' + s + '}';
}
}
return 'null';
case 'number':
return isFinite(arg) ? String(arg) : 'null';
case 'string':
l = arg.length;
s = '"';
for (i = 0; i < l; i += 1) {
c = arg.charAt(i);
if (c >= ' ') {
if (c == '\\' || c == '"') {
s += '\\';
}
s += c;
} else {
switch (c) {
case '\b':
s += '\\b';
break;
case '\f':
s += '\\f';
break;
case '\n':
s += '\\n';
break;
case '\r':
s += '\\r';
break;
case '\t':
s += '\\t';
break;
default:
c = c.charCodeAt();
s += '\\u00' + Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}
}
}
return s + '"';
case 'boolean':
return String(arg);
default:
return 'null';
}
}
function white() {
while (ch) {
if (ch <= ' ') {
this.next();
} else if (ch == '/') {
switch (this.next()) {
case '/':
while (this.next() && ch != '\n' && ch != '\r') {}
break;
case '*':
this.next();
for (;;) {
if (ch) {
if (ch == '*') {
if (this.next() == '/') {
next();
break;
}
} else {
this.next();
}
} else {
error("Unterminated comment");
}
}
break;
default:
this.error("Syntax error");
}
} else {
break;
}
}
}
function error(m) {
throw {
name: 'JSONError',
message: m,
at: at - 1,
text: text
};
}
function next() {
ch = text.charAt(at);
at += 1;
return ch;
}
function str() {
var i, s = '', t, u;
var outer:Boolean = false;
if (ch == '"') {
while (this.next()) {
if (ch == '"') {
this.next();
return s;
} else if (ch == '\\') {
switch (this.next()) {
case 'b':
s += '\b';
break;
case 'f':
s += '\f';
break;
case 'n':
s += '\n';
break;
case 'r':
s += '\r';
break;
case 't':
s += '\t';
break;
case 'u':
u = 0;
for (i = 0; i < 4; i += 1) {
t = parseInt(this.next(), 16);
if (!isFinite(t)) {
outer = true;
break;
}
u = u * 16 + t;
}
if(outer) {
outer = false;
break;
}
s += String.fromCharCode(u);
break;
default:
s += ch;
}
} else {
s += ch;
}
}
}
this.error("Bad string");
}
function arr() {
var a = [];
if (ch == '[') {
this.next();
this.white();
if (ch == ']') {
this.next();
return a;
}
while (ch) {
a.push(this.value());
this.white();
if (ch == ']') {
this.next();
return a;
} else if (ch != ',') {
break;
}
this.next();
this.white();
}
}
this.error("Bad array");
}
function obj() {
var k, o = {};
if (ch == '{') {
this.next();
this.white();
if (ch == '}') {
this.next();
return o;
}
while (ch) {
k = this.str();
this.white();
if (ch != ':') {
break;
}
this.next();
o[k] = this.value();
this.white();
if (ch == '}') {
this.next();
return o;
} else if (ch != ',') {
break;
}
this.next();
this.white();
}
}
this.error("Bad object");
}
function num() {
var n = '', v;
if (ch == '-') {
n = '-';
this.next();
}
while (ch >= '0' && ch <= '9') {
n += ch;
this.next();
}
if (ch == '.') {
n += '.';
this.next();
while (ch >= '0' && ch <= '9') {
n += ch;
this.next();
}
}
if (ch == 'e' || ch == 'E') {
n += ch;
this.next();
if (ch == '-' || ch == '+') {
n += ch;
this.next();
}
while (ch >= '0' && ch <= '9') {
n += ch;
this.next();
}
}
v = Number(n);
if (!isFinite(v)) {
this.error("Bad number");
}
return v;
}
function word() {
switch (ch) {
case 't':
if (this.next() == 'r' && this.next() == 'u' &&
this.next() == 'e') {
this.next();
return true;
}
break;
case 'f':
if (this.next() == 'a' && this.next() == 'l' &&
this.next() == 's' && this.next() == 'e') {
this.next();
return false;
}
break;
case 'n':
if (this.next() == 'u' && this.next() == 'l' &&
this.next() == 'l') {
this.next();
return null;
}
break;
}
this.error("Syntax error");
}
function value() {
this.white();
switch (ch) {
case '{':
return this.obj();
case '[':
return this.arr();
case '"':
return this.str();
case '-':
return this.num();
default:
return ch >= '0' && ch <= '9' ? this.num() : this.word();
}
}
function parse(_text:String):Object {
text = _text;
at = 0;
ch = ' ';
return value();
}
}
Why is json undefined at the end of my code?
Make sure your file name is JSON.as with upper case.
if you write import JSON; and your file name is json.as you will get undefined!
> Blockquote
import JSON;
var json = new JSON();
trace(json);
// Output:
[object Object]
enter code here
Debugging line by line I found this line has a problem
but I don't know why :
s += '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
The class only work if you publish as ActionScript 2.
Also the if JSON.as isn't in the same directory as the fla, you will need to specify the path e.g. import com.JSON;