访问GML中全局数组的索引

发布于 2025-02-13 17:13:23 字数 1226 浏览 2 评论 0原文

我是Game Maker Studio和GML的新手,我正在尝试用网格和正方形填补边界。正方形的颜色将取决于全局数组中的索引值。我一直在范围内得到错误“变量索引[8]”

边界的创建事件是:

global.map= [0,0,0,1,1,0,0,0,
             0,0,0,1,1,0,0,0,
             0,0,0,1,1,0,0,0, 
             1,1,1,1,1,1,1,1,
             1,1,1,1,1,1,1,1,
             0,0,0,1,1,0,0,0,
             0,0,0,1,1,0,0,0,
             0,0,0,1,1,0,0,0]

for (ii=0; ii<8; ii++) {
for (jj=0; jj<8; jj++) {
    // Create the grid
instance_create_layer(x + (ii*sprite_get_width(Spr_Hoop)),
                      y + (jj*sprite_get_height(Spr_Hoop)),
                      "Instances",
                      Obj_Hoop)
    //Create the square               
instance_create_layer(x+1 + (ii*(sprite_get_width(Spr_Square)+2)),
                      y+1 + (jj*(sprite_get_height(Spr_Square)+2)),
                      "Instances",
                      Obj_Square)                 
                                  
}
}

正方形的绘制事件是:

if(global.map[Obj_Border.ii][Obj_Border.jj]) {
//if(global.map[0][0]) {    
image_blend = c_white
}
else { 
image_blend=c_black
}
draw_self()

我认为这是一个简单的修复 谢谢 安德鲁

I am new to Game maker studio and GML and I am trying to fill a border with a grid and squares. The color of the square will depend on an indexed value in a global array. I keep getting an error "Variable Index [8] out of range [0]"

the create event for the border is:

global.map= [0,0,0,1,1,0,0,0,
             0,0,0,1,1,0,0,0,
             0,0,0,1,1,0,0,0, 
             1,1,1,1,1,1,1,1,
             1,1,1,1,1,1,1,1,
             0,0,0,1,1,0,0,0,
             0,0,0,1,1,0,0,0,
             0,0,0,1,1,0,0,0]

for (ii=0; ii<8; ii++) {
for (jj=0; jj<8; jj++) {
    // Create the grid
instance_create_layer(x + (ii*sprite_get_width(Spr_Hoop)),
                      y + (jj*sprite_get_height(Spr_Hoop)),
                      "Instances",
                      Obj_Hoop)
    //Create the square               
instance_create_layer(x+1 + (ii*(sprite_get_width(Spr_Square)+2)),
                      y+1 + (jj*(sprite_get_height(Spr_Square)+2)),
                      "Instances",
                      Obj_Square)                 
                                  
}
}

And the draw event for the squares is:

if(global.map[Obj_Border.ii][Obj_Border.jj]) {
//if(global.map[0][0]) {    
image_blend = c_white
}
else { 
image_blend=c_black
}
draw_self()

I assume it is a simple fix
thank you
Andrew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄傲 2025-02-20 17:13:23

就像史蒂文(Steven)所说的global.map是一个单个数组,但您正在访问它,就好像它是二维数组一样。对您的原始代码进行一些较小的调整,可以使其像这样绘制一个网格:

// Init Global Array
global.map[0] = [0,0,0,1,1,0,0,0];
global.map[1] = [0,0,0,1,1,0,0,0];
global.map[2] = [0,0,0,1,1,0,0,0];
global.map[3] = [1,1,1,1,1,1,1,1];
global.map[4] = [1,1,1,1,1,1,1,1];
global.map[5] = [0,0,0,1,1,0,0,0];
global.map[6] = [0,0,0,1,1,0,0,0];
global.map[7] = [0,0,0,1,1,0,0,0];

// Draw global.map as a grid
for (ii=1; ii<9; ii++) {
    for (jj=1; jj<9; jj++) {
        draw_rectangle(15,30,15 + ii * 32, 30 + jj * 32, true);
        draw_text(20 + (ii-1) * 32,35 + (jj-1) * 32 , global.map[ii-1, jj-1]);
    }
}

然后,您将评估grid.map [ii,jj]等于0、1或您想要的任何值并从那里创建分支。值得注意的是,您还可以使用它来查看是否正确显示draw_text(x + 15 + ii * 32,y + 300 + jj * 32,global.map [ii,jj]);> (我确实在 https://yal.cc/r/gml/ 首先,这应该可以解决问题!)。

希望这会有所帮助。

Like Steven said your global.map is a single array but you're accessing it as if it was a two dimensional array. Making some minor adjustments to your original code you can have it draw a grid like so:

// Init Global Array
global.map[0] = [0,0,0,1,1,0,0,0];
global.map[1] = [0,0,0,1,1,0,0,0];
global.map[2] = [0,0,0,1,1,0,0,0];
global.map[3] = [1,1,1,1,1,1,1,1];
global.map[4] = [1,1,1,1,1,1,1,1];
global.map[5] = [0,0,0,1,1,0,0,0];
global.map[6] = [0,0,0,1,1,0,0,0];
global.map[7] = [0,0,0,1,1,0,0,0];

// Draw global.map as a grid
for (ii=1; ii<9; ii++) {
    for (jj=1; jj<9; jj++) {
        draw_rectangle(15,30,15 + ii * 32, 30 + jj * 32, true);
        draw_text(20 + (ii-1) * 32,35 + (jj-1) * 32 , global.map[ii-1, jj-1]);
    }
}

Then you would evaluate if grid.map[ii, jj]is equal to 0, 1, or whatever value you want and create branches from there. Notably for debugging you can also use this to see if the values displaying correctly draw_text(x + 15 + ii * 32, y + 300 + jj * 32 , global.map[ii, jj]); (I did test this in https://yal.cc/r/gml/ first so this should do the trick!).

Hopefully this helps.

游魂 2025-02-20 17:13:23

您的global.map只是一个数组,但是您的代码:

if(global.map[Obj_Border.ii][Obj_Border.jj])

正在处理数组,好像它是多维数组一样。
因此,尝试将global.map转换为多维数组(或2D数组)。

或者,更改调用global.map的代码,该代码仅在一个数组中读取。

我没有在游戏制造商中使用2D阵列练习,所以不幸的是,我不能举例说明。

Your global.map is just a single array, but your code:

if(global.map[Obj_Border.ii][Obj_Border.jj])

is addressing the array as if it's a multidimensional array.
So try converting the global.map into a multidimensional array (or 2D array).

Or alternatively, change the code that calls the global.map that it only reads out of a single array.

I've not practised with 2D arrays in game-maker, so unfortunately, I cannot give an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文