flash - Word randomization in Actionscript -


in actionscript, i'm trying find way produce strings , string variables spit out different text each time they're brought up. purely visualize:

var text:string "red||blue"; //whenever variable called, it's either red or blue  textoutput("you spot grey||black cat."); //a function equivalent of same issue 

i can produce function effect, seems variable cannot function, far can tell.

i've considered array variables, have no idea how use array spit out single entry when variable called, , don't know how make work string isn't variable -- assuming can away single system works both situations.

edit

to expand upon issue expressed in batman's answer, using result on variable produces result 'sticks' whichever randomly chooses. example:

var shoes:string = grabrandomitem("red shoes||crimson shoes");  trace("you have " + shoes + ".") //whichever result chosen stays way. 

moreover, may want change variable else entirely not-random:

var secondshoes:string = "blue shoes";  function newshoes(): {      shoes = secondshoes; } 

there ton of ways this, align way you'd it, here simplest way accomplish this:

//populate string: (remove word private if using timeline code) private var text_:string = "red||blue||green||yellow";  //create getter use function property function text():string {     //create array splitting text on instances of ||     var arr:array = text_.split("||");     //return random element of array made     return arr[math.floor(math.random() * arr.length)]; }  trace(text); 

even better, create common function parse string:

function grabrandomitem(str:string):string {     var arr:array = str.split("||");     return arr[math.floor(math.random() * arr.length)]; }  //make getter variable regenerates everytime it's called. function label():string {     return "you spot " + grabrandomitem("grey||black||blue||red||purple") + " cat"; }  trace(label); //anytime access label var, re-generate randomized string  trace(label); trace(label); trace(label); //  ^^ should have different results 

of course, way think works best if text comes user input. if hard coding text app, might create in array directly show in another answer have there's less overhead involved way.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -