移动物体碰撞 Phaser

Moving objects collision Phaser

我的移动物体碰撞不起作用。我已经创建了一个演示,所以你可以看到它并看到我的问题所有代码和所有内容。

你可能会看到,我有 2 个方块从左到右,然后我有一个坦克在发射子弹> 我尝试了所有方向,我总是得到相同的结果,基本上只有我的碰撞工作块值的速度,在 zip 文件的例子中只工作到 300px。根据块速度,如果我将块速度更改为更大的数字,那么碰撞将在相同数量的像素上起作用。这真的很奇怪。

我想知道我是不是整件事都做错了,或者我的问题是什么?我将不胜感激任何可以帮助我解决此问题的指示或想法。谢谢

Demo source code.

BasicGame.Game = function (game) {

    //  When a State is added to Phaser it automatically has the following properties set on it, even if they already exist:

    this.game;      //  a reference to the currently running game (Phaser.Game)
    this.add;       //  used to add sprites, text, groups, etc (Phaser.GameObjectFactory)
    this.camera;    //  a reference to the game camera (Phaser.Camera)
    this.cache;     //  the game cache (Phaser.Cache)
    this.input;     //  the global input manager. You can access this.input.keyboard, this.input.mouse, as well from it. (Phaser.Input)
    this.load;      //  for preloading assets (Phaser.Loader)
    this.math;      //  lots of useful common math operations (Phaser.Math)
    this.sound;     //  the sound manager - add a sound, play one, set-up markers, etc (Phaser.SoundManager)
    this.stage;     //  the game stage (Phaser.Stage)
    this.time;      //  the clock (Phaser.Time)
    this.tweens;    //  the tween manager (Phaser.TweenManager)
    this.state;     //  the state manager (Phaser.StateManager)
    this.world;     //  the game world (Phaser.World)
    this.particles; //  the particle manager (Phaser.Particles)
    this.physics;   //  the physics manager (Phaser.Physics)
    this.rnd;       //  the repeatable random number generator (Phaser.RandomDataGenerator)

    //  You can use any of these from any function within this State.
    //  But do consider them as being 'reserved words', i.e. don't create a property for your own game called "world" or you'll over-write the world reference.

     this.bulletTimer = 0;

};

BasicGame.Game.prototype = {

    create: function () {

                            //Enable physics

// Set the physics system
            this.game.physics.startSystem(Phaser.Physics.ARCADE);


        //End of physics 


        //  Honestly, just about anything could go here. It's YOUR game after all. Eat your heart out!

         this.createBullets();
         this.createTanque();

        this.timerBloques = this.time.events.loop(1500, this.createOnebloque, this);

    },

    update: function () {


        if(this.game.input.activePointer.isDown){
            this.fireBullet();
        }

       this.game.physics.arcade.overlap(this.bullets, this.bloque, this.collisionBulletBloque, null, this);

    },

    createBullets: function() {

    this.bullets = this.game.add.group();
    this.bullets.enableBody = true;
    this.bullets.physicsBodyType = Phaser.Physics.ARCADE;
    this.bullets.createMultiple(100, 'bulletSprite');
    this.bullets.setAll('anchor.x', 0.5);
    this.bullets.setAll('anchor.y', 1);
    this.bullets.setAll('outOfBoundsKill', true);
    this.bullets.setAll('checkWorldBounds', true); 


    },

    fireBullet: function(){ 

   if (this.bulletTimer < this.game.time.time) {
            this.bulletTimer = this.game.time.time + 1400;
            this.bullet = this.bullets.getFirstExists(false);
        if (this.bullet) {
            this.bullet.reset(this.tanque.x, this.tanque.y - 20);
            this.game.physics.arcade.enable(this.bullet);
        this.bullet.enableBody = true;
            this.bullet.body.velocity.y = -800;
            }
        }

    },

    createOnebloque: function(){


        this.bloquecs = ["bloqueSprite","bloquelSprite"];
        this.bloquesr = this.bloquecs[Math.floor(Math.random()*2)];
        this.bloque = this.add.sprite(0, 360, this.bloquesr);
        this.game.physics.arcade.enable(this.bloque);
        this.bloque.enableBody = true;
        this.bloque.body.velocity.x = 300;
        this.bloque.body.kinematic = true;
        this.bloque.checkWorldBounds = true;
        this.bloque.outOfBoundsKill = true;
        this.bloque.body.immovable = true;

    },

        createTanque: function() {

        this.tanqueBounds = new Phaser.Rectangle(0, 600, 1024, 150);

        this.tanque = this.add.sprite(500, 700, 'tanqueSprite');
        this.tanque.inputEnabled = true;
        this.tanque.input.enableDrag(true);
        this.tanque.anchor.setTo(0.5,0.5);
        this.tanque.input.boundsRect = this.tanqueBounds;


    },

        collisionBulletBloque: function(bullet, bloque) {


        this.bullet.kill();

    },


    quitGame: function (pointer) {

        //  Here you should destroy anything you no longer need.
        //  Stop music, delete sprites, purge caches, free resources, all that good stuff.

        //  Then let's go back to the main menu.
        this.state.start('MainMenu');

    }

};

代码确实有帮助,但您实际上可以通过玩游戏了解正在发生的事情。

当方块经过时,如果你开火并击中最中心方块的右边缘,实际上可以让子弹消失。

发生的事情是您正在检查子弹和方块之间的碰撞,但是每次您在左侧屏幕上添加新的方块时都会重新定义方块。

您可能想要做的是创建一个方块池(例如 bloquePool),几乎与您在 createBullets 中使用子弹池所做的完全一样(您已经称为 bullets).

然后检查子弹组和方块组之间的碰撞。

this.game.physics.arcade.overlap(
    this.bullets, this.bloquePool, this.collisionBulletBloque, null, this
);

你应该得到单个子弹和碰撞的单个块传递到 collisionBulletBloque 这样你仍然可以像现在这样杀死子弹。