Jump to content

Error in : "Phaser Cannot read property 'velocity' of null"


Recommended Posts

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

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.

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.


  • Recently Browsing   0 members

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