Windows (SOLVE)Try to do a on/off switch for flash lights help!!

E

Exequiel

Guest
iwant to have flashlight with on and off switch.
so hears my code:
turn = false

if keyboard_check_released(ord('F')) && turn = false {
surface_set_target(surf); <--not important
draw_set_color(c_black); <--not important
draw_set_alpha(0.92); <--not important
draw_rectangle(0, 0, room_width, room_height, false); <--not important
draw_set_alpha(1); <--not important
draw_set_blend_mode(bm_subtract); <--not important
draw_sprite_ext(spr_vision, 0, x-view_xview, y - view_yview,1,1, point_direction(x,y, mouse_x,mouse_y), c_white, 1);
surface_reset_target();<--not important
draw_set_blend_mode(bm_normal); <--not important
turn = true
}
else if keyboard_check_released(ord('F')) && turn = true
{
surface_set_target(surf); <--not important
draw_set_color(c_black); <--not important
draw_set_alpha(0.97); <--not important
draw_rectangle(0, 0, room_width, room_height, false); <--not important
draw_set_alpha(1); <--not important
draw_set_blend_mode(bm_subtract); <--not important
draw_sprite_ext(spr_sight, 0, x-view_xview, y - view_yview,1,1, point_direction(x,y, mouse_x,mouse_y), c_white, 1);
surface_reset_target(); <--not important
draw_set_blend_mode(bm_normal); <--not important
turn = false
}


i tried this code but, it only work when i hold down the F
:
turn = false

if keyboard_check(ord('F')) {
surface_set_target(surf); <--not important
draw_set_color(c_black); <--not important
draw_set_alpha(0.92); <--not important
draw_rectangle(0, 0, room_width, room_height, false); <--not important
draw_set_alpha(1); <--not important
draw_set_blend_mode(bm_subtract); <--not important
draw_sprite_ext(spr_vision, 0, x-view_xview, y - view_yview,1,1, point_direction(x,y, mouse_x,mouse_y), c_white, 1);
surface_reset_target();<--not important
draw_set_blend_mode(bm_normal); <--not important

}
else
{
surface_set_target(surf); <--not important
draw_set_color(c_black); <--not important
draw_set_alpha(0.97); <--not important
draw_rectangle(0, 0, room_width, room_height, false); <--not important
draw_set_alpha(1); <--not important
draw_set_blend_mode(bm_subtract); <--not important
draw_sprite_ext(spr_sight, 0, x-view_xview, y - view_yview,1,1, point_direction(x,y, mouse_x,mouse_y), c_white, 1);
surface_reset_target(); <--not important
draw_set_blend_mode(bm_normal); <--not important

}
 

Attachments

L

Lars Karlsson

Guest
EDIT: Check the post beneath. It's better.

You'll have to move the if (turn == false) parts inside the if(keyboard) parts, as shown:

Code:
if keyboard_check_released(ord('F')) {
   
    if (turn == false)
    {
        surface_set_target(surf); <--not important
        draw_set_color(c_black); <--not important
        draw_set_alpha(0.92); <--not important
        draw_rectangle(0, 0, room_width, room_height, false); <--not important
        draw_set_alpha(1); <--not important
        draw_set_blend_mode(bm_subtract); <--not important
        draw_sprite_ext(spr_vision, 0, x-view_xview, y - view_yview,1,1, point_direction(x,y, mouse_x,mouse_y), c_white, 1);
        surface_reset_target();<--not important
        draw_set_blend_mode(bm_normal); <--not important
        turn = true
    }
    else
    {
        surface_set_target(surf); <--not important
        draw_set_color(c_black); <--not important
        draw_set_alpha(0.97); <--not important
        draw_rectangle(0, 0, room_width, room_height, false); <--not important
        draw_set_alpha(1); <--not important
        draw_set_blend_mode(bm_subtract); <--not important
        draw_sprite_ext(spr_sight, 0, x-view_xview, y - view_yview,1,1, point_direction(x,y, mouse_x,mouse_y), c_white, 1);
        surface_reset_target(); <--not important
        draw_set_blend_mode(bm_normal); <--not important
        turn = false
    }
}
Give it a try. It 'should' work.
 
C

Chris

Guest
I see that in your code you're using keyboard_check which only returns true when the key is being held. The function you're probably looking for is keyboard_check_pressed which only returns true the step that the key is initially pressed. It also looks like you might be checking for key presses in the draw event, which is generally considered bad practice. Maybe you could try something like this!
 
Last edited by a moderator:
E

Exequiel

Guest
EDIT: Check the post beneath. It's better.

You'll have to move the if (turn == false) parts inside the if(keyboard) parts, as shown:

Code:
if keyboard_check_released(ord('F')) {
 
    if (turn == false)
    {
        surface_set_target(surf); <--not important
        draw_set_color(c_black); <--not important
        draw_set_alpha(0.92); <--not important
        draw_rectangle(0, 0, room_width, room_height, false); <--not important
        draw_set_alpha(1); <--not important
        draw_set_blend_mode(bm_subtract); <--not important
        draw_sprite_ext(spr_vision, 0, x-view_xview, y - view_yview,1,1, point_direction(x,y, mouse_x,mouse_y), c_white, 1);
        surface_reset_target();<--not important
        draw_set_blend_mode(bm_normal); <--not important
        turn = true
    }
    else
    {
        surface_set_target(surf); <--not important
        draw_set_color(c_black); <--not important
        draw_set_alpha(0.97); <--not important
        draw_rectangle(0, 0, room_width, room_height, false); <--not important
        draw_set_alpha(1); <--not important
        draw_set_blend_mode(bm_subtract); <--not important
        draw_sprite_ext(spr_sight, 0, x-view_xview, y - view_yview,1,1, point_direction(x,y, mouse_x,mouse_y), c_white, 1);
        surface_reset_target(); <--not important
        draw_set_blend_mode(bm_normal); <--not important
        turn = false
    }
}
Give it a try. It 'should' work.
thanks for the reply :)
it didnt work though.
Thanks for the idea :D
 
Last edited by a moderator:
E

Exequiel

Guest
I see that in your code you're using keyboard_check which only returns true when the key is being held. The function you're probably looking for is keyboard_check_pressed which only returns true the step that the key is initially It also looks like you might be checking for key presses in the draw event, which is generally considered bad practice. Maybe you could try something like this!
Thanks Man. It worked :) problem solve!
 
Top