Jump to content

collision problem


hugo
 Share

Recommended Posts

i'm trying to make a collision between my plane and some clouds, but is not working

i think i'm do it wrong

can 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');

},
};


 

 

post-8127-0-12510600-1398182655.png

Link to comment
Share on other sites

 

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

thank you all

i think i made it

I 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

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

 Share

  • Recently Browsing   0 members

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