1

I have written a collision check code and ran it, works perfectly but it is working for just one layer. e.g My code is getting layer id and gets tiles in that layer and giving them a number between 1 and 0. If it is 1 there is a collision, else (0) there is no obstacle. My problem is I have more than one layer like objects and objects2. But I can't choose the "objects2" layer to get the id. I can just choose the "objects" layer. This is my code:

"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
Mr. Fawkes
  • 11
  • 2
  • 1
    Could you explain where you need two collision layers for? Perhaps the second collision is not neccesary to work with. – Steven May 11 '20 at 06:36
  • 1
    @Steven Yes, of course, I have one terrain layer above that I have objects layer e.g Trees, Bushes. (Environmental Things). Above that, I have objects2 layer e.g Houses, farm fences, etc. I need more than one layer because I need to put something onto something. I think you already understood :D [link](https://ibb.co/JFF9vSC) It's the image of my map. – Mr. Fawkes May 11 '20 at 14:50
  • I see, thanks for the information. In my experience, I've used one invisible objects layer for object collisions, and used another objects layer purely for visuals, to draw over the collisions, that way collision is not related to which tile you use, and you only have to use one layer for collisions. – Steven May 12 '20 at 06:37
  • @Steven I have passed to that system because it is easier to do. Thanks for your answers. I appreciate it. :) – Mr. Fawkes May 13 '20 at 22:14
  • No problem, in that case I'll mark my comment as an answer. – Steven May 14 '20 at 06:28

1 Answers1

0

I've learned this method in one of Heartbeast tutorials:

I've using one invisible objects layer for object collisions, and used another objects layer purely for visuals, to draw over the collisions.

My collision objects are transparent squares, that way it's easy to tell where the collision is. Once I'm done with the collision layer, I can either hide it with the eye symbol, or hide the layers in code. (May provide examples later)

That way collisions are not related to which tile you use, and you only have to use one layer for collisions.

Steven
  • 1,996
  • 3
  • 22
  • 33