Why does my goblin only choose a walk direction once?

Posted by Eogcloud on Game Development See other posts from Game Development or by Eogcloud
Published on 2013-10-18T01:24:16Z Indexed on 2013/10/18 4:14 UTC
Read the original article Hit count: 165

Filed under:
|
|

I'm working on a simpe 2d canvas game that has a small goblin sprite who I want to get pathing around the screen.

What I originally tried was a random roll that would choose a direction, the goblin would walk that direction. It didnt work effectively, he sort of wobbled in one spot.

Here's my current apporach but he only runs in a rundom direction and doesnt change. What am I doing wrong?

Here's all the relevant code to the goblin object and movement.

    var goblin = {
            speed: 100,
            pos: [0, 0],
            dir: 1,
            changeDir: true,
            stepCount: 0,
            stepTotal: 0,
            sprite: new Sprite(
                                goblinImage, 
                                [0,0], 
                                [30,45], 
                                6, 
                                [0,1,2,3,2,1],
                                true)
    };


    function getNewDir(){
            goblin.dir = Math.floor(Math.random()*4)+1;
    };

    function checkGoblinMovement(){
        if(goblin.changeDir){
            goblin.changeDir = false;
            goblin.stepCount = 0;
            goblin.stepTotal = Math.floor(Math.random*650)+1;
            getNewDir();
        }
        else
         {
           if(goblin.stepCount === goblin.stepTotal){
                 goblin.changeDir = true;
         }
       }
    };

    function update(delta){

        healthCheck();

        if(isGameOver){
            gameOver();
        }
        if(!isGameOver){
        updateCharLevel();  
        keyboardInput(delta);   

        moveGoblin(delta);
        checkGoblinMovement();  

        goblin.sprite.update(delta);

        //update sprites
        if(mainChar.kills!=0 && bloodReady){
            for(var i=0; i<bloodArray.length; i++){
                    bloodArray[i].sprite.update(delta);
         }
        }

        //collision detection   
        if(collision(mainChar, goblin))
            {
               combatOutcome(combatEvent());
               combatCleanup();
            }
            }
        };

    function main(){
        var now = Date.now();
        var delta = (now - then)/1000;  
        if(!isGameOver){
        update(delta);
        }
        draw();
        then = now;
    };

function moveGoblin(delta){

    goblin.stepCount++;

    if(goblin.dir === 1){
      goblin.pos[1]  -= goblin.speed * delta* 2;        
        if(goblin.pos[1] <= 85){
            goblin.pos[1] = 86;
          }
    }

    if(goblin.dir === 2){
        goblin.pos[1]  += goblin.speed * delta;     
        if(goblin.pos[1] > 530){
            goblin.pos[1] = 531;
          }
    }

    if(goblin.dir === 3){
      goblin.pos[0]  -= goblin.speed * delta;       
        if(goblin.pos[0] < 0){
            goblin.pos[0] = 1;
          }
    }

    if(goblin.dir === 4){
      goblin.pos[0]  += goblin.speed * delta* 2;        
        if(goblin.pos[0] > 570){
            goblin.pos[0] = 571;
          }
    }

};  

© Game Development or respective owner

Related posts about JavaScript

Related posts about sprites