A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [RESOLVED] Goodbye attachMovie, Hello stupidity

  1. #1
    Member
    Join Date
    Sep 2007
    Location
    Richmond, VA
    Posts
    45

    resolved [RESOLVED] Goodbye attachMovie, Hello stupidity

    I consider myself a logical person. AS3 is not logical. AS3 is a ridiculous nonsensical tromp through the looking glasses of a huge, angry nerd with self-esteem issues. Having said that, I realize that attachMovie is now dead. Take something that was once so simple, and improve it by making it unnecessarily complex and irrational so that flash newbies will never dare to set foot in our domain.

    addChild("class"); you say?
    Fascinating.

    Code:
    	// ASSIGN VALUES TO SONG BUTTONS
    	for (var a=0; a<=11; a++) {
    		if (a < songArray[currentCat].length-songPointer) {
    			root["txtsong"+a].text = songArray[currentCat][songPointer+a][0];
    			if (songArray[currentCat][songPointer+a][2] == true) {
    				root["NEWSIGN"+a]:new_mc = new new_mc();
    				addChild(root["NEWSIGN"+a]);
    				root["NEWSIGN"+a].x = root["btnsong"+a].x+100;
    				root["NEWSIGN"+a].y = root["btnsong"+a].y-20;
    			}
    		} else {
    			root["txtsong"+a].text = "";
    		}
    	}
    My code brings me errors.
    Namely, that I must use a simple identifier for:
    addChild(root["NEWSIGN"+a])

    I'd actually rather say:
    root["songbtn"+a].addChild(NEWSIGN)
    root["songbtn"+a].NEWSIGN.x = whatever;
    ..etc..

    but it tells me that that thing doesn't understand addChild.
    addChild only likes root.


    This is worse than congress.

  2. #2
    Member
    Join Date
    Sep 2007
    Location
    Richmond, VA
    Posts
    45
    How odd.

    It lets me do this:
    Code:
    			if (songArray[currentCat][songPointer+a][2] == true) {
    				var piddle:new_mc = new new_mc();
    				addChild(piddle);
    				piddle.x = root["btnsong"+a].x+100;
    				piddle.y = root["btnsong"+a].y-20;
    			}
    (Interesting fact: "piddle" is my British family's euphemism for both cat urine and very weak tea)

    And I can add as many "piddle"s as I like. They can cover the page.
    Obviously, these are not movie clips named piddle.

    So how can I control them? What the hell are they?

    More importantly, how do I get rid of them?
    piddle.removeChild(); // NOPE
    removeChild(piddle); // NOPE
    piddle.removeMovieClip(); // NOT
    Last edited by KindaGamey; 10-19-2007 at 03:04 PM.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Say it with me: "An instancename is not the same thing as a variable name". AS1 and AS2 made you use "paths" to access instances, AS3 allows you to separate logical objects from their appearance on the stage. This is immensely clarifying, once you get used to it.

    That first code does look unnecessarily complex, but I don't think that's AS3's fault. You have a 3 dimensional array of song somethings? Anyway this doesn't work:
    Code:
    root["NEWSIGN"+a]:new_mc = new new_mc();
    Using the array access syntax to create new instances is clumsy at best. Let me try to rewrite your first snippet without understanding the songarray stuff
    Code:
    	// ASSIGN VALUES TO SONG BUTTONS
            var t:TextField;
            var n:new_mc;
            var btnsongs:Array; //Array of btnsong - make class property.  should be populated, but I don't know what you've got in there.
            var texts:Array; //Array of all textfields on root - make class property.  same as above
            var newSigns:Array = new Array(); //Array of new_mcs - make class property
    	for (var a=0; a<=11; a++) {
    		if (a < songArray[currentCat].length-songPointer) {
                            t = texts[a];
    			t.text = songArray[currentCat][songPointer+a][0];
    			if (songArray[currentCat][songPointer+a][2] == true) {
                                    n = new new_mc();
                                    newSigns[a] = n; //or newSigns.push(n)
    				addChild(n);
    				n.x = btnsongs[a].x+100;
    				n.y = btnsongs[a].y-20;
    			}
    		} else {
    			t.text = "";
    		}
    	}
    You seem to have a bunch of things which are tied together through the array index. Have you considered making a class to handle them? For instance, it seems that a new_mc is associated with a text, something from the songArray, and a btnsong. You could make a class which holds all of these things and handles their interactions.

    The piddle example is enlightening. In your code, piddle is a variable of type new_mc. It is re-declared everytime through the loop, and the old reference is lost except for the fact that it's added as a child. That's fine if you never need to touch it again, but if you do, you'll need to keep a reference to it around (or grab it from the displayList). The only way to remove the piddles you've added above would be something like this:
    Code:
    var toremove:Array = new Array(); //of new_mc
    var p:DisplayObject;
    for (var i:int = 0; i < numChildren; i++){
      p = getChildAt(i);
      if (p is new_mc){
       toremove.push(p);
      }
    }
    for (var i:int = 0; i< toremove.length; i++){
      removeChild(toremove[i]);
    }
    toremove = null;
    If you'd kept an array, you wouldn't have to walk the displayList:
    Code:
    var piddles:Array = new Array();
    var piddle:new_mc; //declaring outside the loop is more efficient.
    ...
    			if (songArray[currentCat][songPointer+a][2] == true) {
    				piddle = new new_mc();
                                    piddles.push(piddle);
    				addChild(piddle);
    				piddle.x = root["btnsong"+a].x+100;
    				piddle.y = root["btnsong"+a].y-20;
    			}
    ....
    Now you have an array with all your piddles and nothing else, and you can manipulate them individually.
    Code:
    removeChild(piddles[0]);

  4. #4
    Member
    Join Date
    Sep 2007
    Location
    Richmond, VA
    Posts
    45
    Brain... trying... to... understand...

    Thank you so much for your response. Now I must absorb it in silence.

    "An instancename is the same thing as a variable name"
    "An instancename is not the variable name, but is actually the same thing"
    "A variable name is the same thing as an instancename"
    ... Agh! I can't!

    Oh, the reason I have crazy-arsed arrays is because I was parsing an XML document and found it easier to reference all the bits that way. I have multiple categories each with names and directories, and then songs within those categories with names, mp3s, and whether or not it is "NEW!". By just knowing the currentDirectory and what button the user has pushed I can pick out any bit of information I want with ease.

    What you've helped me with, I think, is putting a "NEW!" label on all the songs that are new -- and then removing said label when I list more songs or click a different category.

    I think.

  5. #5
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    An instance of a class is something that is there in memory,rigth there, over there look --->
    Its a phisical thing rigth there
    Now, to tell WHERE that thing is, inside the memory, you can have a variable that POINTS to it
    Also, you can have many variables with many diferent names pointing to the same class instance
    Instancename...sometimes name is just a property of the instance, like x, y, width, and may not be the same name as the name of the variable that points to that instance
    Does it help?

  6. #6
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    And instead of using root you can

    var rt:MovieClip = root as MovieClip;

    and use rt instead

  7. #7
    Member
    Join Date
    Sep 2007
    Location
    Richmond, VA
    Posts
    45
    Thanks again.
    My solution sculpted from your advice:

    Init @ Beginning
    Code:
    // Array of "new!" movieclips
    var newsigns:Array = new Array();
    var n:new_mc = new new_mc();
    Create MovieClips
    Code:
    // ASSIGN VALUES TO SONG BUTTONS
    	for (var a=0; a<=11; a++) {
    		if (a < songArray[currentCat].length-songPointer) {
    			root["txtsong"+a].text = songArray[currentCat][songPointer+a][0];
    			if (songArray[currentCat][songPointer+a][2] == true) {
    				n = new new_mc();
    				newsigns.push(n);
    				addChild(n);
    				n.x = root["btnsong"+a].x+100;
    				n.y = root["btnsong"+a].y-20;
    			}
    		} else {
    			root["txtsong"+a].text = "";
    		}
    	}
    Remove MovieClips
    Code:
    function removeNew():void {
    	// REMOVE "NEW!" SIGNS
    	for (var g=0; g<newsigns.length; g++) {
    		removeChild(newsigns[g]);
    	}
    	newsigns.splice(0);
    }

    Thank you very much.

  8. #8
    Member
    Join Date
    Sep 2007
    Location
    Richmond, VA
    Posts
    45
    Bonus Question:

    Now I'm trying to get "NEW!" to popup inside of my marquee movie clip too; to highlight any NEW categories instead of songs...

    Placing the clips inside of movie clips seems to be more difficult. Here's what I got so far:

    Code:
    // ASSIGN VALUES TO MARQUEE
    	for (var a=0; a<=5; a++) {
    		if (a < (categoryArray.length-catPointer)) {
    			movmarquee.marquee["martxt"+a].text = categoryArray[catPointer+a][0];
    			n = movmarquee.marquee.addChild(n);
    			newcatsigns.push(n);
    			n.x = movmarquee.marquee["martxt"+a].x+350;
    			n.y = movmarquee.marquee["martxt"+a].y-20;
    		} else {
    			movmarquee.marquee["martxt"+a].text = "";
    		}
    	}
    That adds the "NEW!" just fine.
    Now, I'm trying to remove them and this doesn't work:

    Code:
    function removeNewCat():void {
    	// REMOVE "NEW!" CATEGORY SIGNS
    	for (var g=0; g<newcatsigns.length; g++) {
    		movmarquee.marquee.removeChild(newcatsigns[g]);
    	}
    	newcatsigns.splice(0);
    }
    It hates this:
    movmarquee.marquee.removeChild(newcatsigns[g]);

    Error:
    Code:
    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    	at flash.display::DisplayObjectContainer/removeChild()
    	at jukebox_2_fla::MainTimeline/removeNewCat()
    	at jukebox_2_fla::MainTimeline/marqueeMore()

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    This is a subtle one, but I think I understand the problem. It looks like you're adding the same n object to the movmarquee multiple times. When you add a DisplayObject to a DisplayObjectContainer, it removes it from any other DisplayObjectContainer. That's just a fancy way of saying it can only be on the DisplayList once. However, every time you add it, you are pushing it into the newcatsigns array. It can go in that as many times as it wants to. So, after the first removal, it starts to fail because it's not there to remove again.

    In metaphor: I give you an apple. I give you the same apple. I give you the same apple. I take that apple back. I try to take it again, but you don't have it anymore, so I throw a temper tantrum.

  10. #10
    Member
    Join Date
    Sep 2007
    Location
    Richmond, VA
    Posts
    45
    Whoa!
    I think I did it..

    Code:
    	// ASSIGN VALUES TO MARQUEE
    	for (var a=0; a<=5; a++) {
    		if (a < (categoryArray.length-catPointer)) {
    			movmarquee.marquee["martxt"+a].text = categoryArray[catPointer+a][0];
    			if (categoryArray[catPointer+a][2] == true) {
    				n = new new_mc();
    				n = movmarquee.marquee.addChild(n);
    				newcatsigns.push(n);
    				n.x = movmarquee.marquee["martxt"+a].x+375;
    				n.y = movmarquee.marquee["martxt"+a].y-20;
    			}
    		} else {
    			movmarquee.marquee["martxt"+a].text = "";
    		}
    	}

    Code:
    function removeNewCat():void {
    	// REMOVE "NEW!" CATEGORY SIGNS
    	for (var g=0; g<newcatsigns.length; g++) {
    		movmarquee.marquee.removeChild(newcatsigns[g]);
    	}
    	newcatsigns.splice(0);
    }
    Thanks Flax. I realized what it was doing before reading your latest post.
    (And I will try not to take offense as I DO throw many tantrums when navigating AS3...)
    It just seemed strange to say:
    n = new new_mc
    And then turn around and assign n to be something else.

    In my old world notions, this would be like defining the same thing twice:
    n = 3; // Apples
    n = 4;
    Last edited by KindaGamey; 10-22-2007 at 04:50 PM.

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I was attempting to draw a parallel between the player throwing an error and a kid throwing a tantrum, not any reaction you may have had.

    And, yeah, it would be odd to overwrite n without using it, but you did, with the addChild. It's more like:
    n = 3;
    n = n+1;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center