Deino Posted March 10, 2015 Share Posted March 10, 2015 This is a small game I made,I need the coins to disappear as soon as the player touches them. I tried giving the coins high velocity so they can move away but that does not work very well.var game = new Phaser.Game(1000,600, Phaser.AUTO, 'gameDiv');var mainState = { preload: function() { game.stage.backgroundColor = "#71c5cf"; game.load.image('ball','assets/fireball.png'); game.load.image('wv','assets/wallVertical.png'); game.load.image('wh','assets/wallHorizontal.png'); game.load.image('coin','assets/coin.png'); }, create: function() { game.physics.startSystem(Phaser.Physics.ARCADE); this.ball = this.game.add.sprite(100,250,'ball'); this.createworld(); game.physics.arcade.enable(this.ball); this.ball.body.gravity.y = 1000; var spacekey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); spacekey.onDown.add(this.jump,this); var lkey = this.game.input.keyboard.addKey(Phaser.Keyboard.LEFT); lkey.onDown.add(this.lmove,this); var rkey = this.game.input.keyboard.addKey(Phaser.Keyboard.RIGHT); rkey.onDown.add(this.rmove,this); this.score = 0; this.labelScore = this.game.add.text(30, 90, "", { font: "30px Arial", fill: "#ffffff" }); this.labelScore.text = this.score; }, update: function() { game.physics.arcade.collide(this.ball,this.walls); game.physics.arcade.collide(this.ball,this.coins,this.scoreincrease,null,this); game.physics.arcade.collide(this.ball,this.coins,this.coingoes,null,this); if (this.ball.inWorld == false) { this.restartGame(); } }, restartGame: function() { game.state.start('main'); }, jump: function() { this.ball.body.velocity.y = -350;}, lmove: function() { this.ball.body.velocity.x = -200; }, rmove: function() { this.ball.body.velocity.x = 200; }, createworld: function() { this.walls = this.game.add.group() this.walls.enableBody = true; game.add.sprite(150,150,'wv',0,this.walls); game.add.sprite(170,180,'wh',0,this.walls); game.add.sprite(170,480,'wh',0,this.walls); game.add.sprite(450,480,'wh',0,this.walls); game.add.sprite(650,150,'wv',0,this.walls); game.add.sprite(450,180,'wh',0,this.walls); this.walls.setAll('body.immovable',true); this.coins = this.game.add.group(); this.coins.enableBody = true; game.add.sprite(10,10,'coin',0,this.coins); game.add.sprite(10,60,'coin',0,this.coins); game.add.sprite(30,10,'coin',0,this.coins); game.add.sprite(30,60,'coin',0,this.coins); }, scoreincrease : function() { this.score += 20; this.labelScore.text = this.score; }, coingoes : function() { this.coin.body.velocity.y = -10000; } };game.state.add('main',mainState);game.state.start('main'); Link to comment Share on other sites More sharing options...
Nikow Posted March 10, 2015 Share Posted March 10, 2015 Why duplicated 'game.physics.arcade.collide(this.ball,this.coins,this.scoreincrease,null,this);' just one is enough ! And after : scoreincrease : function(ball,coins){ this.score += 20; this.labelScore.text = this.score; coins.kill(); }, Deino 1 Link to comment Share on other sites More sharing options...
Recommended Posts