hugo Posted April 22, 2014 Share Posted April 22, 2014 i'm trying to make a collision between my plane and some clouds, but is not workingi think i'm do it wrongcan someone help-me thanks here's my code play.js var plane; var cloud; var fly; var play = { create: function () { this.game.add.image(0, 0, 'background'); this.game.physics.startSystem(Phaser.Physics.ARCADE); this.game.physics.arcade.gravity.y = 1000; plane = this.game.add.sprite(100, 96, 'plane'); this.game.physics.enable(plane, Phaser.Physics.ARCADE); fly = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); cloud = this.game.add.group(); this.game.time.events.loop(Phaser.Timer.SECOND * 1.25, this.createClouds, this); }, createClouds: function () { cloud = this.game.add.sprite(-(Math.random() - 800), this.game.world.randomY, 'cloud'); cloud.enableBody = true; cloud.physicsBodyType = Phaser.Physics.ARCADE; cloud.animations.add('run'); cloud.animations.play('run', 20, true); this.game.add.tween(cloud).to({ x: '-1600' }, 20000, Phaser.Easing.Linear.None, true); }, update: function () { if (plane.inWorld === false) { this.restartGame(); //this.state.start('menu'); }; if (fly.isDown) { plane.body.velocity.y = -350; }; this.game.physics.arcade.collide(plane, cloud, this.gameOver, null, this); }, restartGame: function () { this.game.state.start('menu'); }, gameOver: function (plane) { //this.game.state.start('gameover'); this.game.state.start('menu'); }, }; Link to comment Share on other sites More sharing options...
marvster Posted April 22, 2014 Share Posted April 22, 2014 I would wild guess that plane and cloud are unknown to the update function.Try this.plane [..] this.cloud [..] to make the properties public. hugo 1 Link to comment Share on other sites More sharing options...
hugo Posted April 22, 2014 Author Share Posted April 22, 2014 I would wild guess that plane and cloud are unknown to the update function.Try this.plane [..] this.cloud [..] to make the properties public.thanks marvsterthat doesn't work... Link to comment Share on other sites More sharing options...
JP91 Posted April 22, 2014 Share Posted April 22, 2014 try remove o modify the "tween". hugo 1 Link to comment Share on other sites More sharing options...
hugo Posted April 22, 2014 Author Share Posted April 22, 2014 try remove o modify the "tween".if i remove the tween my clouds stop movingand if i modify the tween the result is the same... Link to comment Share on other sites More sharing options...
JP91 Posted April 22, 2014 Share Posted April 22, 2014 ??game.add.tween(cloud.body) hugo 1 Link to comment Share on other sites More sharing options...
hugo Posted April 22, 2014 Author Share Posted April 22, 2014 ??game.add.tween(cloud.body)if i do this the clouds don't appear... Link to comment Share on other sites More sharing options...
Luis Felipe Posted April 23, 2014 Share Posted April 23, 2014 Doesn't seem like you're enabling the physics system for the clouds. Try this in the create function:this.game.physics.enable( [ plane, cloud ], Phaser.Physics.ARCADE); hugo 1 Link to comment Share on other sites More sharing options...
hugo Posted April 23, 2014 Author Share Posted April 23, 2014 Doesn't seem like you're enabling the physics system for the clouds. Try this in the create function:this.game.physics.enable( [ plane, cloud ], Phaser.Physics.ARCADE);you are right Luis if i do create: function () { this.game.add.image(0, 0, 'background'); this.game.physics.startSystem(Phaser.Physics.ARCADE); this.game.physics.arcade.gravity.y = 1000; plane = this.game.add.sprite(100, 96, 'plane'); cloud = this.game.add.sprite(900, this.game.world.randomY, 'cloud'); this.game.physics.enable(plane, Phaser.Physics.ARCADE); this.game.physics.enable(cloud, Phaser.Physics.ARCADE); plane.enableBody = true; plane.physicsBodyType = Phaser.Physics.ARCADE; cloud.enableBody = true; cloud.physicsBodyType = Phaser.Physics.ARCADE; cloud.body.allowGravity = false; fly = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); this.game.time.events.loop(Phaser.Timer.SECOND * 1.25, this.createClouds, this); }, createClouds: function () { this.game.add.tween(cloud).to({ x: '-1600' }, 20000, Phaser.Easing.Linear.None, true, 0, true); }, update: function () { if (plane.inWorld === false) { this.restartGame(); //this.state.start('menu'); }; if (fly.isDown) { plane.body.velocity.y = -350; }; this.game.physics.arcade.collide(plane, cloud, this.gameOver, null, this); },the plane collide with the cloud but now i have only one cloud Link to comment Share on other sites More sharing options...
Heppell08 Posted April 23, 2014 Share Posted April 23, 2014 Create a group for the clouds. You have a group called cloud and a sprite called cloud. Have the group named as cloudgroup and the sprite named as cloud. hugo 1 Link to comment Share on other sites More sharing options...
hugo Posted April 23, 2014 Author Share Posted April 23, 2014 Create a group for the clouds. You have a group called cloud and a sprite called cloud. Have the group named as cloudgroup and the sprite named as cloud. i'm completely lost Link to comment Share on other sites More sharing options...
jpdev Posted April 23, 2014 Share Posted April 23, 2014 Take a look here: http://examples.phaser.io/_site/view_full.html?d=arcade%20physics&f=sprite+vs+group.js&t=sprite%20vs%20group And think of the vegetables as clouds.(Leave out the code that makes some of them collectible, instead make them all kill the player (= the plane) by replacing the code in the collision function at the bottom) hugo 1 Link to comment Share on other sites More sharing options...
hugo Posted April 23, 2014 Author Share Posted April 23, 2014 thank you alli think i made itI don't know if this is the right way to do it but it works here's the code play.js var fly; var play = { create: function () { this.game.add.image(0, 0, 'background'); this.game.physics.startSystem(Phaser.Physics.ARCADE); this.game.physics.arcade.gravity.y = 1000; this.plane = this.game.add.sprite(100, 96, 'plane'); this.game.physics.enable(this.plane, Phaser.Physics.ARCADE); fly = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); this.cloud = this.game.add.group(); this.cloud.enableBody = true; this.cloud.physicsBodyType = Phaser.Physics.ARCADE; this.game.time.events.loop(Phaser.Timer.SECOND * 1.25, this.createClouds, this); }, createClouds: function () { clouds = this.cloud.create(-(Math.random() - 800), this.game.world.randomY, 'cloud'); clouds.body.velocity.x = -200; clouds.body.allowGravity = false; clouds.outOfBoundsKill = true; clouds.checkWorldBounds = true; clouds.animations.add('move'); clouds.animations.play('move',20,true); }, update: function () { if (this.plane.inWorld === false) { this.restartGame(); }; if (fly.isDown) { this.plane.body.velocity.y = -350; }; this.game.physics.arcade.collide(this.plane, this.cloud, this.gameOver, null, this); }, restartGame: function () { this.game.state.start('menu'); }, gameOver: function () { //this.game.state.start('gameover'); this.game.state.start('menu'); }, }; Link to comment Share on other sites More sharing options...
JP91 Posted April 23, 2014 Share Posted April 23, 2014 jejejje as Heppell08 says, in your first example you create a variable called cloud, which used to make a group but in no time using the method cloud.create, unlike add a sprite directly, if this is much better. Link to comment Share on other sites More sharing options...
Luis Felipe Posted April 23, 2014 Share Posted April 23, 2014 Good work Link to comment Share on other sites More sharing options...
Recommended Posts