ibobtouch Posted October 22, 2014 Share Posted October 22, 2014 Hi everyone, My apologies for my bad english, i'm french Here's my code:<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Phaser</title> <script type="text/javascript" src="js/phaser.js"></script> <style type="text/css"> body { margin: 0; } </style></head><body><div id="game"></div><script type="text/javascript">var game = new Phaser.Game(800, 600, Phaser.AUTO, 'game', { preload: preload, create: create, update: update });function preload() { this.load.image('player', 'assets/j1.png'); this.load.image('plate', 'assets/bloc.png');}function create() { game.physics.startSystem(Phaser.Physics.ARCADE); this.stage.backgroundColor = '#7e7e7e'; player = new Player(game, 20, 20); game.add.existing(player); game.time.events.loop(Phaser.Timer.SECOND * 1.25, genPlateforms); //Generation de plateforme //FPS game.time.advancedTiming = true; fpsText = game.add.text( 20, 20, '', { font: '16px Arial', fill: '#ffffff' } ); }function update() { if (game.time.fps !== 0) { fpsText.setText(game.time.fps + ' FPS'); }}var genPlateforms = function(){ random = game.rnd.integerInRange(-50, 50); //Valeur Random plateforms = new Plateforms(game, game.width , game.height / 2 + random); //Plateforms a hauteur random depuis le milieu game.add.existing(plateforms);}// Player contructorvar Player = function(game, x, y, frame) { Phaser.Sprite.call(this, game, x, y, 'player', frame); this.game.physics.arcade.enableBody(this); this.body.bounce.y = 0.1 ; this.body.gravity.y = 600; this.body.collideWorldBounds = true; //Collision avec les bords cursors = this.game.input.keyboard.createCursorKeys();};Player.prototype = Object.create(Phaser.Sprite.prototype);Player.prototype.constructor = Player;Player.prototype.update = function() {this.body.velocity.x = 0;if (cursors.left.isDown) { this.body.velocity.x = -500; }else if (cursors.right.isDown) { this.body.velocity.x = 500; }if (cursors.up.isDown)// && this.body.blocked.down) { this.body.velocity.y = -400; }};//Plateforms Constructorvar Plateforms = function(game, x, y, frame) { Phaser.Sprite.call(this, game, x, y, 'plate', frame); this.anchor.setTo(0.5,0.5); this.game.physics.arcade.enableBody(this); this.body.immovable = true; this.body.velocity.x = -350; this.checkWorldBounds = true; this.outOfBoundsKill = true; //Detruire l'objet non visible};Plateforms.prototype = Object.create(Phaser.Sprite.prototype);Plateforms.prototype.constructor = Plateforms;Plateforms.prototype.update = function() {game.physics.arcade.collide(player, plateforms); //Collision entre le player et les plateformes};</script></body></html>this is what you get: So i split the screen by 3 zone to explain my problem: Zone 3:The player is able to jump on the plateforms, it work perfectly. Zone 2:The player don't collide with the top of the plateforms, Zone 1:The player don't collide with the plateforms I don't know why. I already try it with the yeoman generator, same result, so i'm sure the problem is from the code(also because i'm a newbie in JS). Link to comment Share on other sites More sharing options...
lewster32 Posted October 22, 2014 Share Posted October 22, 2014 You need to call any collide/overlap routines in your main update function, not in the platform instances. Remove the Platform.prototype.update part entirely, and then change the main update function to this:function update() { game.physics.arcade.collide(player, plateforms); if (game.time.fps !== 0) { fpsText.setText(game.time.fps + ' FPS'); }}Although this won't work as intended either, it's a step in the right direction. You should make plateforms a group via game.add.group():function create() { game.physics.startSystem(Phaser.Physics.ARCADE); this.stage.backgroundColor = '#7e7e7e'; player = new Player(game, 20, 20); game.add.existing(player); game.time.events.loop(Phaser.Timer.SECOND * 1.25, genPlateforms); //Generation de plateforme //FPS game.time.advancedTiming = true; fpsText = game.add.text( 20, 20, '', { font: '16px Arial', fill: '#ffffff' } ); plateforms = game.add.group();}And change genPlateforms to the following:var genPlateforms = function(){ random = game.rnd.integerInRange(-50, 50); //Valeur Random var plateform = new Plateforms(game, game.width , game.height / 2 + random); //Plateforms a hauteur random depuis le milieu plateforms.add(plateform);}Then the above should work and check against all platforms. Link to comment Share on other sites More sharing options...
ibobtouch Posted October 22, 2014 Author Share Posted October 22, 2014 Thank you lewster32 !It work but do you know why i can't jump when the player is on a platforms ? Link to comment Share on other sites More sharing options...
lewster32 Posted October 22, 2014 Share Posted October 22, 2014 Because body.blocked.down only detects when you're stood on tiles or the world bounds - you need to check body.touching.down instead, and ensure you check it after the collision check has been done, as it's reset at the start of each frame, and only checking for collisions will it be set appropriately for that frame. You may want to do this by moving the movement code out of Player.prototype.update and calling it manually instead to be sure - change Player.prototype.update to Player.prototype.checkControls and then in your main update function do:function update() { game.physics.arcade.collide(player, plateforms); if (game.time.fps !== 0) { fpsText.setText(game.time.fps + ' FPS'); } if (player) { // just to be sure the player object has been instantiated - you may not need this if statement player.checkControls(); }} Link to comment Share on other sites More sharing options...
ibobtouch Posted October 22, 2014 Author Share Posted October 22, 2014 Oh now I understand ! thanks again lewster32, you helped me alot ! lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts