Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Flash / Data Integration / February 2006



Tip: Looking for answers? Try searching our database.

Need to Access a Local Variable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MajorH - 15 Feb 2006 06:01 GMT
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;
Motion Maker - 18 Feb 2006 21:11 GMT
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;

Signature

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

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;
Motion Maker - 18 Feb 2006 21:21 GMT
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;
);

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;
MajorH - 20 Feb 2006 06:26 GMT
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?
Motion Maker - 20 Feb 2006 11:34 GMT
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.

Signature

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

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?
MajorH - 20 Feb 2006 21:46 GMT
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();
Motion Maker - 21 Feb 2006 02:19 GMT
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();

Signature

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

MajorH - 21 Feb 2006 22:15 GMT
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));
Motion Maker - 21 Feb 2006 23:30 GMT
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.

Signature

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

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));
MajorH - 22 Feb 2006 02:09 GMT
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?
Motion Maker - 22 Feb 2006 03:26 GMT
>>As I asked before, is there anyway I can create the textfield on the
>>second frame from the first?

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.

Signature

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

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?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.