Screen Scrolling not working for Right and Bottom edges AS3

245 views Asked by At

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

1

There are 1 answers

15
user1875642 On BEST ANSWER

**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.