访问GML中全局数组的索引
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就像史蒂文(Steven)所说的
global.map
是一个单个数组,但您正在访问它,就好像它是二维数组一样。对您的原始代码进行一些较小的调整,可以使其像这样绘制一个网格:然后,您将评估
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: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 correctlydraw_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.
您的
global.map
只是一个数组,但是您的代码:正在处理数组,好像它是多维数组一样。
因此,尝试将
global.map
转换为多维数组(或2D数组)。或者,更改调用
global.map
的代码,该代码仅在一个数组中读取。我没有在游戏制造商中使用2D阵列练习,所以不幸的是,我不能举例说明。
Your
global.map
is just a single array, but your code: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.