Log in

View Full Version : can't get the text to alternate on refresh



zeech26
03-17-2008, 08:44 PM
Hello again,
This is my second post on the same subject, and I am still lost.

On this homepage SWF that I built [ http://www.storevault.com/ ]
The client wants the header [ The Perfect Fit... ] to change each time the page refreshes.
So, first view of the page would be "The perfect fit...", then on reload, it would be, "New storage solutions..."

I am not very good with actionscripts, and am having trouble trying to find an easy solution for this. I have investigated Arrays, and even was given some code from this forum which was:

The basic principle is you load all of the text/items into and array. Then use the Math.random() function to randomly choose an index for the item.

If you want to hardcode the values, you could do something like this:

var slogans = array("Text 1", "Text 2", "Text 3");
var randomNumber = Math.ceil(Math.random()*3);
text_field.text = slogans[randomNumber]

I don't even know where to put this code, would it go at the end where the actions tell the movie to stop?
Is there a reload function that will direct the movie to play the alternate text?
There has to be an easy way to do this, and good karma to whomever can enlighten me!

HELP! I am clueless...

Zack 26
www.zeechproductions.com

Medyman
03-18-2008, 01:33 AM
Hello again Zack,

Sorry to hear that my last bit of advice confused you. Also sorry if you were hoping for someone else ;) lol. There are only two of us that regularly answer Flash/ActionScript questions on these forums and Blizzard is busy with other things currently, it seems.

Anyway, to your questions...


I don't even know where to put this code, would it go at the end where the actions tell the movie to stop?


Well, I don't know how your movie is set up so I can't answer that. It would go on the same frame as you have that text field.

If you need the text to change sequentially, then there isn't an "easy" solution. In order to track the order, you'll have to implement some sort of external method (probably cookies in conjunction with either PHP -- or another server side script -- or javascript).

If you just want the text to change randomly, then it's fairly "easy". Just take the AS that I have written below and put it on the same frame as the text field. I've uploaded a simple example here (http://img444.imageshack.us/my.php?image=randomarraymy2.swf)to show you how it would look. You can download the Flash 8 .fla source file here (http://www.mediafire.com/?xzwyy122yml).

So here is the actionscript (in a slightly different syntax to make it easier to understand):


var fruits = new Array();
fruits[0] = "APPLES";
fruits[1] = "GRAPES";
fruits[2] = "ORANGES";
fruits[3] = "PEARS";
fruits[4] = "BANANAS";

var randomNumber = Math.round(Math.random()*(fruits.length-1));
text_field.text = fruits[randomNumber]

A few things to note:
a) the text field must be set to "Dynamic Text" in the properties panel.
b) if you're using a non-standard font, be sure to embed it

Hope it helps :)

zeech26
03-18-2008, 04:13 PM
Thanks Medyman!
I will try this and let you know!
I really appreciate the help.
cheers,
zack

zeech26
03-18-2008, 04:25 PM
Medyman, one more quick question,
would this be easier with images?
If I made the text blocks symbols, would that be easier?
And by the way, it worked! but, it loops now, it seems to not see the stop I put in the AS at the end of the sequence.

cheers
zack

Medyman
03-18-2008, 04:57 PM
Medyman, one more quick question,
would this be easier with images?
If I made the text blocks symbols, would that be easier?
And by the way, it worked! but, it loops now, it seems to not see the stop I put in the AS at the end of the sequence.

cheers
zack

Hmm, the principle would remain the same, even with images. It might even be harder.

You could hardcode the number of images you have and then make a movieclip with a new image on each frame. Then randomly choose which frame to show.

OR

You could list the URLs of the images within the array and the using the MovieClipLoader class bring them in dynamically.

But both of these are more complicated than just text.


As for the stop action...not sure what's happening. Perhaps try adding the stop at the top of the code block instead of the end.

zeech26
03-18-2008, 05:48 PM
Last question I promise Medyman!
Can I use the same code and make it not random?
So that it would always open with, "text01..."
and on the refresh, "text02...", and then back to text01?
Thanks, and I promise this the last question (for today),
I got it going right now and it looks great, the stop issue was my fault, had an extra character at the end of the code.

Thanks again for your awesome help,
zack

Medyman
03-18-2008, 07:14 PM
Can I use the same code and make it not random?
So that it would always open with, "text01..."
and on the refresh, "text02...", and then back to text01?


Afraid not...
For that to work, you would need to be able to track which message was shown last. There is no way to that within Flash.

Again, you would need to set a cookie via PHP or ASP or javascript and then also read it using probably the same language you used to set it. That cookie value would then be added as a FlashVar into the flash embed code and then finally read by the Flash.

It's really not as difficult as it might seem but it might be more trouble than it's worth for a banner.

zeech26
03-18-2008, 09:36 PM
Medyman, fixed it!
Here is the code if you are interested in seeing it.
I used a split, and that made it work by defining which text goes to which text block, and it works great.

var fruits = new Array();
fruits[0] = "Introducing... SnapManager for SQL,Database backups in seconds vs. hours";
fruits[1] = "The Perfect Fit... for your growing storage needs,Introducing the next generation S550 and new S300";
var randomNumber = Math.round(Math.random()*(fruits.length-1));
var displayText;
displayText = fruits[randomNumber];
var quote = new Array();
quote = displayText.split(",");
text_field.text = quote[0];
text_field2.text = quote[1];

Thanks again for your great help, you pointed me in the right direction!
zack

Medyman
03-19-2008, 04:06 AM
Medyman, fixed it!
Here is the code if you are interested in seeing it.
I used a split, and that made it work by defining which text goes to which text block, and it works great.

var fruits = new Array();
fruits[0] = "Introducing... SnapManager for SQL,Database backups in seconds vs. hours";
fruits[1] = "The Perfect Fit... for your growing storage needs,Introducing the next generation S550 and new S300";
var randomNumber = Math.round(Math.random()*(fruits.length-1));
var displayText;
displayText = fruits[randomNumber];
var quote = new Array();
quote = displayText.split(",");
text_field.text = quote[0];
text_field2.text = quote[1];

Thanks again for your great help, you pointed me in the right direction!
zack


Ohhhh....
I see what you meant now. I was totally misunderstanding you.

The above works fine. You could also use an associative array to cut down on some code, but you don't need to worry about that with only a few quotes.