Jump to content

Only first object in groups have body


cosmic jet lag
 Share

Recommended Posts

Hi!

 

I've been working with Phaser for a few months now, but this is a problem I'm having that I can't seem to find or figure out a solution to -- I can create multiple objects in a group, but only one of them ever seems to have body or do what I write the function for. As in, the function runs correctly, but only on one object. I have this for basically every project I ever try, and I've looked at a bunch of docs and tutorials and forum posts. I can't get any farther in any of my projects without fixing this, which probably goes without saying.

 

The weirdest part is that it says there's an overlap in the console, but it doesn't do the thing that it's supposed to besides saying the overlap occurs (so, for example, bouncing off the tree -- there are other things that I had commented out, such as having text appear, but for the purpose of this example, we'll just use the bouncing off of a tree). When I change the overlap detection to treeGroup or houseGroup, it just makes me bounce off of the ENTIRE area from the X coordinate of the first object on (so, say a tree starts at 20, 20, I can only go so far as 20). Halp?

 

The entirety of my uncommented code is below, since I'm not entirely sure where the problem is.

var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render});//SPRITESvar tree;var treeGroup;var player;var houseGroup;var house;var background;//KEYBOARD/DIRECTIONSvar cursors;//TEXTvar text;var style;function preload () {//backgroundgame.load.image('background', 'assets/image/background.png'); console.log("town has been settled");//playergame.load.spritesheet('player', 'assets/image/player.png', 16, 16);console.log("PC has been born");//trees and housesgame.load.image('tree', 'assets/image/tree.png');game.load.image('house', 'assets/image/house.png');console.log("infrastructure has developed");}function create () {  //ENABLE PHYSICS  game.physics.startSystem(Phaser.Physics.ARCADE);  //CURSORS  cursors = game.input.keyboard.createCursorKeys();  //BACKGROUND  background = game.add.tileSprite(0, 0, 800, 600, 'background');  //TREES - insert at various points  treeGroup = game.add.group();  treeGroup.enableBody = true;  for (var i = 0; i < 10; i++){    tree = treeGroup.create(game.world.randomX, game.world.randomY, 'tree');    game.physics.arcade.enableBody(tree);    tree.body.immovable = true;  }  //HOUSES - insert at various points  houseGroup = game.add.group();  houseGroup.enableBody = true;  for (var i = 0; i < 7; i++){    house = houseGroup.create(game.world.randomX, game.world.randomY, 'house');  }  //PLAYER SPRITE  player = game.add.sprite(0, 200, 'player');  game.physics.arcade.enable(player);  player.body.collideWorldBounds = true;  player.body.setSize = (0, 0, 32, 32);  player.scale.set(2, 2);  player.body.bounce.setTo(0.25, 0.25);  //...and animations  player.animations.add('move', [1, 1, 2, 2, 3, 3, 4, 4], 20, true);  player.animations.add('left', [5, 5, 6, 6, 7, 7, 8, 8], 20, true);      }function update(){  game.physics.arcade.overlap(player, house);  game.physics.arcade.overlap(player, tree);  game.physics.arcade.collide(player, tree);  game.physics.arcade.collide(player, house);  player.body.velocity.x = 0;  player.body.velocity.y = 0;  if(cursors.up.isDown){    player.body.velocity.y = -125;    player.animations.play('move');  } else if (cursors.down.isDown) {    player.body.velocity.y = 125;    player.animations.play('move');  } else if (cursors.left.isDown) {    player.body.velocity.x = -125;    player.animations.play('left');  } else if (cursors.right.isDown) {    player.body.velocity.x = 125;    player.animations.play('move')  } else {    player.animations.stop;    player.frame = 0;  }  if(checkOverlap(player, house)){    console.log("overlap is true");  } else {    console.log("no overlap, pls fix");    //do something else here }  if (checkOverlap(player, tree)){    console.log("do not walk into trees");    //make it bounce off:    player.body.velocity.y = -150;    player.body.velocity.x = -150;  }}function render(){  game.debug.body(tree);  game.debug.body(house);}function checkOverlap(player, house){  var boundsA = player.getBounds();  var boundsB = house.getBounds();  return Phaser.Rectangle.intersects(boundsA, boundsB);}function checkOverlap(player, tree){  var boundsA = player.getBounds();  var boundsB = tree.getBounds();  return Phaser.Rectangle.intersects(boundsA, boundsB);}
Link to comment
Share on other sites

Pretty sure your problem is here:

game.physics.arcade.overlap(player, house);game.physics.arcade.overlap(player, tree);game.physics.arcade.collide(player, tree);game.physics.arcade.collide(player, house);

You want "treeGroup" and "houseGroup", otherwise you are only colliding with the last sprite that was created in those loops in your create method. Also, you don't need to call "overlap" *and* "collide".

 

Call "overlap" if you want to know if the two things are overlapping but don't want them to separate (bounce off each other). Call "collide" if you want to know when two things are overlapping *AND* you want them to separate. You usually don't need to do both.

 

"overlap" and "collide" both accept callbacks as their third arguments that will be called when sprites overlap and collide respectively. Here is the documentation for collide: http://phaser.io/docs/2.4.4/Phaser.Physics.Arcade.html#collide

Link to comment
Share on other sites

Hey, thanks for the reply! I've actually tried the houseGroup and treeGroup things before -- that's when it makes me unable to move past the X boundary of any object in the ENTIRE group, which is why I got confused. But I also changed the checkOverlap accordingly. Should the checkOverlap stay as it is and then overlap/collide change, do you think?

 

Thanks for the info on overlap and collide, I'd been confused about that before!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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