Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411590 Posts in 69386 Topics- by 58443 Members - Latest Member: Mansreign

May 07, 2024, 04:06:08 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)User Array Input in Flash
Pages: [1]
Print
Author Topic: User Array Input in Flash  (Read 2764 times)
Craig Stern
Level 10
*****


I'm not actually all that stern.


View Profile WWW
« on: January 20, 2009, 02:17:15 PM »

So, I've been using Flash for about 2 and a half years now, but I'm not great at programming yet. I was hoping one of you Actionscript gurus might be able to tell me how this works.

So you use an array for something in your game. You want your players to be able to paste a copy of that array with the values filled in into a text box, and you want the game to then use the user-supplied array. Short of having users type each value into a separate text field (which is a terrible idea and would take forever), how can one have Flash take whatever has been pasted in and use it as the array?
Logged

Ivan
Owl Country
Level 10
*


alright, let's see what we can see


View Profile
« Reply #1 on: January 20, 2009, 02:20:44 PM »

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/String.html#split()
Logged

http://polycode.org/ - Free, cross-platform, open-source engine.
Craig Stern
Level 10
*****


I'm not actually all that stern.


View Profile WWW
« Reply #2 on: January 20, 2009, 02:45:46 PM »

Thanks--that helps a lot!

For anyone reading this and looking for AS 2.0 documentation on the string.split method, I found it here.

Code:
Array - An array containing the substrings of my_str.
Example

The following example returns an array with five elements:

var my_str:String = "P,A,T,S,Y";
var my_array:Array = my_str.split(",");
for (var i = 0; i<my_array.length; i++) {
    trace(my_array[i]);
}
// output:
    P
    A
    T
    S
    Y

The following example returns an array with two elements, "P" and "A":

var my_str:String = "P,A,T,S,Y";
var my_array:Array = my_str.split(",", 2);
trace(my_array); // output: P,A
Logged

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #3 on: January 20, 2009, 03:19:52 PM »

Yeah, you're gonna have to go for sting.split/join. If you were using AS3, you'd be able to serialize to JSON and AMF (amongst others), which would be better for more complicated structures. But you're not, so never mind.
Also, unless you are intending to share these arrays, you should be using the LocalStore interface, or whatever it is, the Flash equivalent of cookies.
Logged
Craig Stern
Level 10
*****


I'm not actually all that stern.


View Profile WWW
« Reply #4 on: January 20, 2009, 03:58:59 PM »

Thanks Boris, that's exactly it: these arrays are to be shared by users, and it would be much better if they did not have to send .sol files to one another.

Okay, another noob question here. How do you get the string.split function to begin splitting X number of characters into a string?

For example:

Code:
var userValues = "40,description,none,80,more description,none,50,description,none,90,more description,none"
var brandNewArray:Array;
brandNewArray[0] = userValues.split("," 3);

so that grabs the first three entries in the string--"40,description,none"--and makes it into the first row of brandNewArray. But how can I get brandNewArray[1] to equal the fourth through sixth entries--"80,more description,none"?
Logged

Gnarf
Guest
« Reply #5 on: January 20, 2009, 04:44:16 PM »

I don't really know ActionScript, so apologies if this is somehow way off or I'm missing some obvious solution that is way awesome. But I'd guess a fairly straightforward way to do it would be to just to split the entire string into some temporary array, and then shove bits and pieces of that array into the kind-of-2D-array. Something along the lines of:

Code:
var my_array:Array;
var tmp:Array = my_str.split(",");
while (tmp.length > 2) {
    my_array.push(tmp.slice(0, 3));
    tmp.splice(0, 3);
}

Though if you can spare a character value, you can just use a different delimiter for the rows. Like, if you use ';' for that, then:

Code:
var userValues = "40,description,none;80,more description,none;50,description,none;90,more description,none";
var my_array:Array = userValues.split(";").split(",");
Logged
Craig Stern
Level 10
*****


I'm not actually all that stern.


View Profile WWW
« Reply #6 on: January 20, 2009, 05:02:45 PM »

I tried that exact thing using the semicolon as a delimiter every third entry, but Flash didn't like it--it just started returning everything undefined.

I was just trying something like the slice method you mentioned:

Code:
brandNewArray[0] = userValues.split("," , 3);
var lengthTester:String = userValues.split("," , 3);
var charsToDelete:Number = lengthTester.length + 1;
userValues = userValues.substr(charsToDelete); //create a new string starting at charsToDelete, make userValues equal it

lengthTester.length keeps returning as 3 instead of 19 for some reason, and brandNewArray[0] keeps returning as undefined.
« Last Edit: January 20, 2009, 05:28:26 PM by CraigStern » Logged

Gnarf
Guest
« Reply #7 on: January 20, 2009, 05:26:08 PM »

Not sure about that split you're doing in the second line. I suppose you'd have to join that array (that the split returns) again in order to get a string out of it (and that now you're just getting the length of your three element long array in the line below). Like, var lengthTester:String = userValues.split("," , 3).join(",");.

Or just ditch that line and do var charsToDelete:Number = brandNewArray[0][0].length + brandNewArray[0][1].length + brandNewArray[0][2].length + 3; or something to that effect instead.
Logged
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #8 on: January 20, 2009, 05:28:44 PM »

Try:
Code:
var s="asdf,1,0;foo,3,2;"
var a = s.split(";");
for(var i=0;i<a.length;i++)
{
a[i] = a[i].split(",")
}
//Done
trace(a[0][0]);//asdf
trace(a[1][2]);//2
You cannot do string.split(";").split(",") for obvious reasons.

Even if you do things another way, I strongly recommend against using the second argument of split. Much better to get an array straight away, and then work with the array. Array has a slice function is better than trying to figure out where the characters are.
Logged
Craig Stern
Level 10
*****


I'm not actually all that stern.


View Profile WWW
« Reply #9 on: January 20, 2009, 05:31:14 PM »

Oh, cool. Thanks a lot, guys! I'm going to try this later--I'm getting a hunger headache right now.  Hand Fork Left Tired Hand Knife Right
Logged

Gnarf
Guest
« Reply #10 on: January 20, 2009, 05:40:58 PM »

You cannot do string.split(";").split(",") for obvious reasons.

Very obvious Embarrassed Sorry about that.
Logged
Craig Stern
Level 10
*****


I'm not actually all that stern.


View Profile WWW
« Reply #11 on: January 20, 2009, 08:57:19 PM »

Well, I think I figured out a solution: just make arrays with only one entry per row, then use a for loop to fill in the master array with the values from brandNewArray. It's not ideal, but at least it's working.
Logged

Glaiel-Gamer
Guest
« Reply #12 on: January 20, 2009, 09:06:39 PM »

or treat the array as a 1D array, but before you ask for the array make the first 2 elements be width and height

like:

2, 2 : 1, 2, 3, 4,

would generate the array,

1  2
3  4

 just make sure to cut off everything before the : before you call array.split, then you can parse the 1D array into a 2D one fairly easily with a for loop if you want to.


Logged
Craig Stern
Level 10
*****


I'm not actually all that stern.


View Profile WWW
« Reply #13 on: January 21, 2009, 06:38:11 AM »

Interesting. But wouldn't that just run into the same problem I had before with removing elements from a split string (unless the string is restricted to one character for the width value)?
Logged

Gnarf
Guest
« Reply #14 on: January 21, 2009, 07:01:23 AM »

I think you'd run into all the same problems. What he's suggesting is mostly what it sounds like you ended up doing anyway, only with a variable number of columns (and rows). Given the examples of strings you've used, it sounds like you're pretty sure you just need 3 columns no matter what, so I don't suppose you need that now.

Well, I think I figured out a solution: just make arrays with only one entry per row, then use a for loop to fill in the master array with the values from brandNewArray. It's not ideal, but at least it's working.

I think it sounds a lot more ideal than messing around with substrings and so on, though Smiley
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic