0

Hi the following code works when scrolling to either the left or right edges of the screen in my game; however when scrolling to the right or bottom edge of the screen when the edge of the "map" has been reached I am able to see beyond the edge of the map i.e. i am seeing white space i.e. the colour of the stage. Whereas, when scrolling to the edge of the map's left or top edge I am not able to see beyond the edge of the map.

public function scroll_screen():void { //scrolling left, right, up, down

     var stagePositionX:Number = container.x+player.x;
     var rightEdge:Number = stage.stageWidth-edgeDistance;
     var leftEdge:Number = edgeDistance;

     var stagePositionY:Number = container.y+player.y;
     var bottomEdge:Number = stage.stageHeight-edgeDistance;
     var topEdge:Number = edgeDistance;

        //horizontal scrolling
        if (stagePositionX > rightEdge) {
            container.x -= (stagePositionX-rightEdge);
            if (container.x < -(container.width-stage.stageWidth)) container.x = -(container.width-stage.stageWidth);
        }
        if (stagePositionX < leftEdge) {
            container.x += (leftEdge-stagePositionX); 
            if (container.x > 0 )container.x = 0;

        }

        //vertical scrolling
        if (stagePositionY > bottomEdge) {
            container.y -= (stagePositionY-bottomEdge);
            if (container.y < -(container.height-stage.stageHeight)) container.y = -(container.height-stage.stageHeight);
        }
        if (stagePositionY < topEdge) {
            container.y += (topEdge-stagePositionY);
            if (container.y > 0) container.y = 0;
        }
    }

hope that makes sense, thanks

Larry
  • 33
  • 7

1 Answers1

0

**updated**

Your problem is that container.width is changed when your shapes fly away from initial rectangle.

If you create a var initialContainerWidth and save your container width there until you add any shapes to it and then use this saved variable instead of container.width, it will work fine. Or you can add your flying shapes directly to the stage so that they will not extend container's sizes.

You can just hardcode 1386 instead of container.width to check it.

user1875642
  • 1,303
  • 9
  • 18
  • hi unfortunately that hasn't solved the problem, it's still creating the same problem. The code related to the horizontal scrolling has been taken from a book. I'm not sure why its not working properly for my game. Any ideas? Is there a better way to achieve the scrolling effect? thanks – Larry Aug 04 '13 at 16:59
  • this is the code from the book `var stagePosition:Number = gamelevel.x+hero.mc.x; var rightEdge:Number = stage.stageWidth-edgeDistance; var leftEdge:Number = edgeDistance; if (stagePosition > rightEdge) { gamelevel.x -= (stagePosition-rightEdge); if (gamelevel.x < -(gamelevel.width-stage.stageWidth)) gamelevel.x = -(gamelevel.width-stage.stageWidth); } if (stagePosition < leftEdge) { gamelevel.x += (leftEdge-stagePosition); if (gamelevel.x > 0) gamelevel.x = 0; }` – Larry Aug 04 '13 at 17:00
  • Hm, now I see. The code seems to be correct(forget my changes). This `if (container.x < -(container.width-stage.stageWidth)) container.x = -(container.width-stage.stageWidth);` line prevents level from moving too left, so you shall not see any white space to the right. Are you sure that your level/stage are not scaled? Are you writing your app for air mobile? – user1875642 Aug 04 '13 at 18:39
  • There are a lot of movieclips inside the container which are being scaled/tranformed/rotated and i notice that removing these seems to have solved the problem. However, how can i solve this? This app is not for air mobile. Thanks – Larry Aug 05 '13 at 12:46
  • You just need to check if your container's right side is not to the left of the stage's right bound. If your container's width is simply scaled, you can divide it by `scaleX` to receive and use original value. Or you can use `localToGlobal` and `globalToLocal` functions to reliably transform coordinates inside your container to stage coordinates. – user1875642 Aug 05 '13 at 16:51
  • Ok thanks for that, having trouble trying to use localToGlobal and globalToLocal - have fiddled around with it for a while yet can't get it to work. Which part of my code do i need to change to incorporate this? – Larry Aug 07 '13 at 19:37
  • try to replace `container.width` with `(-container.localToGlobal(new Point(0,0)).x+container.localToGlobal(new Point(container.width,container.height)).x)`. – user1875642 Aug 07 '13 at 19:46
  • hmm I'm still getting the same problem - I can still scroll beyond the right edge of the container. Any other suggestions? thanks for your help so far – Larry Aug 07 '13 at 19:59
  • can you draw a border for the container? in that case you will see if the problem is with the container or with its contents – user1875642 Aug 07 '13 at 20:14
  • The size (width and height) of the container is constantly changing due to movieclips inside the container constantly changing position - is this the cause of the problem? The contents of the container determine the containers width and height; movieclips inside the container are frequently being deleted or created. – Larry Aug 07 '13 at 20:27
  • You can test it if you put `if (container.x < -(container.width-stage.stageWidth)) container.x = -(container.width-stage.stageWidth);` this code on enterFrame event and see what happens. However, if you can upload the whole project and post a link to it, it will be easier for me to see what's wrong – user1875642 Aug 08 '13 at 06:09
  • I've uploaded it to mediafire: http://www.mediafire.com/folder/3r0qg79yclkwa/Game2project ,hope its not too hard to follow.. thanks – Larry Aug 09 '13 at 12:10
  • Yes, your problem is that container.width is changed when your shapes fly away from initial rectangle. If you create a `var initialContainerWidth` and save your container width there until you add any shapes to it and then use this saved variable instead of `container.width`, it will work fine. Or you can add your flying shapes directly to the stage so that they will not extend container's sizes. – user1875642 Aug 09 '13 at 18:19
  • You can just hardcode `1386` instead of container.width to check it. – user1875642 Aug 09 '13 at 18:25
  • Ah great, thanks very much for your help. You're welcome to write up the comment as an answer and I'll award it as my accepted answer. – Larry Aug 09 '13 at 23:05