我已经编写了一个碰撞检查代码并运行它,运行良好,但它只适用于一层。例如,我的代码正在获取图层 id 并获取该图层中的图块并给它们一个介于 1 和 0 之间的数字。如果为 1,则表示碰撞,否则 (0) 则没有障碍物。我的问题是我有不止一层,比如对象和对象2。但是我不能选择“objects2”层来获取id。我可以选择“对象”层。这是我的代码:
"CREATE" EVENT IN COLLISION OBJECT "obj_collision"
var lay_id = layer_get_id("Objeler");
var lay_id2 = layer_get_id("Objeler2");
var map_id = layer_tilemap_get_id(lay_id);
var map_id2 = layer_tilemap_get_id(lay_id2);
hcells = room_width / 16;
vcells = room_height / 16;
for (var yy = 0; yy < vcells; yy ++){
for (var xx = 0; xx < hcells; xx ++){
//Bu kod tile'ın integer değerini veriyor.
tile = tilemap_get(map_id, xx, yy);
if (tile <= 6){
//Hareket edebilir.
a_grid[xx,yy] = 0;
}else{
//Hareket edemez.
a_grid[xx,yy] = 1;
}
}
}
for (var yy = 0; yy < vcells; yy ++){
for (var xx = 0; xx < hcells; xx ++){
//Bu kod tile'ın integer değerini veriyor.
tile2 = tilemap_get(map_id2, xx, yy);
if (tile2 <= 6){
//Hareket edebilir.
a_grid2[xx,yy] = 0;
}else{
//Hareket edemez.
a_grid2[xx,yy] = 1;
}
}
}
STEP EVENT IN CHARACTER OBJECT "obj_karakter"
#region MOVEMENT
gridX = floor(x/16);
gridY = floor(y/16);
if (canMove){
if (keyboard_check(vk_up)){
if (gridY > 0) && (obj_collision.a_grid[gridX, gridY - 1] == 0){
targetX = x;
targetY = y - 16;
sprite_index = spr_yukarikarakter;
canMove = false;
}
}
if (keyboard_check(vk_down)){
if (gridY < (obj_collision.vcells - 1)) && (obj_collision.a_grid[gridX, gridY + 1] == 0){
targetX = x;
targetY = y + 16;
sprite_index = spr_asagikarakter;
canMove = false;
}
}
if (keyboard_check(vk_left)){
if (gridX > 0) && (obj_collision.a_grid[gridX - 1, gridY] == 0){
targetX = x - 16;
targetY = y;
sprite_index = spr_solakarakter;
canMove = false;
}
}
if (keyboard_check(vk_right)){
if (gridY < (obj_collision.hcells - 1)) && (obj_collision.a_grid[gridX + 1, gridY] == 0){
targetX = x + 16;
targetY = y;
sprite_index = spr_sagakarakter;
canMove = false;
}
}
}
if (x != targetX) || (y != targetY){
if (x < targetX) x += stepSpeed;
if (x > targetX) x -= stepSpeed;
if (y < targetY) y += stepSpeed;
if (y > targetY) y -= stepSpeed;
}else{
if (x == targetX) || (y == targetY){
canMove = true;
if (sprite_index == spr_yukarikarakter) sprite_index = spr_bostayukari
if (sprite_index == spr_asagikarakter) sprite_index = spr_bostaasagi
if (sprite_index == spr_solakarakter) sprite_index = spr_bostasola;
if (sprite_index == spr_sagakarakter) sprite_index = spr_bostasaga;
}
}
#endregion