Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top