【问题标题】:Phaser.js Drag and drop ProblemsPhaser.js 拖放问题
【发布时间】:2015-08-19 19:27:46
【问题描述】:

我正在尝试制作一个匹配游戏,其中您有 4 个精灵,其中 3 个精灵上有东西,而另一个精灵有类似 anwnser,例如,问题可能是分数和小数的匹配,您将有 3 个分数或 3 个小数点,其中一个作为正确的 awnser 在您与正确的精灵匹配的另一个精灵上。但我的问题是我无法让精灵欺骗它们正在碰撞。这是我目前所拥有的。

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var game = new Phaser.Game(800, 600, Phaser.AUTO, '', {preload: preload, create: create, update: update});

function preload() {
    game.load.image('Cone','pics/ICE-Cream-cone_1.png');
    game.load.image('back','pics/BakerIceBackground_1.png');
    game.load.image('Shoot','pics/CreamShoot.png');
    game.load.image('star','pics/star.png');
}

var cone;
var text;

function create() {    
    back = game.add.sprite(0, 0, 'back');
    s1 = game.add.sprite(500, 250, 'Shoot');
    s2 = game.add.sprite(280, 250, 'Shoot');
    s3 = game.add.sprite(60, 250, 'Shoot');
    cone = game.add.sprite(300, 400, 'Cone');
    cone.inputEnabled = true;
    cone.input.enableDrag();

    game.physics.enable(cone, Phaser.Physics.ARCADE);

    var style = {font: "32px comic sans", fill :"#ff0044", wordwrap: true, wordWrapWidth: cone.width,align:"center"};
    text = game.add.text(0,0, "test", style);
    text.anchor.set(0.5);
}

function update() {       
    text.x = Math.floor(cone.x + cone.width / 2);
    text.y = Math.floor(cone.y + cone.height / 2);
    game.physics.arcade.overlap(cone,s1,collisionHandler,null,this);
    //CX = s2.x;
    //CY = s2.y;
    // console.log(CY);
    // console.log(CX);

    function collisionHandler() {
        console.log("game Over");
    }   
}

【问题讨论】:

    标签: javascript phaser-framework


    【解决方案1】:

    create 函数中启用 s1 game.physics.arcade.enable(s1); 的物理 game.physics.arcade.overlap(cone,s1,collisionHandler,null,this);update 函数的请求下执行此操作。你也可以使用 game.physics.arcade.collide(//your params);

    这里的一些链接可能会有所帮助 - detect collision and overlap

    overlap with a scaled sprite

    【讨论】:

    • 好的,也谢谢我如何切换关卡,因为关于如何切换和添加关卡并没有什么好的教程。
    • 你可以在Phaser中使用state切换关卡首先添加game.state.add("BossLevel",bossLevel);然后启动状态 game.state.start('BossLevel');您可以在示例部分的 Phaser 主页中找到更多关于此的内容。
    • 那里的教程不是很好,对我来说有点难以理解,游戏状态部分。
    猜你喜欢
    • 2018-03-09
    • 2011-06-27
    • 2023-04-10
    • 2015-08-27
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多