You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Canvas游戏中实现英雄靠近怪物时显示GIF

Solution for Showing Scare GIF When Hero Approaches Monster in Hero's Escape Game

Got it, let's work through this step by step. PHP won’t help here—this is a client-side canvas interaction, so we’ll use JavaScript to track the hero-monster distance and control when to render your scare GIF. Here’s how to implement it properly:

First, let’s add the necessary variables to manage the scare GIF state, plus fix a small scope issue in your original code:

Step 1: Add Scare GIF Control Variables

Add these variables alongside your other image/position variables in the <script> section:

// Scare GIF control
var showScareGif = false;
var scareGifTimer = 0; // Frames to keep the GIF visible
var imgScareGif = new Image();
imgScareGif.src = "Images/YourScareGif.gif"; // Replace with your actual GIF path

Step 2: Fix Function Scope

Your original Iniciar() function is nested inside AtualizaTela2(), which will cause errors when the page loads. Move it outside so it’s globally accessible:

function Iniciar(){
    objCanvas = document.getElementById("meuCanvas");
    objContexto = objCanvas.getContext("2d");
    // Start the continuous screen update loop
    requestAnimationFrame(AtualizaTela2);
}

Step 3: Update AtualizaTela2() for Scare Logic

Modify your core update function to trigger the GIF when the hero is close, and control how long it stays visible:

function AtualizaTela2(){
    // Check if hero is within 10px of the monster (your existing condition)
    if((xHero >= xMonster - 10) && (xHero <= xMonster + 10)) {
        // Activate the scare GIF and reset the timer
        showScareGif = true;
        scareGifTimer = 30; // Show for ~0.5 seconds (60fps = 60 frames/sec)
    }

    // Draw base elements first (background, hero, monster)
    objContexto.drawImage(imgFundo2, 0, 0);
    objContexto.drawImage(imgHero, xHero, yHero);
    objContexto.drawImage(imgMonster, xMonster, yMonster);

    // Render scare GIF if active
    if(showScareGif) {
        // Draw GIF to cover the entire canvas (adjust x/y/width/height for partial display)
        objContexto.drawImage(imgScareGif, 0, 0, objCanvas.width, objCanvas.height);
        scareGifTimer--;
        // Turn off GIF when timer runs out
        if(scareGifTimer <= 0) {
            showScareGif = false;
        }
    }

    // Keep the screen updating smoothly
    requestAnimationFrame(AtualizaTela2);
}

Key Details to Note:

  • Render Order: We draw the scare GIF last so it overlays all other elements—this makes the jump scare feel impactful. If you want it to only cover a section of the screen, adjust the drawImage parameters (x, y, width, height) to match your desired area.
  • Timer Adjustment: Tweak scareGifTimer to change how long the GIF stays visible. 30 frames works for a quick jump, but you can increase it to 60 for a full second, or decrease it for a flash effect.
  • Continuous Updates: requestAnimationFrame ensures your canvas refreshes smoothly every frame, which is critical for both movement and detecting the hero-monster proximity in real time.

内容的提问来源于stack exchange,提问作者Erik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:11:41