Jump to content

Unexpected behavior with Group


PsychoGoldfish
 Share

Recommended Posts

I'm having some unexpected behavior using groups in my Phaser game.  I'm using a Tilemap to build my world, and a Texture Atlas for my characters and objects.

 

First I render my tilemap and then I add a group to the game, and add a couple of sprites to the group.  This is where things go awry.

 

The door sprite I add has no physics enabled, so it seems to get added on top of the game and just kid of floats there.

 

The player sprite I add has physics enabled and is tracked by the camera, but it appears to be quite offset.  It seems like the group isn't being created in the world and it's probably something glaringly simple I am overlooking, but I just can't figure this out.  

 

If I add the sprites to the game directly, vs using the group, they work fine.

 

Here's what my state code looks like:

 

{
  preload: function () {
    this.load.atlas('characters', 'assets/characters.png', 'assets/characters.json');
    this.load.spritesheet('sheet1','assets/tiles.png',64,64,-1,1,2);
    this.load.tilemap('lvl_1-1','assets/lvl_1-1.json', null, Phaser.Tilemap.TILED_JSON);

    this.physics.startSystem(Phaser.Physics.ARCADE);
    this.physics.arcade.gravity.y = 800;

    this.physics.arcade.TILE_BIAS = 40;
  },
  create: function () {

    this.map = this.game.add.tilemap('lvl_1-1');
    this.map.addTilesetImage('tiles', 'sheet1');

    this.ground_layer = this.map.createLayer('Ground');
    this.ground_layer.resizeWorld();

    this.map.setCollisionByExclusion([],true,this.ground_layer);

    this.player = new Phaser.Sprite(this.game, 64, 408, 'characters', 1);
    this.game.physics.arcade.enableBody(this.player);

    door = new Phaser.Sprite(this.game, 64, 160, 'characters', 0);

    /** ================ Method 1: this doesn't work as expected =================== **/
    characters_group = this.game.add.group(this.game, 'characters_group', true);
    characters_group.add(door); // door is added like an overlay, does not move with camera
    characters_group.add(this.player); // player moves as expected, but falls off-camera
    /** ============================================================================ **/

    /** =================== Method 2: this works just fine ========================= /
    this.game.add.existing(door);
    this.game.add.existing(this.player);
    /** ============================================================================ **/

    this.game.camera.follow(this.player, Phaser.Camera.FOLLOW_PLATFORMER);

    this.cursors = this.game.input.keyboard.createCursorKeys();
  },

  update: function () {

    this.physics.arcade.collide(this.player, this.ground_layer);

    if (this.cursors.left.isDown) {
      this.player.body.velocity.x = -200;
    } else if (this.cursors.right.isDown) {
      this.player.body.velocity.x = 200;
    } else {
      this.player.body.velocity.x = 0;
    }

    if (this.player.body.blocked.down && this.cursors.up.isDown) {
      this.player.body.velocity.y = -900;
    }

  }

}

 

I attached some screenshots showing what's happening.  Any help would save me a lot of Tylenol!

post-10949-0-38681800-1414180426.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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