RonanTheAccel Posted June 21, 2015 Share Posted June 21, 2015 Hi guys, i'm having trouble with this code , the error is "Cannot read property 'velocity' of null" in player.body.velocity.y = Math.abs(playerSpeed);<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Drop Wizard</title> <script type="text/javascript" src="phaser.min.js"></script> <style type="text/css"> body { margin: 0; } </style></head><body><script type="text/javascript">var game = new Phaser.Game(480, 320, Phaser.CANVAS, '', { preload: preload, create: create, update: update });var playerSpeed = 150;var player;var platformGroup;function preload() { game.load.image("platform180","platform180.png"); game.load.image("platform120","platform120.png"); game.load.image("player","player.png"); game.load.image("ground","ground.png");}function create() { platformGroup = game.add.group(); game.physics.startSystem(Phaser.Physics.ARCADE); player = game.add.sprite(240,0,"player"); player.anchor.setTo(0.5); game.physics.enable(player,Phaser.Physics.Arcade); game.physics.arcade.gravity.y = playerSpeed; addPlatform(240,60,"platform180"); addPlatform(340,140,"platform120"); addPlatform(140,140,"platform120"); addPlatform(420,220,"platform120"); addPlatform(60,220,"platform120"); addPlatform(100,316,"ground"); addPlatform(380,316,"ground"); game.input.onDown.add(changeDir,this);}function addPlatform(posX,posY,asset){ platform = game.add.sprite(posX,posY,asset); platform.anchor.setTo(0.5); game.physics.enable(platform,Phaser.Physics.ARCADE); platform.body.allowGravity = false; platform.body.immovable = true; platformGroup.add(platform);}function update() { player.body.velocity.y = Math.abs(playerSpeed); player.body.velocity.x = 0; player.body game.physics.arcade.collide(player, platformgroup, movePlayer); if(player.y>320){ player.y = 0 } if(player.x<12){ player.x=12; playerSpeed*=-1 } if(player.x>468){ player.x=468; playerSpeed*=-1 }trace(player); trace(player.body) trace(player.body.velocity)}function movePlayer(){ player.body.velocity.x = playerSpeed} function changeDir(){ playerSpeed *= -1; }</script></body></html> Link to comment Share on other sites More sharing options...
rich Posted June 21, 2015 Share Posted June 21, 2015 game.physics.enable(player,Phaser.Physics.Arcade);The 2nd parameter is a constant, so case matters. To avoid in future you can just do this:game.physics.arcade.enable(player); Link to comment Share on other sites More sharing options...
Recommended Posts