0

Writing a side-scrolling platformer and figured that scrollRect would be best for memory, since it only renders the rectangle and not the entire stage. I have it centering my hero and scrolling with him. My problem is with creating the outer boundaries for the rectangle. if(view.x < 0) is easy. I have the x and y origins locked, it's the other edges where I have problems.It runs on ENTER_FRAME, btw. Here's my code:

    public function ScrollWithHero()
    {
        var stageW2:Number = stage.stageWidth/2;
        var stageH2:Number = stage.stageHeight/2;
        var view:Rectangle = new Rectangle(0,0,stage.stageWidth,stage.stageHeight);

        if(hero.x - stageW2 > 0){ view.x = hero.x - stageW2; }

        if(hero.x + stageW2 > levelWidth){ // stop scrolling the rect }

        if(hero.y - stageH2 > 0){ view.y = hero.y - stageH2; }

        if(hero.y + stageH2 < levelHeight){ // stop scrolling the rect }

        scrollRect = view;
    }
PHaZerous
  • 59
  • 9

1 Answers1

0
    public function ScrollWithHero()
    {
        var stageW2:Number = stage.stageWidth/2;
        var stageH2:Number = stage.stageHeight/2;
        var view:Rectangle = new Rectangle(0,0,stage.stageWidth,stage.stageHeight);

        if(hero.x - stageW2 > 0)
        { 
            view.x = hero.x - stageW2; 

            if(hero.x + stageW2 > levelWidth)
            {
                view.x = levelWidth - stage.stageWidth;
            }
        }
        if(hero.y - stageH2 > 0)
        { 
            view.y = hero.y - stageH2; 

            if(hero.y + stageH2 > levelHeight)
            {
                view.y = levelHeight - stage.stageHeight;
            }
        }
        scrollRect = view;
    }
PHaZerous
  • 59
  • 9