Discuss Scratch

-_-Onscratch
Scratcher
100+ posts

Help with variables.

Hello i am making a horror game and im having quite a bit of trouble with some of my code. To be simple, the vent system in my game is not working it should work like this
(1: Click up arrow to get to vent)
(2: move cursor off arrow)
(3: flash flashlight at monster (if hes there)
(4 click down arrow to get to the house)
(5: repeat other steps)

But only one of the arrows work like i want them to the Top arrow is the wrong one it should set the touching gui variable to 1 like the down arrow.
Also something that makes me SUPER confused is that if you press the stop button in the project While in the vent that the bottom one is wrong. Like What??
(TouchingGui)
they should both set the TouchingGui variable to one when your cursor is touching the arrows
when green flag clicked
if <touching [Mouse] ?> then
set [TouchingGui] to [1]
end
any help with this will be amazing either its a bug with scratch or a bug with my code.
Link to my game Link Here

Last edited by -_-Onscratch (July 14, 2021 19:03:18)

Lais9502
Scratcher
7 posts

Help with variables.

Sorry if this response is late.

The problem with your code is simple. One of the arrows is trying to register TouchingGui to 1, but is failing to do so because the other arrow is constantly registering the same variable as 0 because it is not being touched. Solving this problem is quite simple.

Try to do it like this: Every time the Up arrow is touched, the variable is set to 1, if the Down arrow is touched, however, the variable is set to 2 instead. (and set to 0 if none are touched).

By doing this and modifying the code a bit so the arrows only register 0 if another arrow is NOT registering their value (1 or 2), your problem should be solved.

Your code should look like this afterward:

The Up arrow:

when green flag clicked
go to front
show
forever
if <touching [Mouse] ?> then
switch costume to [costume2]
if <(Area) = [House]> then
set [TouchingGui] to [1]
end
if <mouse down?> then
broadcast [Vent]
hide
play sound [Footsteps]
end
else
if <not <(TouchingGui) = [2]>> then
switch costume to [costume1]
set [TouchingGui] to [0]
end
end
end

The Down arrow:

when green flag clicked
go to front
show
forever
if <touching [Mouse] ?> then
switch costume to [costume2]
if <(Area) = [Vent]> then
set [TouchingGui] to [2]
end
if <mouse down?> then
broadcast [ExitVent]
hide
play sound [Footsteps]
end
else
if <not <(TouchingGui) = [1]>> then
switch costume to [costume1]
set [TouchingGui] to [0]
end
end
end

I hope this helps. Cheers!

Slick fires, bro!




Powered by DjangoBB