dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Nothing is being added to the display list...

  1. #1
    Member
    Join Date
    Apr 2009
    Posts
    58

    Nothing is being added to the display list...

    So I've got MovieClips on the stage, both manually added, and added with code. I used this code to trace them all:

    for(var i:int=0 ; i<stage.numChildren; i++) {
    var myObject = stage.getChildAt(i);
    trace(i);
    trace(myObject);
    }

    All it returns is 0 and [object MainTimeline]. Is there something wrong with Flash, or did I do my coding wrong? The objects I put on the stage should be on the Display List, right?

    I'm using Flash CS4.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You've confused the stage with the root. The stage typically has a single child, which is the root. The root is an instance of your document class, which is why it has the MainTimeline type. Your code should be:
    Code:
    for(var i:int=0 ; i<root.numChildren; i++) {
      var myObject:DisplayObject = root.getChildAt(i);
      trace(i);
      trace(myObject);
    }
    Or, if this code is in your main timeline, you can omit the "root." part, since that's already the implied context

    Code:
    for(var i:int=0 ; i<numChildren; i++) {
      var myObject:DisplayObject = getChildAt(i);
      trace(i);
      trace(myObject);
    }

  3. #3
    Member
    Join Date
    Apr 2009
    Posts
    58
    Thanks, that solves my original problem! But, now, I tried to apply this to a class, like so:

    Code:
    package johnEngine.player {
    	import flash.display.Sprite;
    	import flash.display.DisplayObjectContainer;
    	import flash.events.Event;
    	public class Dude extends Sprite {
    		public function Dude() {
    			for(var i:int = 0; i < Sprite(root).numChildren; i++) {
    				trace(i);
    				var temp = Sprite(root).getChildAt(i);
    				trace(temp);
    			}
    		}
    	}
    }
    The problem is when it traces temp. Any instance of the Dude class that is on the stage will return [object Dude], but everything else returns "null." Is there a way I can get it to trace other objects in the display list using the code in a class?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You cannot refer to the root property within the constructor like that. Before your instance is on the displaylist, its root and stage properties will be null. This shouldn't trace null as you've said, but rather result in an 1009 error trying to access the numChildren property of null.

    What are you trying to do? There's probably a better way.

  5. #5
    Member
    Join Date
    Apr 2009
    Posts
    58
    I'm trying to do is to get every object of one class to react the same way to every object of another class. Or maybe I shouldn't be using classes for this, I don't know. I'm making a side-scroller game, and I want the enemies to be able to jump from platform to platform. I don't want to go through the process of painstakingly adding code to each and every enemy, so I'm looking for a way to make all the enemies react to all the platforms in the same way.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The usual way to accomplish this is to have some controller class (which could be the document class) have collections of both the enemies and the platforms and iterate over them doing collision-testing or whatever.

    So, each time you create a new enemy, add it to the enemies array. Same thing with the platforms.
    Code:
    function addEnemy():void{ //you can always pass positioning or other initialization info to this function as parameters.
      var nme:Enemy = new Enemy();
      enemies.push(nme);
      addChild(nme);
    }
    
    function addPlatform():void{
      var platform:Platform = new Platform();
      platforms.push(platform);
      addChild(platform);
    }
    And in some game loop, or function run by the game loop, you test all the enemies against all the platforms
    Code:
    for (var i:int = 0; i < enemies.length; i++){
      for (var j:int = 0; j < platforms.length; j++){
        var enemy:Enemy = enemies[i];
        var platform:Platform = platforms[j];
        if (enemy.touches(platform)){ //made up collision test.
          enemy.doSomething();
        }
      }
    }
    There are also optimizations for quick rejection of enemies or platforms so you don't have to test every single possible interaction, but that's a different topic.

Tags for this Thread

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