Javascript和Phaser - 无法让我的精灵碰撞

时间:2016-05-12 19:04:51

标签: javascript phaser-framework

我正在学习Phaser游戏开发。我有3个精灵,一个玩家,一个苹果和很大一部分背景(不通的地形)作为精灵。我想要的是禁止玩家像player.body.collideWorldBounds = true;一样离开地形,但是使用精灵。这是我的代码:

var movSpeed = 3;
var direccion = "derecha";
var moving = false;
var FirstState = {
    preload: function () {
        //Cargamos la imagen de fondo
        fapple.load.image('background', 'graphics/firstversion/firstversion.jpg');
        fapple.load.image('ceil', 'graphics/firstversion/ceilwall.png');
        fapple.load.image('apple', 'graphics/apple.png', 64, 64);
        //fapple.load.spritesheet('player', 'graphics/firstversion/player.png', 64, 64, 70);
        //fapple.load.spritesheet('player', 'graphics/firstversion/Patito.png', 19, 26, 2);
        fapple.load.spritesheet('player', 'graphics/firstversion/DaniCoru.png', 19, 26, 1);
    },
    create: function () {
        //
        var background = fapple.add.image(0, 0, 'background');
        background.x = 0;
        background.y = 0;
        background.height = fapple.height;
        background.width = fapple.width;
        background.smoothed = false;
        //background.visible = false;
        //Configuramos la pared del fondo (el cielo)
        ceil = fapple.add.sprite(0, 0, 'ceil');
        ceil.smoothed = false;
        ceil.x = 0;
        ceil.y = 0;
        ceil.width = fapple.width;
        ceil.height = (78.51 / 100) * fapple.height;    //El cielo mide el 78.51% de alto de la imagen original


        //al jugador lo ponemos a un porcentaje del fondo
        //Altura del mundo - altura del sprite - 22%
        var percentTop = fapple.height - (fapple.height * 22) / 100;

        manzana = fapple.add.sprite(800, percentTop, 'apple');
        manzana.height = 32;
        manzana.width = 32;
        manzana.anchor.setTo(0.5, 0.5);

        player = fapple.add.sprite(100, percentTop, 'player');
        player.anchor.setTo(0.5, 0.5);

        //player.animations.add('parado-derecha', [13,14,15], 7, true);
        //player.animations.add('caminando-derecha', [5,6,7,8,9], 7, true);
        //player.animations.add('parado-izquierda', [10,11,12], 7, true);
        //player.animations.add('caminando-izquierda', [0,1,2,3,4], 7, true);
        player.animations.add('parado-derecha', [0], 7, true);
        player.animations.add('caminando-derecha', [0], 7, true);
        player.animations.add('parado-izquierda', [0], 7, true);
        player.animations.add('caminando-izquierda', [0], 7, true);
        player.animations.play('parado-derecha');
        player.smoothed = false;

        //Activamos la física para el jugador
        fapple.physics.startSystem(Phaser.Physics.ARCADE);
        fapple.physics.arcade.enable(player);
        fapple.physics.arcade.enable(manzana);
        fapple.physics.arcade.enable(ceil);
        player.enableBody = true;
        player.physicsBodyType = Phaser.Physics.ARCADE;
        player.body.setSize(player.width, player.height, 0, 0);
        player.body.enable = true;
        ceil.enableBody = true;
        ceil.physicsBodyType = Phaser.Physics.ARCADE;
        ceil.body.enable = true;
        ceil.tint = "0xff0000";
        manzana.enableBody = true;
        manzana.body.enable = true;
        manzana.body.move = false;
        manzana.body.immovable = true;
        ceil.body.setSize(ceil.width, ceil.height, 0, 0);
        ceil.body.move = false;
        ceil.body.immovable = true;

        //No dejamos que salga de los límites del mundo
        player.body.collideWorldBounds = true;
        //añadimos funcionalidad a las flechas
        cursores = fapple.input.keyboard.createCursorKeys()

        //Texto
        var style = {
            font: "32px Arial",
            fill: "#F5DB16",
            textTransform: "uppercase",
            backgroundColor: "#ffffff",
            border: "2px solid rgba(0, 0, 0, 0.20)",
            borderRadius: "15px",
            text: "center"
        };

        text = fapple.add.text(fapple.world.centerX, fapple.world.centerY, "Encontraste la manzana!!\n Pasamos al siguiente nivel?", style);
        text.anchor.set(0.5, 0);
        text.visible = false;
    },
    update: function () {
        fapple.physics.arcade.collide(manzana, player, this.appleKill, this.appleFound, this);
        fapple.physics.arcade.collide(ceil, player);
        //Comprobamos si está pulsada la tecla
        if (cursores.right.isDown) {
            direccion = "derecha";
            player.animations.play('caminando-derecha');
            player.x += movSpeed;
            moving = true;
        }

        if (cursores.left.isDown) {
            direccion = "izquierda";
            player.animations.play('caminando-izquierda');
            player.x -= movSpeed;
            moving = true;
        }
        if (cursores.down.isDown) {
            direccion = "izquierda";
            player.animations.play('caminando-izquierda');
            player.y += movSpeed;
            moving = true;
        }
        if (cursores.up.isDown) {
            direccion = "izquierda";
            player.animations.play('caminando-izquierda');
            player.y -= movSpeed;
            moving = true;
        }

        if (!cursores.right.isDown && !cursores.left.isDown) {
            moving = false;
        }

        if (!moving) {
            if (direccion == "derecha") {
                player.animations.play('parado-derecha');
            } else {
                player.animations.play('parado-izquierda');
            }
        }
    },
    appleFound: function (manzana, player) {
        console.log("Se encontró la manzana");
        manzana.kill();
        text.visible = true;
        return true;
    },
    wallCollide: function (player, ceil) {
        console.log("asdf");
    }
}

使用此代码,播放器精灵可以通过 ceil 精灵,如何停止移动?

0 个答案:

没有答案