Movement mutators

Hello all,

Been writing some movement code for a new game I’m currently working on and have run into a little snag which I can’t seem to get my head around. I thought mabey asking a fresh pair of eyes to have a butchers and give me a nudge in the right direction would be a more favourable alternative than ripping all of my hair out.

To start off I’ll show the basic code for the movement just so you get an idea of what’s going on.

dynamic class scripts.playerCode extends MovieClip{

    var horizontalPosition        :Number  = 512;
    var verticalPosition           :Number  = 700;
    var movementSpeed         :Number  = 0;
    var movementMagnitude    :Number  = 10;
    var playerBoundaries         :Object;
    var xMovement                :Boolean  = false;

    function playerCode(){
        
        _x = horizontalPosition;
        _y = verticalPosition;
        
        playerBoundaries = {left:0, top:0, right:1024, bottom:0};
        
        }
    
    function playerMovement(){
        
        movement = {_x:0, _y:0};
        movementSpeed = 0;
        
        
        if (Key.isDown(Key.LEFT)){
            movement._x = movement._x-(movementSpeed + movementMagnitude);
        }
        if (Key.isDown(Key.RIGHT)){
            movement._x = movement._x+(movementSpeed + movementMagnitude);
        }

        if (_x+movement._x+_width/2.0>playerBoundaries.right) {
            _x = playerBoundaries.right-_width/2.0;
            xMovment = true;
        }
        if (_x+movement._x-_width/2.0<playerBoundaries.left) {
            _x = playerBoundaries.left+_width/2.0;
            xMovement = true;
        }
        if (xMovement == false) {
            _x = _x+movement._x;
        }
    }

This is my standard movement code and allows my character to move left and right in the levels boundaries. It’s in a separate .as file which is loaded from an engin.as file linked to an engin movie clip on the main time line. The playerMovement function is called upon in an onEnterFrame function placed in the engin.as file allowing the players position to be constantly updated.

My next task was to create a sprint button for the players movement which will increase the players speed when toggled.

dynamic class scripts.playerCode extends MovieClip{

    var horizontalPosition        :Number     = 512;
    var verticalPosition        :Number     = 700;
    var movementSpeed            :Number        = 0;
    var movementMagnitude        :Number        = 10;
    var playerBoundaries        :Object;
    var xMovement                :Boolean    = false;


    function playerCode(){
        
        _x = horizontalPosition;
        _y = verticalPosition;
        
        playerBoundaries = {left:0, top:0, right:1024, bottom:0};
        
        }
    
    function playerMovement(){
        
        movement = {_x:0, _y:0};
        movementSpeed = 0;
        
        
        if (Key.isDown(Key.LEFT)){
            movement._x = movement._x-(movementSpeed + movementMagnitude);
        }
        if (Key.isDown(Key.RIGHT)){
            movement._x = movement._x+(movementSpeed + movementMagnitude);
        }

        if (_x+movement._x+_width/2.0>playerBoundaries.right) {
            _x = playerBoundaries.right-_width/2.0;
            xMovment = true;
        }
        if (_x+movement._x-_width/2.0<playerBoundaries.left) {
            _x = playerBoundaries.left+_width/2.0;
            xMovement = true;
        }
        if (xMovement == false) {
            _x = _x+movement._x;
        }
    }
    
    function sprint(){
        
        if(Key.isDown(65)){
            movementMagnitude = 20
        }
    }
}

The sprint function is called up in the onEnterFrame function in the engin. as but could just as easily be placed in the playerCode function. When the A key was held down the players speed would increase and when released set back to default. This was simple enough. My next task was to create some code which causes the player to tire if the sprint key was toggled for more than three seconds. After a brief period of being tired in which the players speed decreased passed the normal the player speed would reset to default again. I figured the best was to do this was using a switch and give the player different states, then using a series of timers toggle between the states.

dynamic class scripts.playerCode extends MovieClip{

    var horizontalPosition         :Number     = 512;
    var verticalPosition            :Number     = 700;
    var movementSpeed          :Number      = 0;
    var movementMagnitude     :Number     = 10;
    var playerBoundaries          :Object;
    var sprintTime                   :Number     = 3000;
    var tiredTime                    :Number     = 1500;
    var xMovement                 :Boolean     = false;
    var playerState                :String        = "normal"
    var playerStart                ;
    var tiredStart                ;

    function playerCode(){
        
        _x = horizontalPosition;
        _y = verticalPosition;
        
        playerBoundaries = {left:0, top:0, right:1024, bottom:0};
        playerStart = getTimer()
        }
    
    function playerMovement(){
        
        movement = {_x:0, _y:0};
        movementSpeed = 0;
        
        
        if (Key.isDown(Key.LEFT)){
            movement._x = movement._x-(movementSpeed + movementMagnitude);
        }
        if (Key.isDown(Key.RIGHT)){
            movement._x = movement._x+(movementSpeed + movementMagnitude);
        }

        if (_x+movement._x+_width/2.0>playerBoundaries.right) {
            _x = playerBoundaries.right-_width/2.0;
            xMovment = true;
        }
        if (_x+movement._x-_width/2.0<playerBoundaries.left) {
            _x = playerBoundaries.left+_width/2.0;
            xMovement = true;
        }
        if (xMovement == false) {
            _x = _x+movement._x;
        }
    }
    
    function sprint(){
        
        playerState = "normal";
        
        if(Key.isDown(65)){
            playerState = "sprinting";
            sprintStart = getTimer()
        }
        if(sprintStart - playerStart > sprintTime){
            playerState = "tired";
            tiredStart = getTimer()
        }
        if(tiredStart - sprintStart > tiredTime){
            
            playerState = "normal";
        }
        
    switch(playerState){
        case "normal":
        trace("player is currently normal")
        break;
        case "sprinting":
        trace("Player is currently sprinting");
        break;
        case "tired":
        trace("Player is currently tired");
        break;
    }
    }
    
}

This is about as far as I have come, It feels messy using this many if statements and getTimer vars but it’s the only way I could logically see a light at the end of the tunnel. before this I went through and elaborate maze of true or false statements which again failed.

The majors problems I face at the moment are:

  1. Because of the playerStart timers location after three seconds of the game has passed regardless of how long or even if the sprint key has been pressed. The player will become instantly tired once sprint is toggled.

  2. Once you have been through the sprint cycle and the tired cycle and the playerState has reverted back to default(“normal”) the sprint key will never register again. Effectively a one trick pony.

3)After trying many different positions of the timers, or combining lots of true and false commands when I progress further i am constantly confronted with an infinite loop of the playerState which I can’t seem to break.

The idea is once I can get this bit of code working properly then adding in other mutators which effect the players movement will be as easy as creating a new playerState case. but until then its a completely useless problem which I can’t find a way around.

I’m open to any and all suggestions and would greatly appreciate any help.

Thanks in advance

Sigs