This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-04-02 14:41:20

ScratchOS
Scratcher
Registered: 2012-03-20
Posts: 53

This script doesn't work?

I am making an RPG. This script is for moving the character, but it doesn't work correctly..

when gf clicked
forever
if <key [down arrow] pressed?>
set [ypos] to [-0.4]
else
set [ypos] to [0]
end
if <key [up arrow] pressed?>
set [ypos] to [0.4]
else
set [ypos] to [0]
end
if <key [left arrow] pressed?>
set [xpos] to [-0.4]
else
set [xpos] to [0]
end
if <key [right arrow] pressed?>
set [xpos] to [0.4]
else
set [xpos] to [0]
end
change x by (xpos)
change y by (ypos)
what it basically does is moves the character by the variable and x or y gets changed by the character.

Why you may be asking? This is going to make collision detecting easier so the sensor will touch a colour and change x or ypos.

It works fine for moving upwards and rightwards but you can't go left or down.. please help!!

Offline

 

#2 2012-04-02 15:24:34

Ratty1967UK
Scratcher
Registered: 2007-05-16
Posts: 88

Re: This script doesn't work?

The "else" statements are overriding the earlier actions of the "if" statements - for example, if "down" is pressed, you set "ypos" to -0.4 but at the next statement, "up" isn't pressed and so sets ypos to 0 instead - hope i've explained that properly!  smile

Offline

 

#3 2012-04-02 15:25:23

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: This script doesn't work?

ScratchOS wrote:

I am making an RPG. This script is for moving the character, but it doesn't work correctly..

when gf clicked
forever
if <key [down arrow v] pressed?>
set [ypos v] to [-0.4]
else
set [ypos v] to [0]
end
if <key [up arrow v] pressed?>
set [ypos v] to [0.4]
else
set [ypos v] to [0]
end
if <key [left arrow v] pressed?>
set [xpos v] to [-0.4]
else
set [xpos v] to [0]
end
if <key [right arrow v] pressed?>
set [xpos v] to [0.4]
else
set [xpos v] to [0]
end
change x by (xpos)
change y by (ypos)
what it basically does is moves the character by the variable and x or y gets changed by the character.

Why you may be asking? This is going to make collision detecting easier so the sensor will touch a colour and change x or ypos.

It works fine for moving upwards and rightwards but you can't go left or down.. please help!!

It doesn't work for the down and left because right after it sets it to 0.4 it checks for the right arrow key, and since the right key isn't pressed, it changes it back to 0. Same with the down key and up key. I suggest using this.

when gf clicked
forever
if <<not<key [left arrow v] pressed?>> and <not<key [right arrow v] pressed?>>>
set [x pos v] to [0]
end
if <<not<key [up arrow v] pressed?>> and <not<key [down arrow v] pressed?>>>
set [y pos v] to [0]
end
if <key [right arrow v] pressed?>
set [x pos v] to [0.4]
end
if <key [left arrow v] pressed?>
set [x pos v] to [-0.4]
end
if <key [up arrow v] pressed?>
set [y pos v] to [0.4]
end
if <key [down arrow v] pressed?>
set [y pos v] to [-0.4]
end
change x by <x pos>
change y by <y pos>
I'm almost completely sure that that would work.
BTW also fixed your scratchblocks.

EDIT: Ratty1967UK also explained it but I included the script

Last edited by TorbyFork234 (2012-04-02 15:25:32)

Offline

 

#4 2012-04-03 05:27:53

ScratchOS
Scratcher
Registered: 2012-03-20
Posts: 53

Re: This script doesn't work?

Thank you so much!

Offline

 

Board footer