【问题标题】:How to display a sliding animation in my endless runner using Phaser 3如何使用 Phaser 3 在我的无尽跑步者中显示滑动动画
【发布时间】:2022-07-12 12:56:55
【问题描述】:

我正在尝试用 Phaser 3 制作一个无尽的跑步游戏。我让他跑和跳,但我不知道如何让他滑行(我想是因为动画 Run 在更新功能中不断运行) 有没有办法让他滑几秒钟然后重新开始玩。请任何建议或答案将非常需要和接受。谢谢。

【问题讨论】:

    标签: javascript infinite-scroll phaser-framework


    【解决方案1】:

    如果您使用arcade 物理引擎,则可以使用setAccelerationX。当按键不再按下时,播放器可以滑动。精灵将“滑动”多少,将取决于您设置的drag (预期摩擦)Documentation (在此文档页面上,您可以找到更多信息到accelerationdrag 和其他使用的方法和/或属性)

    这里有一个小演示,展示了这个:

    var config = {
        type: Phaser.AUTO,
        width: 400,
        height: 160,
        physics: {
            default: 'arcade',
            arcade: {
                gravity: { y: 100 },
            }
        },
        scene: {
            create,
            update
        }
    }; 
    
    var cursors;
    var player;
    var playerStateText;
    
    function create () {
        cursors = this.input.keyboard.createCursorKeys();
        playerStateText = this.add.text(10,10, 'Playerstate: ???', {color: '#ffffff'});
        
        let ground = this.add.rectangle(-40, 120, 480, 50, 0xBAF0FF).setOrigin(0);
        player = this.add.rectangle(20, 20, 30, 30, 0xcccccc).setOrigin(0);
        
        ground = this.physics.add.existing(ground);
        ground.body.setImmovable(true);
        ground.body.allowGravity = false;
        
        player = this.physics.add.existing(player);
        
        // Just to be sure that the player doesn't get too fast
        player.body.setMaxSpeed(160); 
        
        // Tweak this value to define how far/long the player should slide
        player.body.setDrag(120, 0);
        
        this.physics.add.collider(player, ground);      
    }
    
    function update (){
        let currentState = 'Playerstate: running';
        if (cursors.left.isDown){
            player.body.setAccelerationX(-160);
        } else if (cursors.right.isDown) {
            player.body.setAccelerationX(160);
        }
        else {
            player.body.setAccelerationX(0);
            if(Math.abs(player.body.velocity.x) > 3) {
               currentState = 'Playerstate: sliding';
            } else if(Math.abs(player.body.velocity.y) > 3) {
               currentState = 'Playerstate: falling';
            } else {
               currentState = 'Playerstate: stopped';
            }
        }
        
        if(player.x > 400){
           player.x = -20;
        }
        
        if(player.x < -20){
           player.x = 400;
        }
        
        playerStateText.setText(currentState);
    }
    
    new Phaser.Game(config);
    &lt;script src="//cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"&gt;&lt;/script&gt;

    然后您可以根据当前玩家的动作、速度或其他属性设置正确的动画。

    更新:

    如果你只是想展示不同的动画,没有任何特殊的交互或功能,你可以像这个例子中解释的那样链接动画:http://phaser.io/examples/v3/view/animation/chained-animation

    只需链接 RunSlide 动画,然后它们将始终以正确的顺序播放而无需更改您的代码。如果你想在滑动过程中改变速度,那就有点工作了。

    并且您可以结帐/使用events of animation,以便您可以对动画执行操作:开始、停止、完成等等... .

    例如:如果玩家在着陆后应该滑动,它可能看起来像这样:

      player.on(Phaser.Animations.Events.ANIMATION_COMPLETE_KEY + 'jump', function (anims) {
           player.chain([ 'slide', 'run' ]);
       }, this);
    

    【讨论】:

    • 我很高兴你提出了这个建议,毫无疑问它不起作用,但我正在努力使其可用于移动设备并且无法使用光标。有没有其他方法可以让它在移动设备上运行。
    • @AceEnyioko 确定你可以使用pointerdownpointerup 左右,但是如果不知道你的代码(结构),很难说最好的方法是什么
    • 取决于你的游戏机制
    • 是的,我可以使用它,但我也有跳跃动画来描述它的运动。
    • @AceEnyioko 在你原来的问题中你说 "...有没有办法让他滑几秒钟然后重新开始玩..." 你怎么办这是什么意思?比赛结束后?跳后?玩家应该什么时候滑动?
    【解决方案2】:

    也许您可以使用“let som=true; if(som==true){runnerfunction()}”,而当滑动按钮将使用“som=false”时

    【讨论】:

    • 我已经使用了布尔值并设置了它的功能,它在滑动方面工作得很好,尽管不像我预期的那样。当我跳跃时,会在跳跃的间隔播放滑动动画。我已经扫描了我的代码,但仍然不知道出了什么问题。如果您可能需要知道我设置了一个设置超时功能来播放滑动动画几秒钟然后返回运行动画(不知道是不是问题)。
    • 滑动动画和跳跃可能也需要一个布尔变量
    猜你喜欢
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    相关资源
    最近更新 更多