Jump to content

Platformer collision problem


Ulbast
 Share

Recommended Posts

Hi guys!

I'm new to Phaser, with basic knowledge of JS. I have a problem with my platform game, which I've started with this tutorial.

https://phaser.io/news/2015/09/platformer-tutorial-part2

The code is different from every tutorial I've seen on Phaser main webpage, so It's hard for me to get in to. (no update, create, preload function in main is the beggining of all my problems)...

I wanted to add a shooting system, which I learned from this:

https://phaser.io/examples/v2/arcade-physics/group-vs-group

I applied this code to the platformer engine, and I am not sure if I did it correctly. I have created shooting system in Player object, because I didn't know how to do it other way. This code look like this:

//In player:
Platformer.Player = function (game_state, position, properties) {
    "use strict";
    Platformer.Prefab.call(this, game_state, position, properties);

(...)

//bullets
bullets = game.add.group();

    bullets.enableBody = true;
    bullets.physicsBodyType = Phaser.Physics.ARCADE;
    bullets.callAll('events.onOutOfBounds.add', 'events.onOutOfBounds', resetBullet, this);
    bullets.setAll('checkWorldBounds', true);
        bullets.createMultiple(100, 'pocisk');

    
    function resetBullet (bullet) {

    bullet.kill();
}

//In player.prototype.update :
Platformer.Player.prototype = Object.create(Platformer.Prefab.prototype);
Platformer.Player.prototype.constructor = Platformer.Player;

Platformer.Player.prototype.update = function () {
    "use strict";
    this.game_state.game.physics.arcade.collide(this, this.game_state.layers.collision);
    this.game_state.game.physics.arcade.overlap(this, this.game_state.groups.enemies, this.hit_enemy, null, this);

(...)

//bullets

 if (this.spaceKey.isDown) {
        this.fireBullet();
    }
    
    this.fireBullet = function() {

    if (game.time.now > bulletTime)
    {
        bullet = bullets.getFirstExists(false);
        

        if (bullet) 
        {
            
            this.game_state.game.physics.arcade.overlap(bullet, Platformer.Enemy, this.zderzenie, null, this);
            
            
            bullet.reset(this.body.x + 6, this.body.y - 8);
             bullet.body.allowGravity = false;
            
            if (this.profil==1) //direction of shooting
            bullet.body.velocity.x = 300;
            else
               bullet.body.velocity.x = -300;
            
            
            if(bullet.body.x==Platformer.Enemy.x && bullet.body.y==Platformer.Enemy.y)
                bullet.kill();
            
            bulletTime = game.time.now + 250;
        }
    }
    }

And it works, the player is shooting circles in left or right direction.

The problem starts now. I need to somehow code the collision between the bullet and the enemy. As you can see above, I wrote thing like this :

            if(bullet.body.x==Platformer.Enemy.x && bullet.body.y==Platformer.Enemy.y)
                bullet.kill();

But this don't work.

What can I do? It is the problem with seperate objects? Or maybe I know nothing and made serious stupid mistakes? :D

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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