8 Replies Latest reply on Nov 19, 2010 1:47 PM by Kristi_cs5_flash

    Quick question regarding background scrolling

    Kristi_cs5_flash

      Hi,

       

      I have converted a large background image (much larger than the stage) into a Movie Clip.  I did this so I can make it scroll as the player traverses to the edge of the scene.  This appears to make the player free to roam the entire background image.  I have a tree symbol with a class attached to it that blocks player movement.  Here is my problem:

       

      When I drag and drop instances of the tree symbol onto the background, the trees are stationary and do not scroll with the background.  I do not want to have to implement code to make the trees scroll when the background does.  I was wondering if there was a way to simply attach the trees to the background so that if the background moves, the trees move with it and appear to be part of the background as if they were drawn into it.

       

      It is convenient for me to be able to drag and drop as many trees as i need to block certain routes on the background from the player.  I tried writing code to make the trees move but it was only working for the very last tree that I added to the scene.

       

      I appreciate any insight into this problem, thank you!

       

      Kristi

        • 1. Re: Quick question regarding background scrolling
          Rothrock Level 5

          Most likely it is possible.

           

          When asking code questions it is generally a good idea to share the parts of your code in question. Because there are most likely a very large number of ways to do anything.

           

          So share just the bit of your code that deals with making the back ground move.

           

          And share the bit of code you are using to drag and drop the trees.

          • 2. Re: Quick question regarding background scrolling
            Kristi_cs5_flash Level 1

            There is no code to drag and drop a tree...this is done using the flash cs5 interface.  The code making the background move is irrelevant.  I simply wish to know if there is a way in cs5 to attach an instance of a symbol to a movieclip background so that it is permanently connected to it.  (so when the background moves so does the tree without adding code to make it move).

            • 3. Re: Quick question regarding background scrolling
              Kristi_cs5_flash Level 1

              here is my scrolling code:  (it takes place in the onenterframe so it is called every frame of animation)

               

                function onEnterFrame(event:Event):void
                {
                 var playerHalfWidth:uint = player.width/2;
                 var playerHalfHeight:uint = player.height/2;
                 var backgroundHalfWidth:uint = bground.width/2;
                 var backgroundHalfHeight:uint = bground.height/2;
                
                 player.x += vx;
                 player.y += vy;
                
                 if(player.x - playerHalfWidth < leftInnerBoundary)
                 {
                  player.x = leftInnerBoundary + playerHalfWidth;
                  rightInnerBoundary = (stage.stageWidth/2)+(stage.stageWidth/4);
                  bground.x -= vx;
                 }

                 else if(player.x + playerHalfWidth > rightInnerBoundary)
                 {
                  player.x = rightInnerBoundary - playerHalfWidth;
                  leftInnerBoundary = (stage.stageWidth/2)-(stage.stageWidth/4);
                  bground.x -= vx;
                 }

                 if(player.y - playerHalfHeight < topInnerBoundary)
                 {
                  player.y = topInnerBoundary + playerHalfHeight;
                  bottomInnerBoundary = (stage.stageHeight/2)+(stage.stageHeight/4);
                  bground.y -= vy;
                 }
                 else if(player.y + playerHalfHeight > bottomInnerBoundary)
                 {
                  player.y = bottomInnerBoundary - playerHalfHeight;
                  topInnerBoundary = (stage.stageHeight/2)-(stage.stageHeight/4);
                  bground.y -= vy;
                 }
                
                 // Stop left bground edge
                 if(bground.x + backgroundHalfWidth < stage.stageWidth)
                 {
                 
                  bground.x = stage.stageWidth - backgroundHalfWidth;
                  rightInnerBoundary = stage.stageWidth;
                 }
                 // Stop right background edge
                 else if(bground.x - backgroundHalfWidth > 0)
                 {
                  bground.x = 0 + backgroundHalfWidth;
                  leftInnerBoundary = 0;
                 }
                 // Stop top background edge
                 if(bground.y - backgroundHalfHeight > 0)
                 {
                  bground.y = 0 + backgroundHalfHeight;
                  topInnerBoundary = 0;
                 }
                 // Stop the bottom background edge
                 else if(bground.y + backgroundHalfHeight < stage.stageHeight)
                 {
                  bground.y = stage.stageHeight - backgroundHalfHeight;
                  bottomInnerBoundary = stage.stageHeight;
                 }
               
                }

              • 4. Re: Quick question regarding background scrolling
                Rothrock Level 5

                Ah. Sorry I missed that bit. So if you are placing the trees using the Flash authoring environment, you will need to make the trees children of the background movieclip. That way they will scroll together.

                 

                You can open the background movieclip in the library, give it a new layer and put the trees on that layer.

                 

                Otherwise you will need to make some kind of code that tracks the movement of the background and then applies the changes to the trees.

                1 person found this helpful
                • 5. Re: Quick question regarding background scrolling
                  Kristi_cs5_flash Level 1

                  When I add the tree I add them to their own layer.  I implemented code to make them scroll when the background scrolls, however only the first tree that I added actually scrolls.  The others seem to ignore the code.

                   

                  Adding them as a child to the background works but it effectively stops the collision detection code in the tree class that is attached to them for some reason.

                  • 6. Re: Quick question regarding background scrolling
                    Ned Murphy Adobe Community Professional & MVP

                    Can you show the code that only moves one tree?  It sounds like you might be trying to use the increment value of a loop directly to try to control the trees scrolling with code.

                    • 7. Re: Quick question regarding background scrolling
                      Rothrock Level 5

                      Without seeing your collision detection code I can't say why it would stop working, but I would guess that either the x/y values change or that since all the trees are the children of the same background clip that they are always/never hitting what they need to do. Code...

                       

                      And when I said layer I meant inside your background movieclip. Just to keep them separate from whatever artwork you already have in the background.

                       

                      If the trees aren't part of the background then they will each need to be moved around. Without seeing the code I can't tell why only the last one placed would move. My guess is that they all had the same instance name? Each will need its own instance name so that they can all be addressed individually.

                      • 8. Re: Quick question regarding background scrolling
                        Kristi_cs5_flash Level 1

                        1) Collison stopped because the new parent is the background when the tree was added as a child to it.

                         

                        2) Giving them all unique names is not feasible.  I am dragging and dropping hundreds of instances of the tree symbol.  I want them all to behave the same.

                         

                        Collision:

                         

                        Tree class:

                          private function onEnterFrame(event:Event):void
                          {
                           MovieClip(parent).checkCollisionWithPlayer(this);
                          }

                         

                        Manager class: (the parent)

                         

                        public function checkCollisionWithPlayer(wall:MovieClip)
                          {
                           if(player.parent != null)
                           {
                           
                            Collision.block(player, wall);
                           }
                          }

                         

                         

                        this makes all trees block the player, regardless how many i add to the scene.  ALso this way i don't have to name any of them at all.

                         

                         

                        Problem:  when using the wal:MovieClip reference to move the trees with the background, only the first tree added moves.  The rest do not.

                         

                        Scroll example:

                         

                        wall2:MovieClip;

                         

                        public function checkCollisionWithPlayer(wall:MovieClip)
                          {
                           if(player.parent != null)
                           {
                           
                            Collision.block(player, wall);

                           wall2 = wall;
                           }
                          }

                         

                         

                           if(player.x - playerHalfWidth < leftInnerBoundary)
                           {
                            player.x = leftInnerBoundary + playerHalfWidth;
                            rightInnerBoundary = (stage.stageWidth/2)+(stage.stageWidth/4);
                            bground.x -= vx;
                            wall2.x -= vx;
                           }

                         

                         

                        The above works fine, but only for the FIRST tree instance added.

                        I don't want to have to write code for EVERY tree.