?
Solved

ActionScript 2 load text from .xml and loop

Posted on 2010-08-26
8
Medium Priority
?
812 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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
 
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 2000 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

770 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