Deino Posted March 13, 2015 Report Share Posted March 13, 2015 Hey everyone, I succeeded in adding animations to my character but it keeps running off the platform, I want it not to jump again until it has reached the platform as well.var game = new Phaser.Game(800,600,Phaser.AUTO,'gameDiv');var mainState = { preload: function() { game.load.spritesheet('dude','assets/dude.png',32,48); game.load.image('wh','assets/wallHorizontal.png'); }, create: function() { game.physics.startSystem(Phaser.Physics.ARCADE); game.stage.backgroundColor = '#71c5cf'; this.dude = game.add.sprite(400,330,'dude'); this.wh = game.add.sprite(380,380,'wh'); game.physics.arcade.enable(this.wh); this.wh.body.immovable = true; game.physics.arcade.enable(this.dude); this.dude.body.gravity.y = 1000; var spaceKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); spaceKey.onDown.add(this.jump,this); var lcur = game.input.keyboard.addKey(Phaser.Keyboard.LEFT); lcur.onDown.add(this.lmove,this); var rcur = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT); rcur.onDown.add(this.rmove,this); game.physics.arcade.enable(this.dude); this.dude.animations.add('left', [0, 1, 2, 3], 10, true); this.dude.animations.add('turn', [4], 20, true); this.dude.animations.add('right', [5, 6, 7, 8], 10, true); }, update: function() { game.physics.arcade.collide(this.dude,this.wh,null,null,this); if(this.dude.inWorld == false) { game.state.start('main'); } }, jump: function() { this.dude.body.velocity.y = -300; }, lmove: function() { this.dude.body.velocity.x = -300; this.dude.animations.play('left'); }, rmove: function() { this.dude.body.velocity.x = 300; this.dude.animations.play('right'); }};game.state.add('main',mainState);game.state.start('main'); Quote Link to comment Share on other sites More sharing options...
valueerror Posted March 13, 2015 Report Share Posted March 13, 2015 in arcade physics you have a very convenient method called "onFloor()" to find out if a body is on floor.. that's all you need Deino 1 Quote Link to comment Share on other sites More sharing options...
Deino Posted March 14, 2015 Author Report Share Posted March 14, 2015 I want the player to stop running after sometime, how do I do that? Quote Link to comment Share on other sites More sharing options...
hollowdoor Posted March 14, 2015 Report Share Posted March 14, 2015 If you want the player animation to be still just use the frame property. Like:this.dude.frame = 40;Put that somewhere in your update after an if else condition you like. Like valueerror said. You can check the onFloor() state, and set the sprite frame to whatever you want. Deino 1 Quote Link to comment Share on other sites More sharing options...
valueerror Posted March 14, 2015 Report Share Posted March 14, 2015 oh.. and in your case you need to set velocity.x to 0 somewhere if no button is pressed. you know that every basic tutorial and some of the official examples cover all of these problems Deino 1 Quote Link to comment Share on other sites More sharing options...
Deino Posted March 14, 2015 Author Report Share Posted March 14, 2015 Thanks, valueerror and hollowdoor, can you provide links for any such tutorial that covers everything? I have seen flappybird and platformer. I understand most of the code in phaser-examples but not all of it. Quote Link to comment Share on other sites More sharing options...
Deino Posted March 14, 2015 Author Report Share Posted March 14, 2015 I modified the jump function to include onFloor and changed gravity to 250 but it does not workjump: function() { if(this.dude.body.onFloor()) { this.dude.body.velocity.y = -300; } }, Quote Link to comment Share on other sites More sharing options...
valueerror Posted March 14, 2015 Report Share Posted March 14, 2015 http://phaser.io/examples/v2/arcade-physics/platformer-basicsMaybe this helps? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.