A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [AS2]Character Movement and Boundaries

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    13

    [AS2]Character Movement and Boundaries

    I have an issue with character movement. I've coded the movement so that the background scrolls instead of the character moving (top down view) and when the bg image reaches the edge of the stage it stops and the character starts to move. It does this until it reaches the centre again at which point the image scrolls again. Hope I've explained that well enough. The problem is, when I add boundary checking to stop the character from going out of the screen it works, but the bg image won't scroll anymore. I have provided the code and the fla below and if anyone could help me that would be great.

    Actionscript Code:
    onClipEvent(load)
    {
        //Velocity variables
        var vy:Number = 0;
        var vx:Number = 0;
       
        var alive:Boolean = true;
       
        //Width and height variables
        var playerHalfWidth:Number = (this._width / 2) + 14;
        var playerHalfHeight:Number = (this._height / 2) + 8;
        var bgHalfWidth:Number = _root.bgImage._width / 2;
        var bgHalfHeight:Number = _root.bgImage._height / 2;
       
        //Inner boundaries
        var rightInner:Number = Stage.width;
        var leftInner:Number = 0;
        var topInner:Number = 0;
        var bottomInner:Number = Stage.height;
       
        this._x = Stage.width / 2;
        this._y = Stage.height / 2;
    }

    onClipEvent(enterFrame)
    {
        //trace("vx: " + vx);
        //trace("vy: " + vy);
       
        trace("y: " + _root.bgImage._y);
        trace("x: " + _root.bgImage._x);
       
        if(alive == true)
        {
            takePicture();
            bgMove();
            playerMove();
            boundaryCheck();
        }
       
        function playerMove()
        {
            if(Key.isDown(Key.UP))
            {
                vy = -5;
                this._rotation = 0;
               
                if(_root.bgImage._y >= bgHalfHeight || _root.bgImage._y <= 0)
                {
                    this._y += vy;
                    trace("Moving UP");
                }
            }
           
            if(Key.isDown(Key.DOWN))
            {
                vy = 5;
                this._rotation =180;
               
                if(_root.bgImage._y <= 0 || _root.bgImage._y >= bgHalfHeight)
                {
                    this._y += vy;
                }
               
                if(this._x >= Stage.width - playerHalfWidth)
                {
                    vy = 0;
                    trace("Moving DOWN");
                }
            }
           
            if(!Key.isDown(Key.UP) && !Key.isDown(Key.DOWN))
            {
                vy = 0;
            }
           
            if(Key.isDown(Key.LEFT))
            {
                vx = -5;
                this._rotation = 270;
               
                if(_root.bgImage._x >= bgHalfHeight || _root.bgImage._x <= Stage.width - bgHalfWidth)
                {
                    this._x += vx;
                    trace("Moving LEFT");
                }
            }
           
            if(Key.isDown(Key.RIGHT))
            {
                vx = 5;
                this._rotation = 90;
               
                if(_root.bgImage._x >= bgHalfHeight ||  _root.bgImage._x <= Stage.width - bgHalfWidth)
                {
                    this._x += vx;
                    trace("Moving RIGHT");
                   
                }
            }
           
            if(!Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT))
            {
                vx = 0;
            }
        }
       
        function bgMove()
        {
            //trace("x: " + _root.bgImage._x);
            //trace("y: " + _root.bgImage._y);
            //trace("w: " + _root.bgImage._width);
            //trace("h: " + _root.bgImage._height);
            if(this._x == Stage.width / 2)
            {
                _root.bgImage._x += -vx;
            }
           
            if(this._y == Stage.height / 2)
            {
                _root.bgImage._y += -vy;
            }
           
            if(_root.bgImage._y >= bgHalfHeight)
            {
                _root.bgImage._y = bgHalfHeight;
            }
           
            else if(_root.bgImage._y <= 0)
            {
                _root.bgImage._y = 0;
            }
           
            if(_root.bgImage._x >= bgHalfWidth)
            {
                _root.bgImage._x = bgHalfWidth;
            }
           
            else if(_root.bgImage._x <= Stage.width - bgHalfWidth)
            {
                _root.bgImage._x = Stage.width - bgHalfWidth;
            }
           
            /*if(Key.isDown(Key.UP))
            {
                _root.bgImage._y += dy;
                this._rotation = 0;
               
                if(_root.bgImage._y >= _root.bgImage._height / 2)
                {
                    _root.bgImage._y = _root.bgImage._height / 2;
                    this._y -= dy;
                }
            }
           
            if(Key.isDown(Key.DOWN))
            {
                _root.bgImage._y -= dy;
                this._rotation = 180;
               
                if(_root.bgImage._y <= 0)
                {
                    _root.bgImage._y = 0;
                    this._y += dy;
                }
            }
           
            if(Key.isDown(Key.LEFT))
            {
                _root.bgImage._x += dx;
                this._rotation = 270;
            }
           
            if(Key.isDown(Key.RIGHT))
            {
                _root.bgImage._x -= dx;
                this._rotation = 90;
            }*/

        }
       
        function takePicture()
        {
            if(Key.isDown(Key.SPACE))
            {
                this.gotoAndPlay(2);
                if(this._currentframe(13))
                {
                    this.gotoAndStop(1);
                }
            }
        }
       
        function boundaryCheck()
        {
            if(this._x >= Stage.width - playerHalfWidth)
            {
                this._x = Stage.width - playerHalfWidth;
            }
           
            else if(this._x <= playerHalfWidth)
            {
                this._x = playerHalfWidth;
            }
           
            if(this._y >= Stage.height - playerHalfHeight)
            {
                this._y = Stage.height - playerHalfHeight;
            }
           
            else if(this._y <= playerHalfHeight)
            {
                this._y = playerHalfHeight;
            }
           
        }
    }
    Attached Files Attached Files

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Used a lot of time trying to solve your problem, but didn't succeed in the end. My conclusion is that the code is poorly constructed as the coherence between the if/else statements is week, and you'll have to re-write your code using another approach. This is as far as I can help you.
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    13
    I feared that might be the problem. Thank you so much for taking the time to do it. I'll approach it from a different angle.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center