dimanche 27 décembre 2015

Creating boundaries for scrollRect for platformer game as3

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;
    }

Aucun commentaire:

Enregistrer un commentaire