simple createEmptyMovieClip/addMovieClip type AS2 thing
(treat me like a retard on this one)
Summary: Trying to draw a course list (complete course catalogue from xml). No problem. It takes the user's job code and draws a complete list of courses they need to take.
My interface allows the user to select other people's job codes and get a new list, so after a NEW selection is made and I recall this script, it removes all the old MCs from the course list MC but it does NOT add the new courses.
Why the F can't AS3 remove all clips and then draw new ones in? I thought this bitch was FAST!
function clearResults():void {
var j:uint;
var cn:uint;
cn = this.numChildren;
for (j=0; j<=cn; j++) {
this.removeChildAt(0);
}
}
function displayResults(xml:XML):void {
clearResults();
var i:uint;
var xPos:uint = 30;
var yPos:uint = 24;
var ySpacing:uint = 20;
for each (var prop:XML in xml.Module) {
var mc_course:courseMC = new courseMC();
addChild(mc_course);
mc_course.x = xPos;
mc_course.y = yPos;
yPos = yPos + ySpacing;
mc_course.modNum.text = xml.Module[i].ModuleCourseID;
mc_course.modTitle.text = xml.Module[i].ModuleName;
mc_course.date_req.text = xml.Module[i].RequiredDate;
mc_course.date_com.text = xml.Module[i].CompletedDate;
mc_course.TestLink = xml.Module[i].TestURL;
mc_course.CourseLink = xml.Module[i].SelfStudyUrl;
mc_course.myXML = xml.Module[i];
i++;
}
msg.text = this.name;
}
clearResults is where my problem lies. With the code removed, it just keeps drawing children on top of each other as they select new job codes. But I want to remove all children from the DisplayObject THEN draw new ones in.
What am I doing wrong?
I might be compleatly off but keep a store of mc's dont try to recreate, re-purpose.. ie visible = false; active = false;
Two reasons as3 is slow at creating and garbage issues if you never delete you never need to worry.
wurd
I just get sick of the compiler confused between MovieClips on stage (because I actually draw) and the MovieClips referenced by code, so I half ass it to make it work.
I think I better start again.
thanks jlm
your clearResults() method will most likely crash, due to the way you're using numChildren, which, as you're calling it first, would crash before executing your redraw.
try replacing your clearResults() method with this, and see how it goes.
function clearResults():void {
var j:uint;
var cn:uint;
cn = **this.numChildren - 1**;
for (j=0; j < cn; j++) {
this.removeChildAt(0);
}
}
Thanks geniuses.
It should only crash IF there were no children, but there always are and always will be courses in the list. And with all the new ERRORS in the compiler, I'm surprised there's nothing built to tell me that will crash.
ok, first of all I'm limited in I can't use the internal debugger because it's all .NET data, so I get handcuffed from my normal procedures. I'm blind most of the time except whatever Fiddler can show me.
So........ explain to me, high level, proper process.
main movie [Main.as]
2 main functions - load user's specific courses and load courses by job
mc job drop down - let's user choose from his job codes or other department jobs...calls main functions after selection.
mc Vertical Scroller - loads the course list mc inside - and handles scrolling buttons.
- - - inside mc VertScroller - CourseList MC - has one function displayResults, gets fed XML and makes a list in repeating loop. Called from Main.as functions.
How should I be "developmentally designing" this? I'm a visual designer.
Should the functions all live INSIDE the CourseList? Is there limitations in AS3 on who can target whom? Should there be a separate Events.as file to handle all functions from anywhere?
Right now the old AS2 "MCs are open and honest and tell each other what to do" is not working best for me in AS3. It works, but I start having too many "patches" to make the specific things work.
sounds like you need to set up a controller for your views. the views would dispatch the relevant events to the controller, and that update would in turn update any of the views if necessary.
if that makes sense.
yeah...see......sounds great. yeah yeah MVC...yeah yeah yeah.....
I can rearrange and write scripts anywhere, anytime, anyway....BUT I always at some point run into compile after compile errors because of conflicts with MC's on stage versus in a class, so no matter where my code lives (in a main class, in a controller class, in arsis' ass) things break or act differently than expected in AS3 versus AS2. So, a controller (while technically the right thing) does not work with a movieclip on the stage three deep because I DO NOT understand nor have I seen it explained anywhere how any class can at any time reference any movie clip (even if it's 4 levels deep in another MovieClip) or any function within a MovieClip. I always run into a problem.
In this case, because I'm working with .NET services written for me I'm blind when Flash doesn't react properly. I'm going to post the whole damn project and let you guys say "storm, you dumbass, do THIS" and then I'll do it because if I follow strict MVC stuff after I read and read and read (some being strict mvc some being based upon mvc in theory) it works WORSE than my half-assed class vs. MovieClip timelines vs. procedural hacks and that's wrong. The MVC format should work the easiest but that damned compiler hates anything I draw on stage or animate on stage.
eg. I DO NOT understand why numChildren is not valid as lith said. I have to have it before otherwise it would clear all of my MCs after I drew them in. I simply want to remove any MovieClips drawn in a previous run of the function.
eg. JLM helping me with this:
var mc:MovieClip = courseScroller.courseList as MovieClip; mc.displayResults(xmlModule); is ridonkulous.
MovieClip(courseScroller.courseList).displayResults(xmlModule); should work just fine but it doesn't. That's what I don't understand about AS3. We took a very open and free communication system and closed and regulated it.
Philosophically alone I have a lot of arguments against that, but I digress.
I'll post more about this another day. I don't at this point understand your language so I'm missing what you're suggesting because I don't have a full understanding of the technical process to apply it.
[edit] I added thishttp://www.storm-studios.com/olu.pdf just to clarify some understanding of my approach. Code is in .AS in Main and CourseList.as but the rest is timeline in each MCs.
Does your class extend MovieClip or Sprite? Are you setting the view class with a reference to the clip on stage?
You should be able to reference nested clips no problem. Assuming you have imported MovieClip into your class and have a proper reference to the clip that is part of your view class.
Alternately the clip on stage can link to the class directly.
Yeah as2 and as3 are complete dif ballparks, i have been coding in as3 exclusively for a year and a half, as2 is some clunky shit. 
If you are porting an as2 class, it is almost always better to rewrite it from scratch leveraging As3's superior event system and displayObject/list than try to replicate the old way of doing it.
It is totally possible i do not understand your question, so sorry if that is the case.
cool man 
If your fighting with AS3 maybe its early to worry about patterns, just get it done and move on.
Really try to build in a testable way. So build a bit test build test etc... and make sure it works at each stage as you expect, almost design chunks to be easy to check.
If your loading movies, application domains are a bit strange with timelines, there are timing and access issues and set up can be tricky, do some simple tests to prove it work as you are assuming (because it probably doesn't).
One approach I like is to load movies with linkage assets into a shared domain but have them based on sprite but not add any code class, then use a composition structure to wrap them so that all code is in the main movie. So load data and graphics but keep code only in the main swf unless the loaded do not interact with the holder. But this is only one approach and prob not standard.
One issue with AS3 is that Sprites are not so tightly bound to the timeline, keep shortcut references to ones you need as class variables. Remember that flash is never really sure if root is a movie or sprite so store it as a var of the appropriate type, and remember sometimes with parents flash is not sure they are Movies/Sprites so use 'as' to help define shortcuts before using them.
Thats my take from what you have said, but I have to re rem my as3 on mon as been messing with haxe for awhile, and apparently its as3 integrating with dot net web services so wish me luck!
yeah, JLM is absolutely right about making simple tests.
Just create a few classes with buttons that dispatchEvents, and have your controller manage them, that way you can get a quick and dirty on the little bits. Little unit test etc.
or just rock it out in as2 and then read an as3 book in the down time.
Thanks guys.
But all in all. I know MVC patterns just fine and even how to set them up "properly" but the problem in this case is limited to (as lith was trying to focus in on):
how do I remove the children?
Everything is a MovieClip at this point because I use the IDE, so everything has to be a MC to use the timeline. Sprite is not an option. I'm still a designer. SO, ASSUME MovieClip for everything.
I draw in children, try to remove them next time I need a new list. It works and the data comes. There's no "controller,view, or model" problems. But I don't understand AS3 well enough to troubleshoot why it draws, removes (but doesn't redraw) but the third time through it DOES draw (because there was nothing to remove).
Hey Storm,
I hear your pain there.
You say you can't use the local debugger cos you're using .NET - but I guess by that you mean you're getting some XML data fed from some .NET scripts? In this case, to test it locally I'd create some plain text XML files with some correct test data in them - just as the .NET would produce, but hand written - and test it using that as input.
Secondly, if you have to test online, which you inevitably will do anyway, can you not use Firefox and Flash Tracer to dump out the trace() comments?
And the final option which I used myself before I heard about Flash Tracer, is to make a debugger yourself - basically just a text field somewhere that you can write out test comments too. It's a bit more hard work setting that up of course, and in my experience not needed anymore thanks to Flash Tracer.
Anyway, I had a look at your code, I dunno if you've tried Lith's fix, but if not:
Originally posted by: Storm function clearResults():void {
var j:uint;
var cn:uint;
cn = this.numChildren;
// *******************************
// This is wrong cos the for loop runs once more than it needs to. If there
// are no children, it runs once. If there is only one child, it runs twice.
// It should be "j < cn".
// *******************************
for (j=0; j<=cn; j++) {
this.removeChildAt(0);
}
}
function displayResults(xml:XML):void {
clearResults();
var i:uint;
var xPos:uint = 30;
var yPos:uint = 24;
var ySpacing:uint = 20;
// *******************************
// What's weird here is that you use a for each loop
// but you don't use that 'prop' variable.
// Instead of using "xml.Module[i]" as you do, just use "prop".
// Also, you never initialise i to 0, although I don't think that's
// what's causing your troubles here.
// *******************************
for each (var prop:XML in xml.Module) {
var mc_course:courseMC = new courseMC();
addChild(mc_course);
mc_course.x = xPos;
mc_course.y = yPos;
yPos = yPos + ySpacing;
mc_course.modNum.text = xml.Module[i].ModuleCourseID;
mc_course.modTitle.text = xml.Module[i].ModuleName;
mc_course.date_req.text = xml.Module[i].RequiredDate;
mc_course.date_com.text = xml.Module[i].CompletedDate;
mc_course.TestLink = xml.Module[i].TestURL;
mc_course.CourseLink = xml.Module[i].SelfStudyUrl;
mc_course.myXML = xml.Module[i];
i++;
}
msg.text = this.name;
}
If this is going to drive you insane, one dirty hack you could use would be to create a Sprite to hold the children - then just delete that Sprite instead of deleting all the children.
But are you sure that XML data coming into the function is correct every time? That's perhaps most likely to be the source of your problem.
ah sorry storm, was not meaning to sound snarky, i misunderstood what you wanted.
apologies
peep what DontBogartMe posted.
j<=cn
ok, finally proof that I am an idiot. That I can handle. But technically, why would it break? It just wouldn't remove anything that didn't exist. But fair enough, now I fully understand lith's original suggestion (although it didn't fix the problem first go). I do need to go back as I think I've buggered lots since trying to "fix" this.
pyro, still luv ya....in the manly way. No worries. I'm not sensitive when it come to my work. In fact, I crave the day I never do it again even though I like what I do (if that makes any sense...time to move on).
DBM, thank you. Yes I am weird and not sure why I used for each then switched. I just get more comfortable iterating through numbers. It's physically easier for me to "see" as a concept.
Lack of "sight" though is my problem. When developing I used the XML data locally and built this List app. Then I connected it to the .NET and everything still worked. Where I ran into trouble was.....making a new list from the same MCs on stage calling another function for a NEW .NET list with a different job code as the parameter. The functions work as Fiddler returns the data everytime. AND I've built my own debugger for all my projects so I have text boxes I can "spit" data in to. it all appears. Honda is an IE ONLY HOUSE, so I cannot even bypass security with Firefox to test. ARGH!! It bounces me out of any hard link, so Flashtracer is NOT an option hence why I feel blind. No debugger (although the new one sucks for me comparatively) and no fun OS tools.
Had considered the Sprite container thing and that may be the best suggestion for this.
But here's a question..... ...in the removal process, can:
this.removeChildAt(0);
be
removeChild(mc_course);
I'm not fully understanding "instance names" in AS3. How can all instances be named the same? Hell if I name 2 MCs the same the compiler slaps my ass and calls me Judy. (I'm asking this as a Best practice kind of thing)
Alright, going to jump back into it and see where I get. It's just the removal that's got me nastier than a drugged up Britney Spears.
well I reckon your problem is not in the code you've posted, but in the code that calls it - I reckon there's some problem with how you're parsing the XML - if the XML is coming in right as you say it is.
You need to be able to trace out the value of the parameter 'xml' that is passed into your displayResults method, to make sure it is what you think it should be.
Originally posted by: Storm I'm not fully understanding "instance names" in AS3. How can all instances be named the same? Hell if I name 2 MCs the same the compiler slaps my ass and calls me Judy. (I'm asking this as a Best practice kind of thing)
mc_course is not an instance name here - it's a variable that contains that MovieClip. You could get back the instance name like this:
trace("mc_course's instance name is... " + mc_course.name);
this is where I get confused, why can't I specifically name instances anymore in one line where I build them? Defend AS3 all you want as a programmer, but as a designer.......PFFFFFT :P lol 
Thank y'all for being cool with my designer ass (minus Mr SnarkyPants). BUT I'm sure this thread could be logged in history of proof how designers can't communicate effectively to programmers and programmers can't effectively communicate to designers.
I'll throw things back how they were when it worked best and try all the fixes. I'm lost between what I wrote new external AS files for and which I left in the FLA. My experiments made it worse.
[edit] While I'm at it I'll explain more.....the Main class file has two functions, load based upon the user's job code (eg. Sales Consultant) or allows the user to see what courses other department positions would need (such as a Sales Manager) if they wanted to consider trying to be certified as a Manager, so its based upon a jobCodeNum.
you've got to break your code up into testable chunks and unit test it I think.
write a simple bit of code to call your displayResults method with 3 different sets of XML that you hardcode in there - see if it works like that.
If that works ok, then that means your XML isn't coming in right - which is what I've got my money on being the problem. So you break that code out and dump the XML it's supposed to be sending to displayResults to a text field instead, and you can then see if it looks ok.
That said I'm lost in a sea of tangled up fucked up classes myself right now and cannot get my head around the damn mouseEnabled... why my buttons aren't clickable anymore :( - not asking for help there - just saying I can't take my own advice 
This is part of my point. Adobe (with "IMPROVEMENTS" to ActionScript) have rendered the majority of our gained knowledge through experience useless and put us on even par with programmers from other languages.
Philosophically, we took an open communication oriented program and made it more "programmery" which if you ask any non-computer science grad has inherent problems. Things can't really talk to each other; things are inflexible; EventHandlers were written as a process to allow programs to work "more like Flash did" because buttons could already do things in Flash.
Anyway, quickly tested all the functions from the Main.as file and THEY ALL WORK and they all return the full XML objects from .NET into my mc_information.debugger.text field. No problem there.
Will update again as I get farther.
damn MouseEnabled. I ran into some bad mouse Flash prgramming as a user on NBC.com watching Heroes last week. The Sprint ad hid the mouse for an interactive, but the timing script launched back to the program before the ad was finished, so I never could get the mouse back visually until completely closing the browser. BAD BAD BAD
[edit] I was REALLY OWLY and miserable this morning. The cure? Animal Crackers. Breakfast of champions!
[edit edit] Everything works again. It get's the user's job code (Sales Consultant) is 24. It uses the second function to go get the list on 24. Returns it. Then I go select Fleet Consultant or Secondary Manager (who knows what the hell he does!?!) and his list shows up BUT it draws the new MCs over top of the old list!! THIS is what I need to fix. Get rid of the old to draw in the new. ARGH!!
ok so post the state of the code now that you've tested the XML and let's have another looksie?
and yeah - mouseEnabled is DOING MY FARKING BOX IN right now.
SUCCESS you fucking geniuses!! Butt slaps for all! Two for Mr SnarkyPants!!
changed lith's suggestion to DBM's and it fucking worked as stated.
Erdinger's for all.
[edit] keep in mind I'm a designer and there's lots commented right now
Main.as
package as3 {
import flash.display.SimpleButton;
import flash.text.TextField;
import flash.display.MovieClip;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.MouseEvent;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.external.ExternalInterface;
public class Main extends MovieClip
{
var _homeLink:String;
var xmlSource:String;
var xmlModule:XML;
var jobCodeNum:Number;
var jobCodeName:String;
public function Main()
{
alignCentre();
MovieClip(alljcList).visible = false;
loadCourseByUser();
//stop();
// Home Button functionality
_homeLink = LoaderInfo( this.root.loaderInfo ).parameters.HomeURL;
b_home.addEventListener(MouseEvent.CLICK, homeHandler);
// Previous (Back) Button functionality
}
public function alignCentre() {
stage.scaleMode = StageScaleMode.EXACT_FIT;
stage.align = StageAlign.TOP;
}
public function homeHandler():void
{
var targetURL:URLRequest = new URLRequest(_homeLink);
navigateToURL(targetURL);
}
public function jobCodesHandler():void
{
MovieClip(alljcList).visible = true;
}
public function loadCourseByUser():void
{
// load user's job codes and send to Job Code drop down menu
xmlSource = ExternalInterface.call("getJobCodeForUser");
xmlModule = new XML(xmlSource);
jobCodeName = xmlModule.JobCode.JobDesc;
jobCodeNum = xmlModule.JobCode.JobCodeId;
//pageTitle.text = jobCodeName.toUpperCase() + " COURSES";
//MovieClip(courseScroller).jobCode = jobCodeNum;
//MovieClip(mc_jobcode).mainJob.text = jobCodeName;
loadCourseByJobCode(jobCodeNum, jobCodeName)
//var mc:MovieClip = courseScroller.courseList as MovieClip;
//mc.displayResults(xmlModule);
}
public function loadCourseByJobCode(jobCodeNum, jobCodeName):void
{
xmlSource = ExternalInterface.call("combineCatalogXML", jobCodeNum);
xmlModule = new XML(xmlSource);
pageTitle.text = jobCodeName + " COURSES";
MovieClip(mc_jobcode).mainJob.text = jobCodeName;
MovieClip(courseScroller).jobCode = jobCodeNum;
MovieClip(mc_information).info_desc.text = xmlModule.toString();
//MovieClip(courseScroller).displayResults(xmlModule);
var mc:MovieClip = courseScroller.courseList as MovieClip;
mc.clearResults();
mc.displayResults(xmlModule);
}
}
}
CourseListMC (in the timeline)
function clearResults():void {
var j:uint;
var cn:uint;
cn = this.numChildren;
for (j=0; j<cn; j++) {
this.removeChildAt(0);
}
}
function displayResults(xml:XML):void {
var i:uint;
var xPos:uint = 30;
var yPos:uint = 24;
var ySpacing:uint = 20;
for each (var prop:XML in xml.Module) {
var mc_course:courseMC = new courseMC();
addChild(mc_course);
mc_course.x = xPos;
mc_course.y = yPos;
yPos = yPos + ySpacing;
mc_course.modNum.text = xml.Module[i].ModuleCourseID;
mc_course.modTitle.text = xml.Module[i].ModuleName;
mc_course.date_req.text = xml.Module[i].RequiredDate;
mc_course.date_com.text = xml.Module[i].CompletedDate;
mc_course.TestLink = xml.Module[i].TestURL;
mc_course.CourseLink = xml.Module[i].SelfStudyUrl;
mc_course.myXML = xml.Module[i];
i++;
}
}
I did change it up a bit from the original, and have to change some things back but it removes and adds children fine now.
congrats 
clearly it makes no diff, but really, you should write: var i:uint = 0;
and just to confirm what was breaking it in the first place, does it still work if you use "j<=cn" instead of "j<cn"?
ummmmmmmmm..........I don't dare touch it now!!
[edit] yes
j<=cn breaks it. FUCK i hate programmering. 
Every component in this app is drawn by me, so getting all this functionality such as drop down lists, searchable text box components and getting them all communicating properly in AS3 rapes my ass.
hehe congrats for sorting it 
well done - it's nice to get something fixed - it's even nicer to know what the hell it was you did that actually fixed it
in other news I've fixed my troubles with mouseEnabled now.... and of course it turned out to be nothing to do with mouseEnabled in the end... but cos I'd been burned by it before I just couldn't imagine it was anything else for ages and had the blinkers firmly on.