I’m not 100% I can ask a phaser question on the corona forums, but if it doesn’t work, oh well.
My problem:
I’ve created this game where these stones are basically obstacles and the point of the game is to shoot them with bullets. When the bullets collide with the stone, nothing happens. Here’s some of the code:
Can someone please help me?
var bg;var stone; var bullets; //space between the bullets var bulletTime = 0; //fire the bullets var fireButton; //400 and 500 is the size of my game screen. This variable will use the preload, create and update function. var game = new Phaser.Game(400, 500, Phaser.AUTO, '', { preload: preload, create: create, update: update }); function preload() {game.load.image('bg', 'assets/sprites/R.png'); game.load.image('car', 'assets/sprites/Car.png'); game.load.image('stone', 'assets/sprites/Stone.png'); game.load.image('bullet', 'assets/sprites/bullet.png'); } function create() {bg = game.add.tileSprite(0, 0, 400, 500, 'bg'); car = game.add.sprite(173, 390, 'car'); car.scale.setTo(0.10, 0.10); game.physics.enable(car, Phaser.Physics.ARCADE); bullets = game.add.group(); //enable physics on the bullet bullets.enableBody = true; //physics type bullets.physicsBodyType = Phaser.Physics.ARCADE; bullets.createMultiple(30, 'bullet'); bullets.setAll('anchor.x', 0.01); bullets.setAll('anchor.y', 1); //take the bullet out of memory if it's off screen bullets.setAll('outOfBoundsKill', true); //check if bullets are on screen bullets.setAll('checkWorldBounds', true); fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); stone = game.add.sprite(20, 350, 'stone'); stone.scale.setTo(0.08, 0.08); } function update() { //Give the variables some movement game.physics.arcade.overlap(bullets, stone, collisionHandler, null, this); bg.tilePosition.y += 2; stone.y = stone.y + 6; if(stone.y \> 500) { stone.y = -600; } if(game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { car.x = car.x - 5; } if(game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { car.x = car.x + 5; } if(fireButton.isDown){ fireBullet(); } if(Phaser.Rectangle.intersects(car.getBounds(), stone.getBounds())) { car.x = 173; car.y = 390; } } function fireBullet() { if(game.time.now \> bulletTime) { //grab the first bullet bullet = bullets.getFirstExists(false); //fire the bullet if(bullet) { //set the bulllet position to the player position bullet.reset(car.x, car.y); //speed of bullet bullet.body.velocity.y = -400; bulletTime = game.time.now + 200; } } } function collisionHandler(bullets, stone) { bullets.kill(); stone.kill(); }