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

JavaFX开发:点击按钮切换其余按钮颜色(Ottlo游戏授权房屋功能)

Hey there! Let's figure out how to build this color-switching feature for your Ottlo game—super straightforward once you break down the core parts. Here's a practical, scalable implementation plan tailored to your needs:

Ottlo Game: Authorized Housing Button Color Switch Implementation

1. Core State Setup

First, you'll need to track two key pieces of state to ensure color changes don't repeat:

  • currentNonSelectedColor: Stores the color all unclicked buttons are currently using
  • previousNonSelectedColor: Keeps a record of the last color used, to enforce the "no repeat" rule

2. Define Your Color Pool

Create a list of colors you want to cycle through—make sure there are at least 2 options so you can always switch to a different one. Example in C# (Unity):

List<Color> colorPool = new List<Color> {
    Color.Gray,
    Color.SkyBlue,
    Color.Olive,
    Color.Coral,
    Color.MediumPurple
};

3. Bind Click Events to All Buttons

Link every one of your 64 buttons to the same click handler method. This keeps your code DRY and easy to maintain. For example, in Unity, you can drag all buttons into an array and assign the listener in Start().

4. The Core Click Handler Logic

Here's the meat of the implementation—this method will handle switching colors when any button is clicked, excluding the clicked one itself:

public Button[] allHousingButtons; // Assign all 64 buttons here in the inspector
private Color currentNonSelectedColor;
private List<Color> colorPool;

void Start() {
    // Initialize color pool and starting color
    colorPool = new List<Color> { Color.Gray, Color.SkyBlue, Color.Olive, Color.Coral };
    currentNonSelectedColor = colorPool[0];

    // Set initial color and bind click events
    foreach (var btn in allHousingButtons) {
        btn.image.color = currentNonSelectedColor;
        btn.onClick.AddListener(() => OnHousingButtonClicked(btn));
    }
}

void OnHousingButtonClicked(Button clickedBtn) {
    // Step 1: Pick a new color that's different from the current one
    Color newColor;
    do {
        newColor = colorPool[Random.Range(0, colorPool.Count)];
    } while (newColor == currentNonSelectedColor);

    // Step 2: Update state for next time
    currentNonSelectedColor = newColor;

    // Step 3: Apply the new color to all buttons except the clicked one
    foreach (var btn in allHousingButtons) {
        if (btn != clickedBtn) {
            btn.image.color = newColor;
        }
        // Optional: Highlight the clicked button to show it's the authorized one
        else {
            btn.image.color = Color.Red; // Use your preferred "selected" color
        }
    }
}

5. Quick Optimizations & Adaptations

  • If you only have 2 colors: Skip the loop for picking a new color—just toggle directly between the two (e.g., newColor = currentNonSelectedColor == colorPool[0] ? colorPool[1] : colorPool[0])
  • For non-Unity frameworks: The logic translates easily. In Godot, use Button.modulate for color control and bind the pressed signal. In JS/HTML, modify element.style.backgroundColor and loop through DOM elements.
  • Sequential color cycling instead of random: Replace the random pick with an index tracker—e.g., currentColorIndex = (currentColorIndex + 1) % colorPool.Count, then check if it matches the current color and increment again if needed.

This setup will reliably let players click any housing button, with all other buttons switching to a fresh, non-repeating color—perfect for your authorization system!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:21:14