A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: addChild preserving unwanted directional changes

Hybrid View

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    16

    addChild preserving unwanted directional changes

    I have a player object, with an attached gun object. When I click, the gun fires, creating a projectile at the tip of the gun (known as the barrel in my code). The projectile is then added as a child. Now, my player character is coded to rotate to face the mouse cursor, which is all fine and dandy, but as I rotate, the projectiles I have previously fired rotate with me. Obviously I do not want this behavior. I suspect it has something to do with my poor understanding of the addChild function, and I could really use some clarification. I'm not entirely clueless, but my previous OOP experience has been in 3D engines, and as a result, I'm used to a very different way of thinking. I'm hoping someone here can give me a push in the right direction.
    Code:
    package
    {
        import flash.display.MovieClip;
        
        public class Weapon extends MovieClip
        {
    		public var projectile : Projectile;	
    		
            public function Weapon()
            {
    			
            }
            public function fire():void
            {
    		var P:Projectile;
    	
    		P = new Projectile;
    		P.x = barrel.x;
    		P.y = barrel.y;
    		addChild(P);
            }
        }
    }

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    A display list is just a series of containers. These containers can hold assets or other containers. All together they create a hierarchy of parents, children, siblings (as well as grandparents, grandchildren, ancestors etc.) Think of it like any operating systems folder structure. When you drag a folder to a new location, all of its contents go with it.

    Your weapon is a container and calls to addChild within that container add children assets to Weapon. Where weapon goes, so must its children. While that might make conceptual sense, we have to think of our games as overlapping systems. There's the background graphics system, the player, the enemies, projectiles, visual effects, UI and so on.

    While all these systems might have their respective logic, we need an overall manager that knows about (and usually creates) each system and how they work. That manager should listen for the mouse click, locate the tip of the gun, create a new bullet instance at that coordinate and then on tell that bullet to update itself at a regular interval (game tick) usually done with enter_frame events.

    There are global methods call localToGlobal() and globalToLocal() that will convert the x,y coordinates from local space to global or global space to local which will help place an instance at the exact same spot as another asset without having to be a child or sibling of it.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    16
    Can you give me a brief rundown of the difference between global and local space? As a 3D programmer I've never even heard of this concept,

  4. #4
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    It means the same thing basically as it does in 3d space. If you place movieclip on the stage and set its x and y each to 50 and trace its coordinates:

    trace(myParentMC.x, myParentMC.y); // you'll get 50, 50 in the output

    If you then add a clip insie of myParentMC and set its x and y to 25 you'll get either 25, 25 or 75, 75 depending on what you care about.

    The reason is, relative to its parent, myChildMC is only 25 pixels away from its origin, but in terms of the overall stages upper left corner, its 75 pixels away.

    So if you wanted to get the coordinates of an object in relation to the stage, you first have to create a point object with its coordinates:
    var myLocalPoint = new Point(myChildMC.x, myChildMC.y);

    Then you pass this point to the localToGlobal method
    trace(myLocalPoint); // 25, 25
    trace(myChildMC.localToGlobal(myLocalPoint); // 75, 75

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    16
    Ah, so it is like relative and absolute positioning. That's what I thought. Thanks for the reply, I'll mess with my code till I get something functional.

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