Hi guys! ~help with levels~

:slight_smile:

I’ve been checking out kirupa.com for at least a couple of years now, but it’s the first time I’ve posted.

I need a little help with a script I’m working on.

The issue here relates to Flash “levels” ( _root, _level0, this ). I still don’t know how everything works; I came across this news script which takes news from an XML file, now this code, together with the fields (where it was retrieving data) were on _root level, I moved this to a movieclip located on _root.

Before everything was working, now I’m getting “undefined” on all the fields :frowning:

theNews = new XML();
theNews.ignoreWhite = true;
theNews.load('http://ionutz.biz/myspace/flash_news/xml.php');
 
function Article( id, title, date, author, body){
 this.id = id;
 this.title = title;
 this.date = date;
 this.author = author;
 this.body = body;
};
Article.prototype.putOut = function(titletxt, datetxt, authortxt, bodytxt){
 _level0.titletxt.text = this.title;
 _level0.datetxt.text = "On: "+this.date;
 _level0.authortxt.text = "By: "+this.author;
 _level0.bodytxt.text = this.body;
};
function articleOut(xml){
 _global.numArts = xml.firstChild.childNodes.length-1;
 info = xml.firstChild.childNodes[at].attributes;
 body = xml.firstChild.childNodes[at].firstChild.firstChild.nodeValue;
 currArticle = new Article(info.id, info.title, info.date, info.author, body);
 currArticle.putOut("titletxt","datetxt","authortxt","bodytxt");
 delete currArticle;
 delete info;
}
trace(_root._width);
theNews.onLoad = function(){
 articleOut(this);
 at = numArts;
 articleOut(this);
 _root.createEmptyMovieClip("menu", 1000);
 items = this.firstChild.childNodes;
 to = Number(items.length-1);
 for(a = 0; a>= -to; a--){
   _root.menu.attachMovie( "menuItem", "item"+a, a);
  _root.menu["item"+a].nametxt.text = items[to-(-a)].attributes.title;
  _root.menu["item"+a]._y = -20*a;
  _root.menu["item"+a]._y += 10;
  _root.menu["item"+a]._x = 400-63.5;
  _root.menu["item"+a].itemConst = Number(items[0].attributes.id)-1;
  _root.menu["item"+a].itemNum = to-(-a);
  _root.menu["item"+a].onRelease = function(){
   at = this.itemNum;
   _root.articleOut(theNews);
  }
 }
}
articleOut(theNews);

Now the “menu” in functions() is the only thing that works (and I’m guessing that’s because it’s loading the variables at _root level)

Please help =)

Thanks in advance,

John :rambo:

“_level0” is the absolute root timeline (the main clip that everything is in). “this” references the current clip, and “_root” references the root timeline. If you nest a clip within a clip, _root is the same as _level0. However, _level0 is always the base but _root can be changed like so - if you access _root in a clip, and it works, and you then want to put that clip inside another clip, add the following line (inside the nested clip, not the _level0 clip):

this._lockroot = true;

This will make any references to _root in the nested clip see the nested clip’s timeline instead of the _level0 timeline.

If you often build clips on their own before nesting them inside another swf, it would be good practice to get into the habit of turning on _lockroot, and using _level0 if you want to reference the base clip rather than _root.

In your case you shouldn’t need _root at all in most places. eg root.createEmptyMovieClip(“menu”, 1000) would create an empty clip at the base (_level0) level. If you turned on lockroot, it would create it at the level of this nested clip instead. But if you just did createEmptyMovieClip(“menu”, 1000) it would work just the same, without needing _lockroot. So in short, avoid _root unless you really need it - it just causes problems most of the time.

One last thing - u say uv used this site 4 a couple of years. I’ve answered this same question about roots several times in the last few months, and it seems ppl just keep asking it again. Searching for an answer b4 posting would be more helpful for ppl using this forum, and 4 u 2 because you’ll get answers much quicker.

Thanks!

I’ll use _lockroot

I’ve used the site for the last couple of years not the forums :confused:

John

Oh… another question

What’s up with putting

_root[whatever].text, why the [] ?

When you attach clips dynamically and give them a name as a parameter, that name can’t be accessed directly like it can if you initialized a variable yourself.

eg if u did

var myClip:MovieClip = new MovieClip();

u could then do

myClip.play();

But, if u did

createEmptyMovieClip(“myClip”, getNextHighestDepth());

Then you could not do myClip.play() because the name was given as a parameter and not initialised as a variable. To access that clip, you must then do:

_root[“myClip”].play();

In flash 8 this is less of a problem because the functions for dynamic loading return the loaded object. So you could do:

var myClip:MovieClip = createEmptyMovieClip(“myClip”, getNextHighestDepth());
myClip.play();

But, if you create clips in a loop, and want to access them later, then you would do it the _root[clipname] way. This works because the items you have in a movieclip can be accessed as pointers in that clip (thats the simplest way i can think of putting it). So if u had a clip called menu and dynamically created 10 items called item1, item2 etc inside it, u could access the item within it by using menu[“item” + number] where number is a given integer between 1 and 10. In the case of your code that’s what’s done, that way you can go through the loop and run the code on each item in the menu.

Hope that makes sense.

Yup makes perfect sense, thanks!

Now I’ve got it to work but it gives me some values “undefined” in the beginning - while loading the variables… I made it so that from the beginning it would add stuff to a textbox, however I want to omit the undefined values and I made an if statement, if a value was undefined (or null) not to add it to the textbox… when I do that, nothing adds anymore :confused:

 Withing the Article putout :: 

 Article.prototype.putOut = function(titletxt, datetxt, authortxt, bodytxt){
if (this.title != undefined) {
  txt = bla bla bla ... 
}
};

Any ideas?

B U M P
o p y o
o s
s t
t

That means all values are undefined - so this.title never exists. Have you initialized the title variable outside any functions? In your original code, in the constructor, I see this.title being used but i don’t see title initialized before that function (eg var title:String). If you use this.title in a function without initializing it before, then its scope is only within that function, and it’s gone again when that function ends.

Of course I may be wrong, if all your code isn’t shown. It could also be that the title is on a different level to the code you’re using. But without seeing the fla, it’s hard 2 say.

if u did

createEmptyMovieClip(“myClip”, getNextHighestDepth());

Then you could not do myClip.play() because the name was given as a parameter and not initialised as a variable. To access that clip, you must then do:

_root[“myClip”].play();

Array notation is only necissary if you have a dynamic instance name.

You could refer to thie clip with “_root.myClip.play();” without issue.

This is what you were looking for:

theNews = new XML();
theNews.ignoreWhite = true;
theNews.load('http://ionutz.biz/myspace/flash_news/xml.php');
 
function Article( id, title, date, author, body){
 this.id = id;
 this.title = title;
 this.date = date;
 this.author = author;
 this.body = body;
};
Article.prototype.putOut = function(titletxt, datetxt, authortxt, bodytxt){
 _level0.titletxt.text = this.title;
 _level0.datetxt.text = "On: "+this.date;
 _level0.authortxt.text = "By: "+this.author;
 _level0.bodytxt.text = this.body;
};
function articleOut(xml){
 _global.numArts = xml.firstChild.childNodes.length-1;
 info = xml.firstChild.childNodes[at].attributes;
 body = xml.firstChild.childNodes[at].firstChild.firstChild.nodeValue;
 currArticle = new Article(info.id, info.title, info.date, info.author, body);
 currArticle.putOut("titletxt","datetxt","authortxt","bodytxt");
 delete currArticle;
 delete info;
}
trace(_root._width);
theNews.onLoad = function(){
 articleOut(this);
 at = numArts;
 articleOut(this);
 _root.createEmptyMovieClip("menu", 1000);
 items = this.firstChild.childNodes;
 to = Number(items.length-1);
 for(a = 0; a>= -to; a--){
   var theitem:MovieClip = _root.menu.attachMovie( "menuItem", "item"+a, a);
  theitem.nametxt.text = items[to-(-a)].attributes.title;
  theitem._y = -20*a;
  theitem._y += 10;
  theitem._x = 400-63.5;
  theitem.itemConst = Number(items[0].attributes.id)-1;
  theitem.itemNum = to-(-a);
  theitem.onRelease = function(){
   at = this.itemNum;
   _root.articleOut(theNews);
  }
 }
}
articleOut(theNews);

Associative array referencing is another way to reference objects. Just know that: this[“property”] is exactly the same as *this.property; *- any object can be reference in terms of an array.Note that with array referencing you can concatenate data to the string before it is resolved to the object reference (which is what I think Defective was talking about).

Thanks guys this helps a lot!!!