Jump to content

Uncaught TypeError: Cannot read property 'velocity' of null


KnTproject
 Share

Recommended Posts

Hello,

This is my code:
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
        var cursors;
        var dude;
        var dude2;
        var controls = {};

        function preload () {

        game.load.spritesheet('dude','Assets/dude.png', 32,48,9);
        game.load.spritesheet('dude2','Assets/dude.png', 32,48,9);
        }

        function create () {
        dude = this.game.add.sprite(300,400,'dude');
        dude.frame = 4;

        dude.animations.add('left', [0,1,2,3],10,true);
        dude.animations.add('right',[5,6,7,8,],10,true);
        
        cursors = game.input.keyboard.createCursorKeys();

        dude2 = this.game.add.sprite(400,100,'dude2');
        dude2.frame = 4;

        dude2.animations.add('left', [0,1,2,3],10,true);
        dude2.animations.add('right',[5,6,7,8,],10,true);

        controls = {
          right: this.input.keyboard.addKey(Phaser.Keyboard.D),
          left: this.input.keyboard.addKey(Phaser.Keyboard.A),  
        };
        
        game.physics.arcade.enable(dude,dude2);
        }      

      function update() {
        
        dude2.body.velocity.x = 0;

        //Dude2
        if (controls.left.isDown){
          dude2.animations.play('left');
          dude2.body.velocity.x = -100;
          
        }

        else if(controls.right.isDown){
          dude2.animations.play('right');
          dude2.body.velocity.x = +100;
          
        } else {
          dude2.animations.stop();
          dude2.frame = 4;
        }

        //Dude
        if (cursors.left.isDown){
             dude.animations.play('left');
             dude.body.velocity.x = -100;
             
        }
        
        else if (cursors.right.isDown){
            dude.animations.play('right');
            dude.body.velocity.x = +100;
        }

        else {
            dude.animations.stop();
            dude.frame=4;
        }
      }

 

I get the ERROR: Uncaught TypeError: Cannot read property 'velocity' of null

The whole code works, but when I add the code dude.body.velocity.x or something with the word 'velocity' then I get a completely black screen. Do anybody knows a (easy) solution?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...