Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to do a pretty simple sidescroller in Action Script 3 with Flash CS6 and I am currently stuck at the stage boundaries and the level scrolling.

My hero should move freely in the middle of the stage and should be stopped if he enters a position greater that 75% or smaller than 25% of the stage with the level starting to scroll unless the level reaches one of the ends (then he should be able to walk exactly to the stage boundaries). The Level is a wide movieClip which should later contain blocks to jump on and tweened enemy's.

Currently I have a Main.as class that instantiates the Level class, in which I have my hero. There are a lot of mistakes in the boundary code I think, but I am so stuck at the moment that I totally need some help.

Here's a link to all Files with the fla: JumpGame

Thanks in advance for all advices :)

The following calculations are currently done in my hero object with setter and getters to access them:

private function keyDownHandler(e:KeyboardEvent):void
    {
        trace("Keycode" + e.keyCode);

        if (e.keyCode == Keyboard.LEFT)
        {
            _accX = -0.2;
        }

        else if (e.keyCode == Keyboard.RIGHT)
        {
            _accX = 0.2;
        }

        else if (e.keyCode == Keyboard.UP || e.keyCode == Keyboard.SPACE)
        {
            if(_isOnGround)
            {
                _vy += _jumpForce;
                _isOnGround = false;
            }

        }
    }

    private function keyUpHandler(e:KeyboardEvent):void
    {
        if (e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.RIGHT)
        {
            _accX = 0;
        }
    }

    public function frameEvents():void
    {
        //Apply acceleration
        _vx += _accX;
        //Apply friction
        _vx *= _friction;
        //Apply gravity
        _vy += _gravity;
        //Limit the speed, except when the character
        //is moving upwards
        if (_vx > _speedLimit)
        {
            _vx = _speedLimit;
        }
        if (_vx < -_speedLimit)
        {
            _vx = -_speedLimit;
        }
        if (_vy > _speedLimit * 2)
        {
            _vy = _speedLimit * 2;
        }
        //Force the velocity to zero
        //after it falls below 0.1
        if (Math.abs(_vx) < 0.1)
        {
            _vx = 0;
        }
        if (Math.abs(_vy) < 0.1)
        {
            _vy = 0;
        }
    }

The boundaries are calculated in my Level class:

private function onEnterFrame(e:Event):void 
    {
        trace("LevelX:" + this.x);
        trace("stage" + stage.width);

        _hero.frameEvents();

        _levelVX = _hero.vx


        //Hero Boundaries

        if (_hero.x < 0.25 * _viewWidth)
        {
            trace("Hero is in 25% Boundary");
            if (this.x > 0)
            {
                trace("Background is at the beginning");
                this.x = 0;
                _hero.x += _hero.vx;

                if (_hero.x < 0)
                {
                    trace("Hero is at the Beginning");
                    _hero.vx = 0;
                    _hero.x = 0;
                }
            }

            else if (this.x < 0) 
            {
                trace("Background is NOT at the beginning");
                this.x -= _levelVX;
                _hero.x = 0.25 * _viewWidth;
                _hero.vx = 0;
            }
        }


        else if (_hero.x >= 0.75 * _viewWidth)
        {
            trace("Hero in 75% Boundary");
            if (this.x > -this.width)
            {
                trace("Background not at the end");
                this.x -= _levelVX;
                _hero.x = 0.75 * _viewWidth;
                _hero.vx = 0;
            }

            else if (this.x < _viewWidth - this.width) 
            {
                trace("Background is at the end");
                this.x = this.width - _viewWidth;
                _hero.x += _hero.vx;

                if (_hero.x > _viewWidth -_hero.width)
                {
                    trace("Hero is at the End");
                    _hero.x = _viewWidth - _hero.width;
                }
            }

        }

        else

        {
            trace("Hero is Moving in the middle");
            _hero.x += _hero.vx;
        }

        if (_hero.y < 0)
        {
            _hero.vy = 0;
            _hero.y = 0;
        }

        else if (_hero.y + 100 + _hero.height > _viewHeight)
        {
            _hero.vy = 0;
            _hero.y = _viewHeight - _hero.height - 100;
            _hero.isOnGround = true;
        }

        _hero.y += _hero.vy;


    }
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Found another approach, hope this help anyone soon who is programming a sidescroller. Sadly my approach doesn't consider the maximum width for a level movieclip on the stage and it doesn't just render what's inside the stage boundaries. So much to improve, but anyways here is a working answer using custom event handlers:

Hero.as

    //EnterFrame function triggered in Main.as
        public function frameEvents():void
        {   
            //Apply acceleration
            _vx += _accX;

            //Apply friction
            _vx *= C.FRICTION;

            //Apply gravity
            _vy += C.GRAVITY;

            //Limit the speed, except when the character
            //is moving upwards
            if (_vx > C.SPEED_LIMIT)
            {
                _vx = C.SPEED_LIMIT;
            }
            if (_vx < -C.SPEED_LIMIT)
            {
                _vx = -C.SPEED_LIMIT;
            }
            if (_vy > C.SPEED_LIMIT * 2)
            {
                _vy = C.SPEED_LIMIT * 2;
            }

            //Force the velocity to zero
            //after it falls below 0.1
            if (Math.abs(_vx) < 0.1)
            {
                _vx = 0;
                _isMoving = false;
            }
            if (Math.abs(_vy) < 0.1)
            {
                _vy = 0;
                _isMoving = false;
            }

            //Move Horizontally

            //Is Hero moving Left?
            if (_vx < 0)
            {   
                //Check if Hero should move Left
                if ((_levelX >= 0) || (this.x >= 0.25 * C.GAME_WIDTH))
                {
                    //Move Hero Left
                    this.x += _vx;
                    _isMoving = true;

                    //Stop Hero at Left Level Boundary
                    if (this.x < 0)
                    {
                        this.x = 0;
                        _vx = 0;
                        _accX = 0;
                        _bounceX = 10;
                        this.gotoAndStop("standLeft");
                        _isMoving = false;
                    }
                }

                //Trigger Event to move Level right (Hero left walking motion)
                else
                {
                    dispatchEvent(new Event("levelMoves", true));
                    _isMoving = false;
                }
            }

            //Is Hero moving Right?
            else if (_vx > 0)
            {
                //Check if Hero should move Right
                if (_levelX <= C.GAME_WIDTH - _levelWidth || this.x <= (0.75 * C.GAME_WIDTH) - this.width)
                {
                    //Move Hero Right
                    this.x += _vx;
                    _isMoving = true;

                    //Stop Hero at Right Level Boundary
                    if (this.x > C.GAME_WIDTH - this.width)
                    {
                        this.x = C.GAME_WIDTH - this.width;
                        _vx = 0;
                        _accX = 0;
                        _bounceX = -10;
                        this.gotoAndStop("standRight");
                        _isMoving = false;
                    }
                }

                //Trigger Event to move Level left (Hero right walking motion)
                else
                {
                    dispatchEvent(new Event("levelMoves", true));
                    _isMoving = false;
                }
            }

            //Move Vertically

            //Move Hero up/down
            this.y += _vy;

            //Stop Hero at Top Level Boundary
            if (this.y < 0)
            {
                _vy = 0;
                this.y = 0;
            }

            if (this.y > C.GAME_HEIGHT + 120)
            {
                dispatchEvent(new Event("gameOver", true));
            }
        }

And the part in the Main.as that initializes the Level objects in arrays to move them later:

private function setUpGameObjects()
    {   
        _gameObjects = [];
        _grounds = [];
        _platforms = [];
        _enemies = [];
        _gold = [];

        //Loop through all items on stage
        for (var i=0; i< _level01.gameObjects.numChildren; i++)
        {
            var currObject = _level01.gameObjects.getChildAt(i);


                //Identify the Ground Objects
                if (currObject is Ground)
                {
                    _grounds.push(currObject);
                    _gameObjects.push(currObject);
                }

                //Identify the Platform Objects
                else if (currObject is Stone)
                {
                    _platforms.push(currObject);
                    _gameObjects.push(currObject);
                }

                //Identify the Enemy Objects
                else if (currObject is Enemy)
                {
                    _enemies.push(currObject);
                    _gameObjects.push(currObject);
                }

                //Identify the JumpEnemy Objects
                else if (currObject is JumpEnemy)
                {
                    _enemies.push(currObject);
                    _gameObjects.push(currObject);
                }

                //Identify the BossEnemy
                else if (currObject is BossEnemy)
                {
                    _enemies.push(currObject);
                    _gameObjects.push(currObject);
                }

                //Identify the Gold Objects
                else if (currObject is Gold)
                {
                    _gold.push(currObject);
                    _gameObjects.push(currObject);
                }

                //Identify the House
                else if (currObject is House)
                {
                    _gameObjects.push(currObject);
                }

                //Identify the Finish Zone for Game completion check
                else if (currObject is FinishZone)
                {
                    _gameObjects.push(currObject);
                }
        }
    }

And finally my event handler that moves the level as soon as the hero triggers the event:

private function onLevelMoves(e:Event):void 
    {
        for (var i in _gameObjects)
        {
            _gameObjects[i].x -= _hero.vx;

            if (_gameObjects[i].x < C.GAME_WIDTH - _levelWidth + _gameObjects[i].initX)
            {
                _gameObjects[i].x = C.GAME_WIDTH - _levelWidth + _gameObjects[i].initX;
            }

            if (_gameObjects[i].x > _gameObjects[i].initX)
            {
                _gameObjects[i].x = _gameObjects[i].initX;
            }
        }

        _level01.skyObjects.x -= _hero.vx;

        _level01.sky.x -= _hero.vx;

        if (_level01.sky.x < C.GAME_WIDTH - _levelWidth + _level01.sky.initX)
        {
            _level01.sky.x = C.GAME_WIDTH - _levelWidth + _level01.sky.initX
        }

        if (_level01.sky.x > _level01.sky.initX)
        {
            _level01.sky.x = _level01.sky.initX
        }

        _level01.hills.x -= _hero.vx * 1/2;

        if (_level01.hills.x < C.GAME_WIDTH - _levelWidth + _level01.hills.initX)
        {
            _level01.hills.x = C.GAME_WIDTH - _levelWidth + _level01.hills.initX
        }

        if (_level01.hills.x > _level01.hills.initX)
        {
            _level01.hills.x = _level01.hills.initX
        }

        _level01.mountains.x -= _hero.vx * 1/4;

        if (_level01.mountains.x < C.GAME_WIDTH - _levelWidth + _level01.mountains.initX)
        {
            _level01.mountains.x = C.GAME_WIDTH - _levelWidth + _level01.mountains.initX
        }

        if (_level01.mountains.x > _level01.mountains.initX)
        {
            _level01.mountains.x = _level01.mountains.initX
        }
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.