A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: can't find objects on stage

  1. #1
    Member
    Join Date
    Feb 2003
    Location
    local habitation
    Posts
    73

    can't find objects on stage

    Hi,

    I'm studying the code for a vertical shooter game that is given in the book "ActionScript Design Patterns 3.0." The author is explaining the factory pattern and so covers just that aspect of the code -- doesn't cover hit testing. I would like to figure out how to do hit testing for this example because it's similar to something I am planning.

    The code for the game consists of 12 classes, abstract and concrete creator classes, abstract and concrete product classes and a Main class. These classes instantiate the ship and weapon objects at various points in their execution. I made a separate Collision class and I'm trying to find all the objects on the stage using this Collision class so I can hit test them with each other. Here's my code:

    Code:
    package {
    	
    	import flash.display.*;
    	import flash.events.*;
    	import ships.*;
    	
    	public class Collision extends Sprite {
    		
    		private var target:Stage;
    		private var heroShip:Sprite;
    		private var heroCannonBall:Sprite;
    		private var alienShip:Sprite;
    		private var alienMine:Sprite;
    		private var alienCannonBall:Sprite;
    		
    		public function Collision(stage:Stage){
    			target = stage;
    			addEventListener(Event.ENTER_FRAME, checkForObjects);
    			
    		}
    		private function checkForObjects(event:Event){
    			for (var j:int = 0; j < target.numChildren; j++){
    				var spr = target.getChildAt(j);
    				trace (spr);
    				if (spr is HeroShip){
    					trace(spr);
    				}
    			}
    		}
    	}
    }
    The problem is that when I trace for the variable spr, I get a nice list of objects names, like Main, HeroShip, AlienShip AlienCannonBall etc, but when I try to trace for one of those object names as per above,
    Code:
    if(spr is HeroShip)
    it doesn't work.
    I get the error message "Access of undefined property HeroShip." However, I am able to trace for Main and it works, but just with Main --
    Code:
    if(spr is Main)
    works.

    Is there a way to fix this code so I can access objects on the stage? Am I missing a cast somewhere? Or do I need a different approach to find objects on the stage in a multiclass game? Thanks.

    Zaffer
    Zaffer

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You probably need to import HeroShip, unless it is directly in the ships package (ships.HeroShip should work, ships.goodguys.HeroShip won't).

    This design isn't great though. Since ENTER_FRAME events don't bubble according to the docs, you will only get the targets on the actual stage (which is only the root). You could process the children recursively, but that's a lot of work to do each frame. Instead, you should keep track of which instances are on the displayList when you add and remove them, so that you don't have to re-calculate that each frame.

  3. #3
    Member
    Join Date
    Feb 2003
    Location
    local habitation
    Posts
    73
    Thanks 5TonsOfFlax. You can probably tell I'm new to working with classes. The biggest headache I find whenever I try to do something like this is trying to figure out how to reference things from one class to another. I know I could reference the objects by name if I put them on the stage manually and gave them instance names, but that doesn't work for objects created at runtime.

    How do I keep track of which instances are on the stage when I add or remove them? Should I be using getters and setters? Or if the answer to my question is too labor-intensive, could you point me to a tutorial or book on how to get around and reference objects from class to class when working with a lot of classes. Thanks.
    Zaffer

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Whenever you are dealing with a bunch of similar objects, you should ask yourself whether you want to process them together. If the answer is "yes" (and it usually is), you should keep a collection of those objects. An Array will work fine for this.

    So, I'm assuming that you have methods which determine when to put new ships on stage. Ideally, you would have an actual method which puts the ships on stage and handles any bookkeeping that's necessary. That method would put the ship in the array.

    Similarly, you probably have methods that determine when to remove a ship from the stage. Those should remove the ship from the array. Now, your collision algorithms only need access to the array, which should always be up to date.

    I probably would NOT make Collision a separate class. But I might make a GameWorld or Level class which would have the array of ships and handle the collision detection.

  5. #5
    Member
    Join Date
    Feb 2003
    Location
    local habitation
    Posts
    73
    Thanks, I'll give that a try.
    Zaffer

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