KnTproject Posted September 17, 2016 Share Posted September 17, 2016 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 More sharing options...
DegGa Posted September 17, 2016 Share Posted September 17, 2016 Hi I think you should replace: " game.physics.arcade.enable(dude,dude2);" with : "game.physics.arcade.enable([dude, dude2]);". Because the first argument should be a single object or an array of object to enable physics on. You can read more about that here. Link to comment Share on other sites More sharing options...
KnTproject Posted September 18, 2016 Author Share Posted September 18, 2016 Wow, thanks, it works! But, why would these [] help? I don't understand the site. Link to comment Share on other sites More sharing options...
DegGa Posted September 18, 2016 Share Posted September 18, 2016 These [] in javascript are arrays (arrays hold any type of data seperated by "," ), so "game.physics.arcade.enable([dude, dude2]);" pass into the objects in this array and will enable physics on them. Link to comment Share on other sites More sharing options...
KnTproject Posted September 18, 2016 Author Share Posted September 18, 2016 Ah! I understand it, thank you very much! douglas and DegGa 2 Link to comment Share on other sites More sharing options...
Recommended Posts