00:00
00:00
Newgrounds Background Image Theme

markololohands just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

[Help]Actionscript 2:Array.toString

1,325 Views | 5 Replies
New Topic Respond to this Topic

Hello Newgrounds.. I am in need of some help.. I Know im going through alot of trouble doing this.. and its probably not worth the trouble but i want to accomplish this .. so im in need of some help.. i have a few functions in AS2 that take in an array which is my Loaded Games link but the array is just numbers which actually use a function with a case to replace those numbers with there matching letters.. My Problem is i cant get it to return as a string.. ive tried putting .toString()
but it didnt make much change and still didnt load the game

_root.gameLink = [78, 105, 110, 106, 97, 46, 115, 119, 102];
load(parseURL(_root.gameLink));

function load(gameURL:String){
	loadMovieNum(gameURL,1,"GET"); // Load our gameURL Parameters Text
}

function parseURL(Link:String){
	var RepHolder = [];
	for( i = 0; i < Link.length; i++) {
		RepHolder.push(n2s(Link[i]));
}
	var Splitted = SplitURL(RepHolder);
	var Joined = JoinURL(RepHolder);
	var Product = Joined;
	trace(Product);
	return Product;
}

function SplitURL(Link:String){
    var SplitLink = Link.split("");
	var realLink = SplitLink;
    return realLink;
}

function JoinURL(Split:Array){
	var JoinLink = "\"" + Split.join("\" + \"") + "\"";
	return JoinLink.toString();
}

function n2s(Letra:Number)
      {
         switch(Letra)
         {
            case 65:
               return "A";
            case 66:
               return "B";
               ......
               ......
               ......
           default:
               return "";
            }
      }

Thats pretty much my code right now.. i cut out alot of the other things that had nothing to do with this.. but the problem i have is my load game isnt coming back as a String to be used it traces out exactly how i wanted it too though

"N" + "i" + "n" + "j" + "a" + "." + "s" + "w" + "f"

i was told most languages dont have the ability to convert arrays to strings.. but i looked it up and seen .toString() I Tried it but my guess is it wasn't working .. So not sure if i placed it wrong .. or what.. but ive been trying to figure this out all day.. any help?

P.S : Im a total noob to AS2 , so if i did something so stupid and simple wrong dont judge me lol


Happy Coding

- Xploit

BBS Signature

Response to [Help]Actionscript 2:Array.toString 2016-10-29 10:10:24


You can use the Array.map and String.fromCharCode to achieve that. (You can ignore the fact that those are JavaScript docs; ActionScript and JavaScript are basically the same language, so the docs are the same for ActionScript.)

Something like this would work:

var numArr:Array = [65, 82, 110, 98, 105];

// Convert the array to ["A", "B", "C", "D"] format.
var strArr:Array = numArr.map(function(elem) {
  return "\"" + String.fromCharCode(elem) + "\"";
});

// Convert the new array to a string, separated by commas.
var numStr:String = strArr.join(" + ");

// Output: "A" + "R" + "n" + "b" + "i"
trace(numStr);

Basically the map() function just passes each element to a given function, in this case an anonymous function, and replaces that element with the returned value. String.fromCharCode() converts an integer to its ASCII equivalent character, so using them together gives you the conversion you're looking for.

Response to [Help]Actionscript 2:Array.toString 2016-10-29 10:45:58


At 10/29/16 10:10 AM, Diki wrote: Basically the map() function just passes each element to a given function

the trace just outputts "undefined" with your code :/


Happy Coding

- Xploit

BBS Signature

At 10/29/16 10:45 AM, iXploit wrote:
At 10/29/16 10:10 AM, Diki wrote: Basically the map() function just passes each element to a given function
the trace just outputts "undefined" with your code :/

Also do you know anything about a Switch / case condition not working with onKey press when a flash file is loaded?

Accidentally Clicked Reply instead of edit.. not sure how to delete posts neither :/


Happy Coding

- Xploit

BBS Signature

Response to [Help]Actionscript 2:Array.toString 2016-10-29 11:59:36


At 10/29/16 10:45 AM, iXploit wrote:
At 10/29/16 10:10 AM, Diki wrote: Basically the map() function just passes each element to a given function
the trace just outputts "undefined" with your code :/

That might be because I forgot to put in the type definitions for the argument passed to the callback as well as the callback's return type, though that shouldn't make any significant difference in AS2. But the code should be working. Unfortunately, I don't have anything that can compile ActionScript to a SWF application. Can you post the entire code you're using that's giving you undefined?

At 10/29/16 11:05 AM, iXploit wrote: Also do you know anything about a Switch / case condition not working with onKey press when a flash file is loaded?

Depends what you mean by "not working." I assume you mean it's simply doing nothing. Can you post the code that isn't doing what you want?

Response to [Help]Actionscript 2:Array.toString 2016-10-29 23:03:58


At 10/29/16 11:59 AM, Diki wrote:
At 10/29/16 10:45 AM, iXploit wrote:
At 10/29/16 10:10 AM, Diki wrote: Basically the map() function just passes each element to a given function
the trace just outputts "undefined" with your code :/
That might be because I forgot to put in the type definitions for the argument passed to the callback as well as the callback's return type, though that shouldn't make any significant difference in AS2. But the code should be working. Unfortunately, I don't have anything that can compile ActionScript to a SWF application. Can you post the entire code you're using that's giving you undefined?

At 10/29/16 11:05 AM, iXploit wrote: Also do you know anything about a Switch / case condition not working with onKey press when a flash file is loaded?
Depends what you mean by "not working." I assume you mean it's simply doing nothing. Can you post the code that isn't doing what you want?

with your code i just used your code to see if it would work because 'map' didnt pop up as a keyword in AS2 for me so i tested your code to make sure it would work n it returned undefined..

Also ill post the whole project

_global.Keyz = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57]; // Number Array 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Stage.align = "TL"; // Align the stage to the top left
Stage.scaleMode = "noScale"; // Dont scale the stage upon Resizing
_root.gameLink = [78, 105, 110, 106, 97, 46, 115, 119, 102]; // Useless for now till bug is fixed
load(parseURL(_root.gameLink)); // Testing Swf
_root.onKeyDown = function() // Add an onKeyDown listener
{
switch(Key.getCode()) {
	case _global.Keyz[1]:
	if(_root.toggle1On == true)
	{
		_root.toggle1On = false;
		clearInterval(_root.timer1);
	}else{_root.toggle1On = true; _root.toggle1 = function()
	{
		trace("1. Your Code Here");
	};_root.timer1 = setInterval(_root.toggle1,seCon(0.1));
}
	    break;
	case _global.Keyz[2]:
		if(_root.toggle2On == true)
	{
		_root.toggle2On = false;
		clearInterval(_root.timer2);
	}else{_root.toggle2On = true; _root.toggle2 = function()
	{
		trace("2. Your Code Here");
	};_root.timer2 = setInterval(_root.toggle2,seCon(0.1));
}
	    break;
	case _global.Keyz[3]:
		if(_root.toggle3On == true)
	{
		_root.toggle3On = false;
		clearInterval(_root.timer3);
	}else{_root.toggle3On = true; _root.toggle3 = function()
	{
		trace("3. Your Code Here");
	};_root.timer3 = setInterval(_root.toggle3,seCon(0.1));
}
	    break;
	case _global.Keyz[4]:
		if(_root.toggle4On == true)
	{
		_root.toggle4On = false;
		clearInterval(_root.timer4);
	}else{_root.toggle4On = true; _root.toggle4 = function()
	{
		trace("4. Your Code Here");
	};_root.timer4 = setInterval(_root.toggle4,seCon(0.1));
}
	    break;
	default: 
	   return;
	}
};
Key.addListener(_root);

function load(gameURL:String){
	loadMovieNum(gameURL, 0, "GET"); // Load our gameLink Parameters Text
}

function seCon(Seconds:Number) {
	Seconds = Seconds * 1000;
	return Seconds;
}
function parseURL(Link:String){
	var RepHolder = [];
	for( i = 0; i < Link.length; i++) {
		RepHolder.push(n2s(Link[i]));
}
	var Splitted = SplitURL(RepHolder);
	var Joined = JoinURL(RepHolder);
	var Product = Joined;
	trace(Product);
	return Product;
}

function SplitURL(Link:String){
    var SplitLink = Link.split("");
	var realLink = SplitLink;
    return realLink;
}

function JoinURL(Split:Array){
	var JoinLink = "\"" + Split.join("\" + \"") + "\"";
	return JoinLink;
}

function n2s(Letra:Number)
      {
         switch(Letra)
         {
            case 65:
               return "A";
            case 66:
               return "B";
            case 67:
               return "C";
            case 68:
               return "D";
            case 69:
               return "E";
            case 70:
               return "F";
            case 71:
               return "G";
            case 72:
               return "H";
            case 73:
               return "I";
            case 74:
               return "J";
            case 75:
               return "K";
            case 76:
               return "L";
            case 77:
               return "M";
            case 78:
               return "N";
            case 79:
               return "O";
            case 80:
               return "P";
            case 81:
               return "Q";
            case 82:
               return "R";
            case 83:
               return "S";
            case 84:
               return "T";
            case 85:
               return "U";
            case 86:
               return "V";
            case 87:
               return "W";
            case 88:
               return "X";
            case 89:
               return "Y";
            case 90:
               return "Z";
            case 97:
               return "a";
            case 98:
               return "b";
            case 99:
               return "c";
            case 100:
               return "d";
            case 101:
               return "e";
            case 102:
               return "f";
            case 103:
               return "g";
            case 104:
               return "h";
            case 105:
               return "i";
            case 106:
               return "j";
            case 107:
               return "k";
            case 108:
               return "l";
            case 109:
               return "m";
            case 110:
               return "n";
            case 111:
               return "o";
            case 112:
               return "p";
            case 113:
               return "q";
            case 114:
               return "r";
            case 115:
               return "s";
            case 116:
               return "t";
            case 117:
               return "u";
            case 118:
               return "v";
            case 119:
               return "w";
            case 120:
               return "x";
            case 121:
               return "y";
            case 122:
               return "z";
            case 48:
               return "0";
            case 49:
               return "1";
            case 50:
               return "2";
            case 51:
               return "3";
            case 52:
               return "4";
            case 53:
               return "5";
            case 54:
               return "6";
            case 55:
               return "7";
            case 56:
               return "8";
            case 57:
               return "9";
            case 46:
               return ".";
            case 45:
               return "-";
            case 95:
               return "_";
            default:
               return "";
         }
      }

Happy Coding

- Xploit

BBS Signature