Advertisement
Segfault1

Array move

Mar 23rd, 2023 (edited)
1,820
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// addToAlpha(obj, alpha)
  2.  
  3. // add a new object to the alpha array
  4. array_push(alpha, obj);
  5.  
  6.  
  7. //call and creation
  8.  
  9. // create an array of objects
  10. var alpha = [obj_player, obj_enemy1, obj_enemy2];
  11.  
  12. // update positions of objects in the alpha array by adding 10 to x and 5 to y
  13. changePositions(alpha, 10, 5);
  14.  
  15.  
  16. //Run
  17. /// changePositions(alpha, deltaX, deltaY)
  18.  
  19. for (var i = 0; i < array_length_1d(alpha); i++) {
  20.     var obj = alpha[i];
  21.     obj.x += deltaX; // add deltaX to x position
  22.     obj.y += deltaY; // add deltaY to y position
  23. }
Advertisement
Comments
  • Segfault1
    1 year
    1. /// Map Array Storage where Map is your 2d Array
    2.  
    3.  
    4. // define the size of each tile
    5. var tile_size = 32;
    6.  
    7. // define the number of rows and columns in the map
    8. var num_rows = array_height_2d(map);
    9. var num_cols = array_length_2d(map, 0);
    10.  
    11. // define the offset of the currently visible row
    12. var row_offset = 0;
    13.  
    14. // define the number of visible rows
    15. var num_visible_rows = room_height div tile_size;
    16.  
    17. // draw the visible portion of the map
    18. for (var row = row_offset; row < row_offset + num_visible_rows; row++) {
    19.     for (var col = 0; col < num_cols; col++) {
    20.         var tile = map[row, col];
    21.         draw_sprite(tile_sprite, tile, col*tile_size, (row-row_offset)*tile_size);
    22.     }
    23. }
    24.  
    25. // increment the row offset when scrolling down
    26. if (keyboard_check_pressed(vk_down)) {
    27.     row_offset += 1;
    28. }
    29.  
    30. // decrement the row offset when scrolling up
    31. if (keyboard_check_pressed(vk_up)) {
    32.     row_offset -= 1;
    33. }
  • Segfault1
    1 year
    1. // Saw this in a forum might work for your map
    2.  
    3. var tile_size = 32; // size of each tile in pixels
    4. var start_row = floor(scroll_y / tile_size); // first visible row
    5. var end_row = min(start_row + ceil(view_hview[0] / tile_size), array_height_2d(map) - 1); // last visible row
    6.  
    7. for (var y = start_row; y <= end_row; y++) {
    8.     for (var x = 0; x < array_length_2d(map, 1); x++) {
    9.         // draw the tile at (x, y) in the array
    10.         var tile = map[x, y mod array_height_2d(map)];
    11.         draw_sprite(tile_sprite, tile, x * tile_size, (y - start_row) * tile_size - (scroll_y mod tile_size));
    12.     }
    13. }
Add Comment
Please, Sign In to add comment
Advertisement