GML Making a door: image_index/mouse_pressed

Dr_Nomz

Member
I can't believe this isn't working, but here's my code:
Code:
if image_index=0{
  if keyboard_check(ord("E")){
  image_index=1
  }
}

if image_index=1{
  if keyboard_check(ord("E")){
  image_index=0
  }
}
Now I've tried putting this in a mouse_enter event, and trying a few variations with "abs(mouse_coords)" but nothing is working.

All I want is for the image_index to be 0 (set in create event, kept still with image_speed=0 in step) and then switch between the two depending on if the mouse cursor is over the door, and the E key is pressed.

I'd also REALLY like to have it so the mouse can be a few pixels away from the door and still work, like instead of y10 or x10 (away from mouse) it could be 50, or something.

EDIT: Also my NPCS are stupidly acting like there's something in the way of their path even if the door is open and can be walked through... any suggestions for fixing that? (Like maybe changing the sprite mask of the door, or using a different approach in opening it?)
 

Bentley

Member
What's likely happening is the first block of code and the second block of code run one after the other. In other words, you press E which sets image_index to 1. Now the second block of code runs because image_index is 1.
 

Dr_Nomz

Member
Okay, but if it's not IN the image (like with mouse_enter event, or abs_mouse) then it shouldn't matter, because once the image is changed, it won't be in the object anymore.
 
Create event:
Code:
buffer = 4; //how many pixels around the edges of the door you want the mouse to be able to be. Set to 0 for precise mouseover
Step event:
Code:
if (point_in_rectangle(mouse_x,mouse_y,bbox_left-buffer,bbox_top-buffer,bbox_right+buffer,bbox_bottom+buffer)){
    if(keyboard_check_pressed(ord("E"))){
        image_index = -image_index+1; //This will toggle it between 0 and 1
    }
}
Alternatively, you could do something like make the mask of the door bigger than the door, and use the mouse enter and leave events to set a variable that the mouse is ontop of the door. Then in the step or keyboard e press event, check that that variable is true.
 

TheouAegis

Member
The enemies and whatnot colliding with the open door as perhaps due to the fact that even if if a Sprite has all of uts bounding-box settings set to zero, it is still a 1 pixel bounding box. So it's possible they are detecting that one little pixel. to completely remove collisions, you have to remove the sprite, which would mean you would have to drop the doors sprite rather than simply assigning it to the door object.

As Bentley said, your code was indeed changing the image index to one, but because you did not have an else between those conditionals, it was immediately then setting it back to zero. The state of any key or button persists throughout the entire step unless you clear it manually yourself. Telling this case, once the E button has been pressed, it will continue to be pressed until the beginning of the next step. Even if the key is no longer pressed. This is also why changing an instance into another object will not stop the next object's code from running immediately.
 
Top