Sign in to follow this  
P4nch0

Problem with collisione player and group of objects

Recommended Posts

Please help me with this collision in phaser.

I have defined player:
 

   if (zmiennastart != 0){

    this.player = this.game.add.sprite(parseInt(playerx), parseInt(playery), 'player', 5);

    
    }
    
    else
    {this.player = this.game.add.sprite(parseInt(playerx), parseInt(playery), 'player', 5);}                                                                    
     
     
    

    this.game.physics.arcade.enable(this.player );
 
    this.player.anchor.set(.5);
    this.player.scale.x=70/100;
    this.player.scale.y=65/100;
    this.player.body.setSize(4, 25, 0, 15);

     //this.game.debug.body(this.player);

    
    //the camera will follow the player in the world
    this.game.camera.follow(this.player);
      

    
     this.physics.startSystem(Phaser.Physics.Arcade);    

I have objects created in TIlededitor i objectsLayer. Objects have added type in program "item"

And there is define group "this.items". Objects are adding by type "item".

 

  createItems: function() {
    //create items  
    this.items = this.game.add.group();
    this.items.enableBody = true;

    var item; 
      
    result = this.findObjectsByType('item', this.map, 'objectsLayer');
    result.forEach(function(element){
      this.createFromTiledObject(element, this.items);
          
      
    }, this); 
   

    
  },

 

I forgot to add, this is function creating sprite to objects:

  //find objects in a Tiled layer that containt a property called "type" equal to a certain value
  findObjectsByType: function(type, map, layer) {
    var result = new Array();
    map.objects[layer].forEach(function(element){
      if(element.properties.type === type) {
        //Phaser uses top left, Tiled bottom left so we have to adjust
        //also keep in mind that the cup images are a bit smaller than the tile which is 16x16
        //so they might not be placed in the exact position as in Tiled
        element.y -= map.tileHeight;
        result.push(element);
      }      
    });
    return result;
  },
  //create a sprite from an object
  createFromTiledObject: function(element, group) {
    var sprite = group.create(element.x, element.y, element.properties.sprite);

      //copy all properties to the sprite
      Object.keys(element.properties).forEach(function(key){
        sprite[key] = element.properties[key];
      });
  },

 

There is collisione before player and groups.

    this.game.physics.arcade.overlap(this.player, this.items, this.funkcjaitem, null, this);

 

Player is colliding with objects, but passes through. Can anyone help me to do this collision that player collide with objects but cant pass through them?

I will be very gratefull. :)

 

Share this post


Link to post
Share on other sites

OMG, that was easy, thanks :)

Ok, now player is colliding with objects, but obects are movable.

When i try to meke it immovable:

  createItems: function() {
    //create items  
    this.items = this.game.add.group();
    this.items.enableBody = true;
    this.items.body.immovable = true;
    var item; 
      
    result = this.findObjectsByType('item', this.map, 'objectsLayer');
    result.forEach(function(element){
      this.createFromTiledObject(element, this.items);
         
      
    }, this); 
   
    
    
  },

I have that wrong:

TypeError: this.items.body is undefined

TypeError: this.items.body is undefined

Do You know why?

Share this post


Link to post
Share on other sites
  createItems: function() {
    //create items  
    this.items = this.game.add.group();
    this.items.enableBody = true;

    //this.items.body.immovable = true;

    this.items.setAll('body.immovable', true);


    var item; 
      
    result = this.findObjectsByType('item', this.map, 'objectsLayer');
    result.forEach(function(element){
      this.createFromTiledObject(element, this.items);
         
      
    }, this); 
   
    
    
  },

Since it's a group you need to use setAll to change the values of all members of the group.

Share this post


Link to post
Share on other sites

Man, thans a lot for You help, but still nothing.. Now it looks that:
 

  createItems: function() {
    //create items  
    this.items = this.game.add.group();
    this.items.enableBody = true;

    //this.items.body.immovable = true;

    this.items.setAll('body.immovable', true);
    this.items.setAll('body.moves', false);

    var item; 
      
    result = this.findObjectsByType('item', this.map, 'objectsLayer');
    result.forEach(function(element){
      this.createFromTiledObject(element, this.items);
         
      
    }, this); 
   
    
    
  },

 

Share this post


Link to post
Share on other sites

Try it like this

createItems: function() {
    //create items  
    this.items = this.game.add.group();
    this.items.enableBody = true;

    var result = this.findObjectsByType('item', this.map, 'objectsLayer');
    result.forEach(function(element){
      this.createFromTiledObject(element, this.items);
    }, this); 
   
    this.items.setAll('body.immovable', true);
    this.items.setAll('body.moves', false);

    
  },

You were first changing the body then creating them. This was the order is correct as you create them from tiles then changed their body attributes.

You also seem to not use 

var item;

So it might be safe to delete that also, unless you have more code in that function, and I added var in front of result as that seems to be a local variable.

Share this post


Link to post
Share on other sites

Yeeeh, You are right! Man thanks a lot, nice to see helpfull person who know a lot of phaser :)

My project is still creating, i have done a lot but i am still learning java script, fine to know about anyone can help.

Thanks! :)

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.