all groups > flash data integration > february 2006 >
You're in the

flash data integration

group:

Need to Access a Local Variable


Need to Access a Local Variable MajorH
2/15/2006 12:00:00 AM
flash data integration:
How do I access a variable that is generated within a function? For example, in
the code below, I need to access the "count" variable (which is generated in
the "startMovie" function) in another layer.

function startMovie(success) {
if (success == true) {
var childItems:Array =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/title");
var descNodes:Array =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/description"
);

count = childItems.length;
Re: Need to Access a Local Variable Motion Maker
2/18/2006 4:11:21 PM
I do not see count variable in the startMovie function. Perhaps you meant
the childItems variable.

var childItems:Array
function startMovie(success) {
if (success == true) {
childItems =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/title");
var descNodes:Array =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/description"
);

count = childItems.length;


--
Lon Hosford
www.lonhosford.com
May many happy bits flow your way!
[quoted text, click to view]
How do I access a variable that is generated within a function? For example,
in
the code below, I need to access the "count" variable (which is generated in
the "startMovie" function) in another layer.

function startMovie(success) {
if (success == true) {
var childItems:Array =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/title");
var descNodes:Array =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/description"
);

count = childItems.length;

Re: Need to Access a Local Variable Motion Maker
2/18/2006 4:21:00 PM
or
var count:Number;

function startMovie(success) {
if (success == true) {
var childItems:Array =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/title");
var descNodes:Array =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/description"
count = childItems.length;
);


[quoted text, click to view]
How do I access a variable that is generated within a function? For example,
in
the code below, I need to access the "count" variable (which is generated in
the "startMovie" function) in another layer.

function startMovie(success) {
if (success == true) {
var childItems:Array =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/title");
var descNodes:Array =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/description"
);

count = childItems.length;

Re: Need to Access a Local Variable MajorH
2/20/2006 12:00:00 AM
Sorry, it still doesn't work. The variable "count" has to be inside the
function in order to get the value of the number of nodes in the XML file. BUT,
I need to use that value elsewhere in my Flash movie (outside of the function).
Can anyone help?
Re: Need to Access a Local Variable Motion Maker
2/20/2006 12:00:00 AM
Declaring a variable on the starting time line (Scene 1 or root)
var count:Number = 1;
makes its scope a time line variable and thus all functions created on the
timeline will have access to the variable.

var count:Number = 1;
function abc()
{
trace (count)
count ++;
}
abc()
trace (count)
If you use Control->Test movie for the code above you will see your output
window a 1 and 2.

If you cannot access it in the function then you have presented all the
details or you are having XML parsing problems.

Finally you probably need to use Control->Test Movie and then look at the
variables with Debug->List Variables.


You can go to a higher level scope

_global.count = 1;

Use _global.count everywhere and if you do not see the values there, I
suspect it is not the issue of scope (inside outside of a function) and
something else about your code.

--
Lon Hosford
www.lonhosford.com
May many happy bits flow your way!
[quoted text, click to view]
Sorry, it still doesn't work. The variable "count" has to be inside the
function in order to get the value of the number of nodes in the XML file.
BUT,
I need to use that value elsewhere in my Flash movie (outside of the
function).
Can anyone help?

Re: Need to Access a Local Variable Motion Maker
2/20/2006 9:19:21 PM
Ok it is clearer now.

And you are correct there is a delay. That is expected.

When you issue the slideshow.load method, requests go out over the network.
There is no guarantee when the response will arrive and as such the
processing of the onLoad method. So in the meantime the code following
slideshow.load is fully processed and does not waiting for the data to be
received. In general the code will be processed way faster than the network
response.

Do not think of onLoad as a subroutine but as a separate thread processing
on its own sweet time.

When the data is returned the onLoad method is called. Only then you can use
the values you derive from the XML data file.

So what you want to do is in the onLoad add a gotoAndStop(___) or
nextFrame() as the last line in the success if block. On that frame you can
then have access to all the data received. You can also add a gotoAndStop()
for the failure if block.

I put the lines in here but you need to supply the frame numbers.

//Loads function if XML file loaded
function startMovie(success)
{
if (success == true)
{
//trace(slideshow)
//Loads content from nodes from XML
var childItems:Array = mx.xpath.XPathAPI.selectNodeList(this.firstChild,
"rss/channel/item/title");
var descNodes:Array = mx.xpath.XPathAPI.selectNodeList(this.firstChild,
"/rss/channel/item/description");
var urlNodes:Array = mx.xpath.XPathAPI.selectNodeList(this.firstChild,
"/rss/channel/item/link");
//Counts # of Item nodes

count = childItems.length;
trace(count);
//Var z is the _x position of movie
z = 10;
//Create Headline, Description text fields and extract URL
for (var i = 0; i < count; i++)
{
trace(_root.slider_mc.createTextField("headline" + i,
_root.slider_mc.getNextHighestDepth(), z, 10, 50, 100));
trace(_root.slider_mc.createTextField("desc" + i,
_root.slider_mc.getNextHighestDepth(), z + 40, 125, 50, 200));
//Increase _x position for each Item
z += 600;
var headline:String = "headline" + i;
var desc:String = "desc" + i;
var link:String = "link" + i;
var txtFmt:TextFormat = new TextFormat();
var descFmt:TextFormat = new TextFormat();
var linkFmt:TextFormat = new TextFormat();
txtFmt.size = 30;
descFmt.size = 22;
linkFmt.size = 18;
trace(_root.slider_mc[headline].text =
childItems[i].firstChild.nodeValue);
trace(_root.slider_mc[desc].text = descNodes[i].firstChild.nodeValue);
_root.slider_mc[headline].setTextFormat(txtFmt);
_root.slider_mc[headline].wordWrap = true;
_root.slider_mc[headline].multiline = true;
_root.slider_mc[headline]._width = 490;
_root.slider_mc[headline].html = true;
_root.slider_mc[headline].htmlText = "<bold><font size='30'
face='Arial'><ahref='" + urlNodes[i].firstChild.nodeValue + "'
target='_blank'>" + childItems[i].firstChild.nodeValue +
"</a></font></bold>";
_root.slider_mc[desc].setTextFormat(descFmt);
_root.slider_mc[desc].wordWrap = true;
_root.slider_mc[desc].multiline = true;
_root.slider_mc[desc]._width = 450;

}
gotoAndStop(______frame number for success______);
}
else
{
this.createTextField("error", 10, 10, 10, 320, 100);
error.text = "Didn't work";
gotoAndStop(______frame number for failure______);
}
}
var count:Number = 1;
//Loads RSS file from Digg.com
import mx.xpath.XPathAPI;
slideshow = new XML();
slideshow.onLoad = startMovie;
slideshow.load("http://www.digg.com/rss/index.xml");
slideshow.ignoreWhite = true;
trace("a count" + count);
stop();

--
Lon Hosford
www.lonhosford.com
May many happy bits flow your way!

Re: Need to Access a Local Variable MajorH
2/20/2006 9:46:29 PM
I'm posting my entire code. With this, I get a value of "1" for both traces on
count, even though the second should output the number of nodes in the XML file
(which is actually 40). I think the problem may be that the second count does
not take the number of nodes because the XML file loads too slowly. In
addition, placing "_global" in front of every count variable does not fix
things.

Code:


//Loads function if XML file loaded
function startMovie(success) {
if (success == true) {

//Loads content from nodes from XML
var childItems:Array =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,"rss/channel/item/title");
var descNodes:Array =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/description"
);
var urlNodes:Array =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/link");

//Counts # of Item nodes
trace(count);
count = childItems.length;

//Var z is the _x position of movie
z = 10;

//Create Headline, Description text fields and extract URL
for (var i = 0; i < count; i++){
trace(_root.slider_mc.createTextField("headline" + i,
_root.slider_mc.getNextHighestDepth(), z, 10, 50, 100));
trace(_root.slider_mc.createTextField("desc" + i,
_root.slider_mc.getNextHighestDepth(), z + 40, 125, 50, 200));

//Increase _x position for each Item
z += 600;

var headline:String = "headline" + i;
var desc:String = "desc" + i;
var link:String = "link" + i;

var txtFmt:TextFormat = new TextFormat();
var descFmt:TextFormat = new TextFormat();
var linkFmt:TextFormat = new TextFormat();
txtFmt.size = 30;
descFmt.size = 22;
linkFmt.size = 18;

trace(_root.slider_mc[headline].text = childItems[i].firstChild.nodeValue);
trace(_root.slider_mc[desc].text = descNodes[i].firstChild.nodeValue);

_root.slider_mc[headline].setTextFormat(txtFmt);
_root.slider_mc[headline].wordWrap = true;
_root.slider_mc[headline].multiline = true;
_root.slider_mc[headline]._width = 490;
_root.slider_mc[headline].html = true;
_root.slider_mc[headline].htmlText = "<bold><font size='30' face='Arial'><a
href='"+urlNodes[i].firstChild.nodeValue+"'
target='_blank'>"+childItems[i].firstChild.nodeValue+"</a></font></bold>";

_root.slider_mc[desc].setTextFormat(descFmt);
_root.slider_mc[desc].wordWrap = true;
_root.slider_mc[desc].multiline = true;
_root.slider_mc[desc]._width = 450;

}
}

else {
this.createTextField("error", 10, 10, 10, 320, 100);
error.text = "Didn't work";
}
}

var count:Number = 1;

//Loads RSS file from Digg.com
import mx.xpath.XPathAPI;
slideshow = new XML();
slideshow.onLoad = startMovie;
slideshow.load("http://www.digg.com/rss/index.xml");
slideshow.ignoreWhite = true;

trace(count);

stop();
Re: Need to Access a Local Variable Motion Maker
2/21/2006 6:30:12 PM

trace(_root.slider_mc.createTextField("headline" + i,
_root.slider_mc.getNextHighestDepth(), z, 10, 50, 100));

You may want to trace the individual parts to see what is there.

trace ("z = " + z)
trace ("i = " + i)
trace ("_root.slider_mc = " + _root.slider_mc)
trace ("_root.slider_mc.getNextHighestDepth() = " +
_root.slider_mc.getNextHighestDepth())
trace ("_root.slider_mc[\"headline\" + i ] = " + _root.slider_mc["headline"
+1])

If slider_mc is on stage at design time, then have it also on the frame you
are using the createTextField method.

It may also need to be on the the frame before the frame you first use it in
code.

--
Lon Hosford
www.lonhosford.com
May many happy bits flow your way!
[quoted text, click to view]
Ok. It's working better now, but there are still a few quirks. By placing a
stop() action on the frame that reads in the XML, and moving my movie
(slider_mc) to another frame, it prevents the code from creating text fields
in
slider_mc. In other words, I need to figure out how to tell it to create my
text fields on frame 2 in the slider_mc movie. The code I'm referencing is
below.

trace(_root.slider_mc.createTextField("headline" + i,
_root.slider_mc.getNextHighestDepth(), z, 10, 50, 100));

Re: Need to Access a Local Variable MajorH
2/21/2006 10:15:03 PM
Ok. It's working better now, but there are still a few quirks. By placing a
stop() action on the frame that reads in the XML, and moving my movie
(slider_mc) to another frame, it prevents the code from creating text fields in
slider_mc. In other words, I need to figure out how to tell it to create my
text fields on frame 2 in the slider_mc movie. The code I'm referencing is
below.

trace(_root.slider_mc.createTextField("headline" + i,
_root.slider_mc.getNextHighestDepth(), z, 10, 50, 100));
Re: Need to Access a Local Variable Motion Maker
2/21/2006 10:26:45 PM
[quoted text, click to view]

I do not see why you cannot.

From what I can see via the code you have slider_mc on the timeline at
design time, in other words you did not use createEmptyMovieClip to make it.

So you can use it on any frame on the timeline as long as you have it on
that frame.

If the code you posted so far is on frame 1 and so is slider_mc, then you
only need to extend keyframe containing slider_mc to the next frame or
frames to use it.

If however you reach a frame that does not contain slider_mc, you will not
have predictable results as it is going in and out of scope.




--
Lon Hosford
www.lonhosford.com
May many happy bits flow your way!
[quoted text, click to view]
Slider_mc is the movie that I need to have the variable count equal the
total
number of nodes. If I want it to take the correct value of count, I have to
have it on the second frame. As I asked before, is there anyway I can create
the textfield on the second frame from the first?

Re: Need to Access a Local Variable MajorH
2/22/2006 2:09:16 AM
Slider_mc is the movie that I need to have the variable count equal the total
number of nodes. If I want it to take the correct value of count, I have to
have it on the second frame. As I asked before, is there anyway I can create
the textfield on the second frame from the first?
AddThis Social Bookmark Button