精灵不会与2个不同的阵列碰撞,只有一个

时间:2019-05-26 15:33:45

标签: java html phaser-framework

我目前正在制作一个小型平台游戏游戏,其中玩家可以无限跳跃,就像android游戏“ Abduction”一样。
我设法在一个定时间隔的随机位置上创建了平台,并将其添加到数组中。
但是我只能使播放器精灵与其中一个平台数组(即this.walls数组)碰撞我已经做好了准备,因此我为播放器设定了起点。
有谁知道问题可能在哪里。 顺便说一下,丑陋的代码警报,我对此很陌生。

我已经检查了update:函数,以查看是否忘记在此添加一些东西。香港专业教育学院拼写检查了代码和各种各样的东西,但我完全不知所措。

var playState = {
preload: function () {

},

create: function () {
    game.add.image (0, 0,'bg');
    game.physics.startSystem(Phaser.Physics.ARCADE);
    game.renderer.renderSession.roundPixels = true;

    this.cursor = game.input.keyboard.createCursorKeys();

    game.input.keyboard.addKeyCapture(
        [Phaser.Keyboard.UP, Phaser.Keyboard.DOWN, Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT]);

    this.wasd = {
        up: game.input.keyboard.addKey(Phaser.Keyboard.W), left: game.input.keyboard.addKey(Phaser.Keyboard.A), right: game.input.keyboard.addKey(Phaser.Keyboard.D)
    };


    this.player = game.add.sprite(game.width/2, game.height/1.2, 'player'); 
    this.player.anchor.setTo(0.5, 0.5);
    game.physics.arcade.enable(this.player);
    this.player.body.gravity.y = 800;

   this.scoreLabel = game.add.text(30, 30, 'score: 0', 
        { font: '18px Arial', fill: '#ffffff' });
    game.global.score = 0;

    game.time.events.loop(1000, this.updateScore);
    game.time.events.loop(4000, this.addPlatform);

    this.createWorld();



},


 update: function () {
    game.physics.arcade.collide(this.player, this.walls);
    game.physics.arcade.collide(this.player, this.platforms);



    if (!this.player.inWorld) {
        this.playerDie();
    }

     this.movePlayer();


},
    updateScore: function(){
        this.score +=1;
        game.global.score +=1;
        //fix this at some point 

    },


createWorld: function() {
    this.walls = [];

    this.walls.push(game.add.sprite(170, 750, 'platform', 0));
    this.walls.push(game.add.sprite(70, 650, 'platform', 0));
    this.walls.push(game.add.sprite(320, 550, 'platform', 0));
    this.walls.push(game.add.sprite(200, 450, 'platform', 0));
    this.walls.push(game.add.sprite(10, 550, 'platform', 0));
    this.walls.push(game.add.sprite(70, 350, 'platform', 0));
    this.walls.push(game.add.sprite(310, 300, 'platform', 0));
    this.walls.push(game.add.sprite(230, 200, 'platform', 0));
    this.walls.push(game.add.sprite(100, 100, 'platform', 0));
    this.walls.push(game.add.sprite(270, 0, 'platform', 0));




    for (var x=0; x< this.walls.length; x++) {
        game.physics.arcade.enable(this.walls[x]);
        this.walls[x].enableBody = true;
        this.walls[x].body.immovable = true;
        this.walls[x].body.velocity.y = 20;

        } 

},
addPlatform: function() {
    this.platforms = [];

    this.platforms.push(game.add.sprite(game.rnd.integerInRange(0, 350),-20,'platform',0));


    for (var x=0; x< this.platforms.length; x++) {
        game.physics.arcade.enable(this.platforms[x]);
        this.platforms[x].enableBody = true;
        this.platforms[x].body.immovable = true;
        this.platforms[x].body.velocity.y = 20;

    }
},




movePlayer: function() {
    if (this.cursor.left.isDown || this.wasd.left.isDown) {
        this.player.body.velocity.x = -250;
    }
    else if (this.cursor.right.isDown || this.wasd.right.isDown) {
        this.player.body.velocity.x = 250;
    }
    else { 
        this.player.body.velocity.x = 0;
    }

    if ((this.cursor.up.isDown || this.wasd.up.isDown)
        && this.player.body.touching.down){
        this.player.body.velocity.y = -450;
    }
},
playerDie: function() {
    this.player.kill();

    //this.deadSound.play();
    //this.emitter.x = this.player.x;
    //this.emitter.y = this.player.y;
    //this.emitter.start(true, 800, null, 15);
    game.time.events.add(1000, this.startMenu, this);
    game.camera.shake(0.02, 300);
},

startMenu: function() {
    game.state.start('menu');
},



};

this.walls是我建立平台的起点,而this.platforms是随机生成的平台。

  

我希望能够真正跳上那些平台,而不仅仅是   失败了,我目前正在这样做

2 个答案:

答案 0 :(得分:0)

仅应在创建函数中创建数组:

this.platforms = [];

但是似乎您在addPlatforms函数中一次又一次地创建了this.platforms数组,这意味着您有多个具有该名称的数组。取而代之的是,删除该行并仅创建新元素并将其推入数组:

addPlatform: function() {
//this.platforms.push(game.add.sprite(game.rnd.integerInRange(0, 350),-20,'platform',0));

// better way
this.platforms.create(game.rnd.integerInRange(0, 350),-20,'platform',0));
}

答案 1 :(得分:0)

您的平台是一个sprite数组,因此您必须解析数组中的每个sprite。

或者,您可以创建一个“ Group”对象,而不是普通数组。您可以为每个平台制作一个Group,然后与该Group和玩家进行碰撞。