Jump to content

Platformer collision problem


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.


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:


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 = 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) {


//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);



 if (this.spaceKey.isDown) {
    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;
               bullet.body.velocity.x = -300;
            if(bullet.body.x==Platformer.Enemy.x && bullet.body.y==Platformer.Enemy.y)
            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)

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

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.

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.


  • Recently Browsing   0 members

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