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

Raylib代码报错error: 'ballPosition' undeclared,求解决方法

解决Raylib中'ballPosition' undeclared报错问题

报错原因

你直接使用了ballPosition变量,但未提前声明它的类型与名称。GetMousePosition()返回的是Raylib定义的Vector2结构体类型,必须先声明对应类型的变量才能使用。

另外代码还有个小问题:ClearBackground(RAYWHITE)要放在DrawCircleV之前,否则绘制的圆会被背景覆盖,无法显示。

修改后的完整代码

#include"raylib.h"

int main()
{
    const int screenWidth = 800;
    const int screenHeight = 450;
    // 声明Vector2类型的变量并初始化
    Vector2 ballPosition = { 0.0f, 0.0f };

    InitWindow(screenWidth,screenHeight, "ArcadeGame");
    
    while(!WindowShouldClose())
    {
        ballPosition = GetMousePosition();
        BeginDrawing();
            ClearBackground(RAYWHITE); // 先清除背景再绘制元素
            DrawCircleV(ballPosition, 35, DARKBLUE);
        EndDrawing();
    }
    
    CloseWindow();
    
    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 18:22:48