Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 2.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 01-21-2005, 01:32 AM   #1
Phaelix
Director ex-pat
 
Join Date: Jan 2005
Posts: 29
Default editing a string through AS

I'm trying to use a semi-random selector to pick where a graphic will appear in my movie. What I have at the moment is:

ActionScript Code:
string = "1234"; myScrollPane.onEnterFrame = function() {     if (string.length=0) {         string = "1234";     }     index = random(string.length);     trace(index+" returns "+string.substring(index, index+1)); }

This returns a number from one to four, which is what it's supposed to do but as it stands it's just a verbose version of

ActionScript Code:
random(3)+1

What I want to do is have numbers removed from the string as they're retrieved. To that end I included the "if (string.length = 0)" to repopulate the list. With both of those working I should have a random number generator which will not repeat any number until every number in the list has been used.

My problem is that none of the string operators actually seem to alter the string- slice, charAt and substring all return values while leaving the string untouched. Is there a way to do this with a string, or should I be using something like an array?
Phaelix is offline   Reply With Quote
Old 01-21-2005, 01:56 AM   #2
SmoothDime
Registered User
 
Join Date: Dec 2004
Posts: 112
Default

what are you trying to do?
SmoothDime is offline   Reply With Quote
Old 01-21-2005, 02:47 AM   #3
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

Howdy...

I'd use an array just because it is easier for me...
ActionScript Code:
string = "1234".split(""); trace("String = " + string); this.onEnterFrame = function() {     if (string.length == 0)     {         trace("Resetting the seed...");         string = "1234".split("");     }     index = Math.floor(Math.random() * string.length);     val = string[index];     string.splice(index, 1);     trace(val + " returns " + string); };
That outputs...
Quote:
String = 1,2,3,4
4 returns 1,2,3
2 returns 1,3
1 returns 3
3 returns
Resetting the seed...
3 returns 1,2,4
4 returns 1,2
2 returns 1
1 returns
Resetting the seed...
2 returns 1,3,4
3 returns 1,4
4 returns 1
1 returns
Resetting the seed...
2 returns 1,3,4
4 returns 1,3
...
...
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Old 01-21-2005, 03:04 AM   #4
Phaelix
Director ex-pat
 
Join Date: Jan 2005
Posts: 29
Default

Thx, Cyan- I'll try your method. As per, I'm trying to do something I originally learnt in Director and the syntax has got me going in circles.

<edit>
Worked perfectly. Thanks again.
</edit>

Last edited by Phaelix; 01-21-2005 at 03:15 AM. Reason: newsflash!
Phaelix is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:23 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.