Solved

ActionScript 2 load text from .xml and loop

Posted on 2010-08-26
8
803 Views
Last Modified: 2013-11-11
Hello Experts,
I have been working on this for a while, and cannot get it working. I am quite new to AS. Ultimately what I would like to achieve is to read a number of strings \ values from an .xml file, and as each one is read, load the string into a text box within a movie clip, then do an alpha tween from 0 --> 100, then remove the text from the stage and repeat for the next value. Once the end of the .xml file is reached, I would want it to loop and the process repeat. Additionally, I am trying to get the text box to appear at random positions within a designated area on the stage for each string. The code I have so far is pasted below. What is happening is that the last string in the .xml file seems to work the way I want it to, but it is the only string that is displayed. The remaining strings from the .xml file are not displayed. If anyone has any advice on how to
a) Make all strings load, alpha tween, then disappear
b) get the process to repeat
I would be very grateful!
Thanks very much,
Russ

import mx.transitions.Tween;

import mx.transitions.easing.*;

createEmptyMovieClip("friend_mc", 1);

function loadXML(loaded) {

if (loaded) {

xmlNode = this.firstChild;

friendText = [];

total = xmlNode.childNodes.length;

for (i=0; i<total;i++) {

friendText[i] = xmlNode.childNodes[i].childNodes[0].nodeValue;

var intStringLength = friendText[i].length;

var intStringMaxWidth = intStringLength*13;

var intMaxXPos = 850-intStringMaxWidth;

var intRanXPos = Math.round(Math.random()*intMaxXPos);

var intRanYPos = Math.round(Math.random()*380);

friend_mc.createTextField("friendName_txt", 1, intRanXPos, intRanYPos, intStringMaxWidth, 41);

friend_mc.friendName_txt.text = friendText[i]

friend_mc.friendName_txt.embedFonts = true;

friendformat = new TextFormat();

friendformat.font = "Brush Script MT";

friendformat.color = 0x006633;

friendformat.size = 30;

friendformat.bold = true;

friend_mc.friendName_txt.setTextFormat(friendforma t);

var visInTween:Tween = new Tween(friend_mc.friendName_txt, "_alpha", Regular.easeOut, 0, 100, 30, false);

//var visOutTween:Tween = new Tween(friend_mc.friendName_txt, "_alpha", Regular.easeOut, 100, 0, 30, false);





}

friendNames();

} else {

content = "file not loaded!";

}

}

xmlData = new XML();

xmlData.ignoreWhite = true;

xmlData.onLoad = loadXML;

xmlData.load("friends.xml");

-------------------------------------------------------------

friends.xml format:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<friends>

<friend>Friend 1</friend>

<friend>Friend 2</friend>

<friend>Friend 3</friend>

etc..... 

</friends>

Open in new window

0
Comment
Question by:rdillion
  • 4
  • 4
8 Comments
 
LVL 19

Expert Comment

by:moagrius
ID: 33534824
this is how i'd do it
import mx.transitions.Tween;
import mx.transitions.easing.*;

// just set all your variables up once, no sense doing it in every loop iteration

var maxX = 850;
var maxY = 380;

var strings = [];

var friendformat = new TextFormat();
friendformat.font = "Brush Script MT";
friendformat.color = 0x006633;
friendformat.size = 30;
friendformat.bold = true;

// create one container, no need for a new one each time
var container = this.createEmptyMovieClip(0, 1);

// and one textfield...
var textfield = container.createTextField(0, 0, 0, 0, 200, 41);
textfield.setNewTextFormat(friendformat);
textfield.embedFonts = true;			
			
function loadXML(loaded) {
	if (loaded) {
		xmlNode = this.firstChild;
		var total = xmlNode.childNodes.length;
		// dump it all into an array, so we can easily cycle through it top to bottom and back again
		for (i=0; i<total; i++) {
			strings[i] = xmlNode.childNodes[i].childNodes[0].nodeValue;			
		}
		// fire the first effect
		showNext();
	} else {
		content = "file not loaded!";
	}
}

xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("friends.xml");

// this is the function that does all the work
function showNext(){
	var current = strings.shift();  // get the next string
	strings.push(current);  // put it back at the end
	container._alpha = 0;  // set container transparency
	textfield.text = current;  // set the text
	textfield._width = textfield.textWidth + 20;  // set the width
	container._x = Math.random() * maxX - textfield._width;  // position it randomly based on the seeds above +
	container._y = Math.random() * maxY - textfield._height; // and the current dimensions after text has been applied
	var visInTween:Tween = new Tween(container, "_alpha", Regular.easeOut, 0, 100, 30, false);  // tween it
	visInTween.onMotionFinished = showNext;  // when tween is done, call this same function - rinse and repeat
}

Open in new window

0
 

Author Comment

by:rdillion
ID: 33542641
Hello moagrius,
Thank you very much for your help on this!!  I truly appreciate it!  I tried the code you posted, and I am getting an error: "There is no property with the name 'onMotionFinished'.  visInTween.onMotionFinished = showNext;".  I am using Flash MX Professional 2004, as it is currently the only version available to me.  I don't know if that is the problem or not?  I did some searching, and saw a couple of posts indicating that I would need to change the tween to an object (var visInTween:Object instead of var visInTween:Tween ).  I tried this and did not get the error when running the code, but do not see anything display on the stage when the code is run.  Any thoughts?
Thanks again
0
 
LVL 19

Expert Comment

by:moagrius
ID: 33542767
the built-in Tween isn't that great anyway.

MX...  wow that's old : )

i included a very old (6 years) prototype that'll do the same thing.
MovieClip.prototype.tween = function(steps, ease, props, callback) {
	var out = function (a, b) {
		return 1-pow(1-a, b);
	};
	var pow = Math.pow;
	var abs = Math.abs;
	var sqr = Math.sqrt;
	var target = this;
	var values = [];
	var factor = sqr(abs(ease))+1;
	var easing = ease>0 ? out : pow;
	var loop = steps;
	var obj;
	var prop;
	do {
		obj = {};
		for (prop in props) obj[prop] = target[prop]+(props[prop]-target[prop])*easing(loop/steps, factor) | 0;
		values[steps-(--loop)] = obj;
	} while (loop);
	target.onEnterFrame = function() {
		var dest = values.pop();
		for (prop in dest) target[prop] = dest[prop];
		if (!values.length) {
			delete target.onEnterFrame;
			if(callback) callback();

		}
	};
};

var maxX = 850;
var maxY = 380;

var strings = [];

var friendformat = new TextFormat();
friendformat.font = "Brush Script MT";
friendformat.color = 0x006633;
friendformat.size = 30;
friendformat.bold = true;

var container = this.createEmptyMovieClip(0, 1);
var textfield = container.createTextField(0, 0, 0, 0, 200, 41);
textfield.setNewTextFormat(friendformat);
textfield.embedFonts = true;
textfield.text = "Hi there";
			
			
function loadXML(loaded) {
	if (loaded) {
		xmlNode = this.firstChild;
		var total = xmlNode.childNodes.length;
		for (i=0; i<total; i++) {
			strings[i] = xmlNode.childNodes[i].childNodes[0].nodeValue;			
		}
		showNext();
	} else {
		content = "file not loaded!";
	}
}

xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("friends.xml");

function showNext(){
	var current = strings.shift();
	strings.push(current);
	container._alpha = 0;
	textfield.text = current;
	textfield._width = textfield.textWidth + 20;
	container._x = Math.random() * maxX - textfield._width;
	container._y = Math.random() * maxY - textfield._height;
	container.tween(30, 1, { _alpha : 100 }, showNext);
}

Open in new window

0
 

Author Comment

by:rdillion
ID: 33545098
Hey moagrius,
Yeah, it is old, hehe, I'd like to get into this a lot more in the future, which should justify a newer version, but for now....  
I tried the new code you gave, and I am still not seeing anything on the stage when it is run.  I added a trace statement after the "strings.push(current);" line of code (trace (current);).  When I run that, all of the strings from the .xml file are shown correctly in the trace window.  I then tried a trace statement after the "textfield.text = current;" line of code (I tried both trace (textfield.text); and trace (textfield);) and both only yielded "undefined" in the trace box.  
Again, thank you very much for your help with this!!!
Russ
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 19

Expert Comment

by:moagrius
ID: 33545154
i know it works - i ran it.  do you have the font installed?  you might want to comment out the line with embedFonts=true while debugging.

you can also try setting the text immediately to something random ("Hello World") to make sure it's rendering.

ahh wait...  MX doesn't return a reference to the textfield...

try changing line 43 from :

var textfield = container.createTextField(0, 0, 0, 0, 200, 41);

to :

container.createTextField("textfield", 0, 0, 0, 200, 41);

try that with the traces you have currently...

see normally (in every language but AS2 for FP7), you can say someVar = something();

but for unknown reasons in MX, createTextField didn't return a reference, so that's probably coming up empty...

0
 
LVL 19

Accepted Solution

by:
moagrius earned 500 total points
ID: 33545370
ok I just ran it targetting FP7, and this works (it was the texfield reference, you just need to add one line).

change line 43 from :

var textfield = container.createTextField(0, 0, 0, 0, 200, 41);

to :

container.createTextField("textfield", 0, 0, 0, 200, 41);
var textfield = container.textfield;

working snippet attached
MovieClip.prototype.tween = function(steps, ease, props, callback) {
	var out = function (a, b) {
		return 1-pow(1-a, b);
	};
	var pow = Math.pow;
	var abs = Math.abs;
	var sqr = Math.sqrt;
	var target = this;
	var values = [];
	var factor = sqr(abs(ease))+1;
	var easing = ease>0 ? out : pow;
	var loop = steps;
	var obj;
	var prop;
	do {
		obj = {};
		for (prop in props) obj[prop] = target[prop]+(props[prop]-target[prop])*easing(loop/steps, factor) | 0;
		values[steps-(--loop)] = obj;
	} while (loop);
	target.onEnterFrame = function() {
		var dest = values.pop();
		for (prop in dest) target[prop] = dest[prop];
		if (!values.length) {
			delete target.onEnterFrame;
			if(callback) callback();

		}
	};
};

var maxX = 850;
var maxY = 380;

var strings = [];

var friendformat = new TextFormat();
friendformat.font = "Brush Script MT";
friendformat.color = 0x006633;
friendformat.size = 30;
friendformat.bold = true;

var container = this.createEmptyMovieClip(0, 1);
container.createTextField("textfield", 0, 0, 0, 200, 41);
var textfield = container.textfield;
textfield.setNewTextFormat(friendformat);
textfield.embedFonts = true;
textfield.text = "Hi there";
			
			
function loadXML(loaded) {
	if (loaded) {
		xmlNode = this.firstChild;
		var total = xmlNode.childNodes.length;
		for (i=0; i<total; i++) {
			strings[i] = xmlNode.childNodes[i].childNodes[0].nodeValue;			
		}
		showNext();
	} else {
		content = "file not loaded!";
	}
}

xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("friends.xml");

function showNext(){
	var current = strings.shift();
	strings.push(current);
	container._alpha = 0;
	textfield.text = current;
	textfield._width = textfield.textWidth + 20;
	container._x = Math.random() * maxX - textfield._width;
	container._y = Math.random() * maxY - textfield._height;
	container.tween(30, 1, { _alpha : 100 }, showNext);
}

Open in new window

0
 

Author Comment

by:rdillion
ID: 33545564
moagrius,
Thank you very much!!  When I comment out the "textfield.embedFonts = true;" line of code it works!  I need to do a little more work on the tween variables, but it is working and I sincerely thank you!!  I have been wanting to get this going for quite some time, as I will be using it on a site that promotes a golf outing fund raiser my family and I host each year to help fight cancer.  I truly appreciate all of your help and wish you all the best!!  :)
Russ
0
 

Author Closing Comment

by:rdillion
ID: 33545575
moagrius was extremely helpful and courteous, thanks again!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now