Jump to content

TileSprites collision


Twizty
 Share

Recommended Posts

I'm tryin to create my own flappy bird and got a problem: my bird (it's actually Darth Vader) doesn't collide with pipes which are changing their position with baground via tilePosition field. But if I change my bird's location, it collides with pipes. What do I do wrong? Here is my code 

var width = $(window).width(), height = 600;var game = new Phaser.Game(width, height, Phaser.AUTO,/*id of element which I want to use as game window */ '', { preload: preload, create: create, update: update });var platforms, cursors, player, texture, stars, score = 0, scoreText,    timer = 0, spacebar, walls, bg, pipes1 = [], pipes2 = [];var valuesForPipes1 = [-50, -150, -250, -350, -450], valuesForPipes2 = [600, 500, 400, 300, 200];function collision() {  alert("You lose!");}function preload() {	game.load.image('sky', 'sky.jpg');	game.load.image('pipe1', 'pipe1.png');	game.load.image('darth', '0001.png');	game.load.image('htrad', '0.png');	game.load.image('star', 'star.png');  game.load.image('pipe2', 'pipe2.png');}function create() {	game.physics.startSystem(Phaser.Physics.ARCADE);	bg = game.add.tileSprite(0, 0, game.stage.bounds.width, height, 'sky');  walls = game.add.group();	walls.enableBody = true;	player = game.add.sprite(300, height/2, 'darth');  game.physics.arcade.enable(player);  player.body.bounce.y = 0.2;  player.body.gravity.y = 250;  player.body.collideWorldBounds = true; 	scoreText = game.add.text(16, 16, 'Score: 0', { fontSize: '32px', fill: '#000' }); 	spacebar = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); 	spacebar.onDown.add(function() { 		player.body.velocity.y = -200; 	}, this)  cursors = game.input.keyboard.createCursorKeys();}function update() {  game.physics.arcade.collide(player, walls, function() { location.reload() }, null, this);  timer++;	bg.tilePosition.x -= 2;  player.body.velocity.x = 0.01;  for (var j = 0; j<pipes1.length; j++) {    pipes1[j].x -= 2;    pipes2[j].x -= 2;    game.physics.arcade.collide(player, pipes1[j], collision, null, this);    game.physics.arcade.collide(player, pipes2[j], collision, null, this);  }  if ((timer/200) % 1 == 0) {    var i = Math.floor(Math.random() * valuesForPipes1.length);    var pipe1 = walls.create(width, valuesForPipes1[i], 'pipe1');    var pipe2 = walls.create(width, valuesForPipes2[i], 'pipe2');    pipe1.body.immovable = true;    pipe2.body.immovable = true;    pipes1.push(pipe1);    pipes2.push(pipe2);  }  if (cursors.left.isDown) {    player.body.velocity.x = -150;    player.loadTexture('htrad');  }  else if (cursors.right.isDown) {    player.body.velocity.x = 150;    player.loadTexture('darth');  }}

And one more quastion: in collusion callback function I can't use alert function. Why?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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