1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

How do I click on a sprite without changing it's collision mask?

Discussion in 'Programming' started by Raginim, Sep 22, 2018.

  1. Raginim

    Raginim Guest

    So say I want to have a harvestable tree. The current collision mask is near the base of the tree so the player can move behind the leaves, but not through the base of the tree. However I want to be able to click anywhere on the tree to harvest it.


    Code:
    if (mouse_check_button_released(mb_left) and distance_to_object(nearestTree) <harvestRange and instance_position(mouse_x,mouse_y,nearestTree)) etc.

    This is in the player actions script on the player Step event. It works but only if I click on the base of the tree, instead of the whole tree. I'd like an exact outline of the tree if possible.

    Thanks
     
  2. Kanugane

    Kanugane Member

    Joined:
    Sep 20, 2018
    Posts:
    25
    Try to create a parent object. In its events, create a mouse enter event. You can execute any code there you like. For example:
    Code:
    if (mouse_check_button_pressed(mb_left)){
     instance_create_depth(x,y,100, obj_wood);
    }
    Then you can assign any tree object you want to the parent object and you theoretically should be able to chop wood from any of the tree. And don't forget to make a check for the distance to player!
     
  3. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,026
    var left = x - sprite_xoffset;
    var top = y - sprite_yoffset;
    if median(mouse_x, left, left+sprite_width) == mouse_x
    and median(mouse_y, top, top+sprite_height) == mouse_y
    { //harvest code// }

    However, this would incorporate empty space in the sprite, too. To avoid that, you could have a separate mask that covers just the visible parts of the tree and temporarily assign that mask_index to the tree before doing the mouse collision checks.
     
  4. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    725
    Ah, I very much like the idea of swapping masks in and out. I wonder what the load would be like to swap in and out masks for the purposes of vision cones.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice