Jump to content

object with same name collide with object


choppermio
 Share

Recommended Posts

hi have problem with this code,

 

this code create object("knive") every one second and it falls from the top the problem is the knive cross the player and don't collide with him i need to make all the knives created when it hits the player  the knive stop

var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });function preload() {	  nin =   game.load.image('nin', 'assets/player.png'); 		  game.load.image('sky','sky.png'); 		  game.load.image('knive','assets/knife.png'); 	 	}function create() {	//  We're going to be using physics, so enable the Arcade Physics system    game.physics.startSystem(Phaser.Physics.ARCADE);    game.add.sprite(0, 0, 'sky');	ninn = game.add.sprite(0, 0, 'nin');	ninn.enableBody = true;	game.physics.arcade.enable(ninn);    //  Player physics properties. Give the little guy a slight bounce.    ninn.body.bounce.y = 0.2;    ninn.body.gravity.y = 300;    ninn.body.collideWorldBounds = true;    ninn.body.collideWorldBounds = true;scoreText = game.add.text(10,10,"-",{				font:"bold 16px Arial"			}); knive = '';score = 0;  setInterval(function(){  	score++;  	if(localStorage.topscore ==null){  		localStorage.topscore=0  	}else{  		if(score > localStorage.topscore){  			localStorage.topscore = score;  		}else{  			  		}  	}  	topscore = localStorage.topscore;scoreText.text = "Score: "+score+"\n top score"+topscore;rannnd =  Math.floor(Math.random() * (600 - 10 + 1)) + 10;//console.log(rannnd);  	knive = game.add.sprite(rannnd, 0, 'knive');    game.add.tween(knive);  	game.physics.arcade.enable(knive);  	 knive.body.bounce.y = 0.2;    knive.body.gravity.y = 300;      },1000);   chckFlip = 0;}function update() {//	if(knive){	//console.log(knive.body.x);	//}		collllide = game.physics.arcade.collide(knive, ninn);	if(collllide){		//console.log('sli');	}	cursors = game.input.keyboard.createCursorKeys();//  Reset the players velocity (movement)    ninn.body.velocity.x = 0;    if (cursors.left.isDown)    {    	if(chckFlip == 0){    		    		chckFlip=1;    		console.log(chckFlip);    		ninn.scale.x *= -1;    	}    	//ninn.scale.x *= -1;        //  Move to the left        ninn.body.velocity.x = -150;        ninn.animations.play('left');    }    else if (cursors.right.isDown)    {    	if(chckFlip == 1){    		    		chckFlip=0;    		console.log(chckFlip);    		ninn.scale.x *=-1;    	}    	//ninn.scale.x *= -1;        //  Move to the right        ninn.body.velocity.x = 150;        ninn.animations.play('right');    }    else    {        //  Stand still        ninn.animations.stop();        ninn.frame = 4;    }    //  Allow the player to jump if they are touching the ground.    //console.log(ninn.body.velocity.y);    if (cursors.up.isDown   &&  ninn.body.velocity.y >-10 )    {        ninn.body.velocity.y = -350;             }}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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