Problem using JSON.as - json

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;

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?

Error #2007: Parameter text must be non-null

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 ",
]
}

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 :)

Got lost on this code and cant make it work

Im getting an weird error about constructions definitions that i have no idea what means.
Im studing this for a test.I think the problem is on the functions but im not sure.
Its a code from an game (pretty simple one) that you have to move your player from point b to point a without letting the falling asteroids touch you.
Thanks in advance.
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
var up:Boolean;
var down:Boolean;
var left:Boolean;
var right:Boolean;
var vel:Number;
var velInimigo:Number;
var sentidoX:Number;
var sentidoY:Number;
var d:MovieClip;
var txt1:MovieClip;
var chegada:MovieClip;
var player:MovieClip;
var inimigos:Array;
var i:int;
var mc:MovieClip;
function MainTimeline()
{
addFrameScript(0, frame1);
return;
}
function OnKeyDown(event:KeyboardEvent) : void
{
switch(event.keyCode)
{
case Keyboard.W:
{
up = true;
break;
}
case Keyboard.S:
{
down = true;
break;
}
case Keyboard.A:
{
left = true;
break;
}
case Keyboard.D:
{
right = true;
break;
}
default:
{
break;
}
}
return;
}
function OnKeyUp(event:KeyboardEvent) : void
{
switch(event.keyCode)
{
case Keyboard.W:
{
up = false;
break;
}
case Keyboard.S:
{
down = false;
break;
}
case Keyboard.A:
{
left = false;
break;
}
case Keyboard.D:
{
right = false;
break;
}
default:
{
break;
}
}
return;
}
function OnFrame(event:Event) : void
{
var loc3: = null;
var loc4: = null;
if (up)
{
sentidoY = -1;
}
else if (down)
{
sentidoY = 1;
}
else
{
sentidoY = 0;
}
if (left)
{
sentidoX = -1;
}
else if (right)
{
sentidoX = 1;
}
else
{
sentidoX = 0;
}
if (sentidoX == 1 && sentidoY == 0)
{
player.rotation = 0 + 90;
}
else if (sentidoX == 1 && sentidoY == 1)
{
player.rotation = 45 + 90;
}
else if (sentidoX == 0 && sentidoY == 1)
{
player.rotation = 90 + 90;
}
else if (sentidoX == -1 && sentidoY == 1)
{
player.rotation = 135 + 90;
}
else if (sentidoX == -1 && sentidoY == 0)
{
player.rotation = 180 + 90;
}
else if (sentidoX == -1 && sentidoY == -1)
{
player.rotation = 225 + 90;
}
else if (sentidoX == 0 && sentidoY == -1)
{
player.rotation = 270 + 90;
}
else if (sentidoX == 1 && sentidoY == -1)
{
player.rotation = 315 + 90;
}
player.x = player.x + vel * sentidoX;
player.y = player.y + vel * sentidoY;
var loc2: = 0 ;
while (loc2 < inimigos.length)
{
loc3 = inimigos[loc2];
loc3.y = loc3.y + loc3.vel;
loc3.rotation = loc3.rotation + loc3.rotVel;
if (loc3.hitTestObject(player))
{
loc4 = new GameOverMC();
stage.addChild(loc4);
loc4.x = stage.stage.width / 2;
loc4.y = stage.stageHeight / 2;
stage.removeEventListener(Event.ENTERFRAME, OnFrame);
}
if (loc3.y > stage.stageHeight)
{
loc3.y = -loc3.height;
}
loc2++;
}
if (player.hitTestObject(chegada))
{
if (txt1 == null)
{
txt1 = new VitoriaMC();
stage.addChild(txt1);
txt1.x = stage.stage.width / 2;
txt1.y = stage.stageHeight / 2;
}
}
d.x = d.x + d.vel * d.sx;
d.y = d.y + d.vel * d.sy;
if (d.x >= stage.stageWidth - d.width / 2 || d.x <= d.width / 2)
{
d.sx = -d.sx;
}
if (d.y >= stage.stageHeight - d.height / 2 || d.y <= d.height / 2)
{
d.sy = -d.sy;
}
return;
}
function frame1()
{
vel = 5;
velInimigo = 2;
sentidoX = 0;
sentidoY = 0;
d = new DemoMC();
d.vel = 1;
d.sx = 1;
d.sy = 1;
d.x = stage.stageWidth / 2;
d.y = stage.stageHeight / 2;
txt1 = null;
chegada = new ChegadaMC();
player = new PlayerMC();
inimigos = new Array();
stage.addChild(d);
stage.addChild(chegada);
stage.addChild(player);
player.x = player.width / 2;
player.y = stage.stageHeight - player.height / 2;
chegada.x = stage.stageWidth - chegada.width;
chegada.y = 0;
i = 0;
while (i < 60)
{
mc = new AstroMC();
stage.addChild(mc);
inimigos.push(mc);
mc.x = 100 + Math.random() * (600 - mc.width);
mc.y = Math.random() * stage.stageHeight;
mc.rotVel = Math.random() * 10 - 5;
mc.vel = Math.random() * 3 + 1;
var loc1: = i;
var loc2: = i + 1;
loc1.i = loc2;
}
stage.addEventListener(KeyboardEvent.KEYDOWN, OnKeyDown);
stage.addEventListener(KeyboardEvent.KEYUP, OnKeyUp);
stage.addEventListener(Event.ENTERFRAME, OnFrame);
return;
}
It has been awhile since i have played with AS3 but i see a few odd things one being the
loc1.i = loc2;
near the bottom
what is "i" in relation to loc1? and this is in a while loop using i but never increments it so it seems like an infinite loop
again i could be wrong its been quite some time.

SpaceBar Character issues in AS3

Im working on a virtual keyboard, but my space key is giving me some issues. When its pressed, it creates the space but it dosent physically show. When I hit a normal letter or number key, the space is now visible with the letter in front of it. The keyboard and space code are nearly identical.
function _keys(event:MouseEvent):void
{
textSelect.setSelection(textSelect.length, textSelect.length);
if(electoff.currentFrame == 2)
{
ajWidth(event);
}
if(terminalinput.currentFrame == 2)
{
TrWidth(null);
}
if (control == "on" && menu.visible == false )
{
if (! shift)
{
textSelect.appendText(letterButtonSmall[event.target.name]);
}
else
{
textSelect.appendText(letterButtonCaps[event.target.name]);
shift = false;
}
savedTxt = textSelect.text;
textSelect.setSelection(textSelect.length, textSelect.length);
if(textSelect.width <= 134.05 && textSelect == inputClips.inputTxt )
{
textSelect.autoSize = TextFieldAutoSize.LEFT;
textSelect.x = 1.2;
}
if (textSelect.width >= 134.05 && textSelect == inputClips.inputTxt)
{
textSelect.autoSize = TextFieldAutoSize.LEFT;
textSelect.x = 1.2 - (textSelect.width-134.05);
}
}
textSelect.setSelection(textSelect.length, textSelect.length);
if(electoff.currentFrame == 2)
{
ajWidth(event);
}
if(terminalinput.currentFrame == 2)
{
TrWidth(null);
}
focuser();
strehrows3();
_fontSize();
}
//spaces
key_space.addEventListener(MouseEvent.CLICK, spaceClickListener);
function spaceClickListener(e:MouseEvent):void
{
strehrows3();
textSelect.setSelection(textSelect.length,textSelect.length);
if (electoff.currentFrame == 2)
{
ajWidth(null);
}
if (terminalinput.currentFrame == 2)
{
TrWidth(null);
}
if (control == "on" && menu.visible == false )
{
if (! shift)
{
textSelect.appendText(" ");
}
else
{
textSelect.appendText(" ");
shift = false;
}
savedTxt = textSelect.text;
textSelect.setSelection(textSelect.length,textSelect.length);
if (textSelect.width <= 134.05 && textSelect == inputClips.inputTxt )
{
textSelect.autoSize = TextFieldAutoSize.LEFT;
textSelect.x = 1.2;
}
if (textSelect.width >= 134.05 && textSelect == inputClips.inputTxt)
{
textSelect.autoSize = TextFieldAutoSize.LEFT;
textSelect.x = 1.2-(textSelect.width-134.05);
}
}
textSelect.setSelection(textSelect.length,textSelect.length);
if (electoff.currentFrame == 2)
{
ajWidth(null);
}
if (terminalinput.currentFrame == 2)
{
TrWidth(null);
}
focuser();
strehrows3();
_fontSize();
}
Now I suspect the autoSize is responsible for my problems but I cant be sure.
Wondering on some creative fixes may work, Ive been contemplating using a blank symbol if there is such a thing.