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

#1 2012-02-08 15:39:22

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Tutorial: Simple Arrow-key Controlled Sprite Motion

A lot of the beginning projects I see often use this kind of simple sprite control:

when [Up Arrow v] key pressed
point in direction (0 v)
move (10) steps
when [Down Arrow v] key pressed
point in direction (180 v)
move (10) steps
when [Right Arrow v] key pressed
point in direction (90 v)
move (10) steps
when [Left Arrow v] key pressed
point in direction (-90 v)
move (10) steps
Probably because it's in some of the sample projects and it's easy to understand.

The method above works...but it's kind of jerky for continuous movement.  It takes some time after your finger presses a key for your computer to decide that you really want a repeated motion and starts to generate more key presses.

A better method (in most cases), uses a continuously scanning loop with key sensing to see if the key is pressed.  The result is faster response and smoother motion.  I've shown it with a variable called "Speed" defined because it's so handy to be able to tune the speed in one place instead of four.

when gf clicked
set [Speed v] to (4)
forever
  if <key [Up Arrow v] pressed?>
    point in direction (0 v)
    move (Speed) steps
  end
  if <key [Down Arrow v] pressed?>
    point in direction (180 v)
    move (Speed) steps
  end
  if <key [Right Arrow v] pressed?>
    point in direction (90 v)
    move (Speed) steps
  end
  if <key [Left Arrow v] pressed?>
    point in direction (-90 v)
    move (Speed) steps
  end
end

Last edited by Paddle2See (2012-02-08 18:40:31)


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#2 2012-02-08 15:48:38

TRocket
Scratcher
Registered: 2009-08-18
Posts: 1000+

Re: Tutorial: Simple Arrow-key Controlled Sprite Motion

great guide  smile  i'm sure this will help a lot of new scratchers...

Last edited by TRocket (2012-02-08 15:54:00)


http://i.imgur.com/1QqnHxQ.png

Offline

 

#3 2012-02-08 15:52:32

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Tutorial: Simple Arrow-key Controlled Sprite Motion

Actually TRocket, he did it just fine. Technically it's supposed to be like this:

when gf clicked
set [Speed v] to (4)
forever
  if <key [Up Arrow v] pressed?>
    point in direction (0 v)
    move (Speed) steps
  end
  if <key [Down Arrow v] pressed?>
    point in direction (180 v)
    move (Speed) steps
  end
  if <key [Right Arrow v] pressed?>
    point in direction (90 v)
    move (Speed) steps
  end
  if <key [Left Arrow v] pressed?>
    point in direction (-90 v)
    move (Speed) steps
  end
end

Last edited by Magnie (2012-02-08 15:53:00)

Offline

 

#4 2012-02-08 17:18:25

CheeseMunchy
Scratcher
Registered: 2008-10-13
Posts: 1000+

Re: Tutorial: Simple Arrow-key Controlled Sprite Motion

Awesome.


6418,

Offline

 

#5 2012-02-08 17:42:21

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: Tutorial: Simple Arrow-key Controlled Sprite Motion

Wow, my first ever project used the method with the hat blocks :') It's really annoying when I find a project which would he awesome, but it uses the method with the hat blocks, so then you can't move properly :l


Why

Offline

 

#6 2012-02-08 18:42:12

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Tutorial: Simple Arrow-key Controlled Sprite Motion

Magnie wrote:

Actually TRocket, he did it just fine. Technically it's supposed to be like this:

Thanks  smile

Fixed.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#7 2012-06-06 03:04:03

Jupitar
New Scratcher
Registered: 2012-06-05
Posts: 27

Re: Tutorial: Simple Arrow-key Controlled Sprite Motion

Great tutorial! In many New Scratcher projects, I see the basic jump down"formulae" and the sprite hangs in mid-air. I'm sure it was based on a sample project, where the sprite hangs in mid-air and drops down again?

Offline

 

#8 2012-06-06 09:10:07

sonicfan12p
Scratcher
Registered: 2011-11-16
Posts: 1000+

Re: Tutorial: Simple Arrow-key Controlled Sprite Motion

Good guide, this should come in handy for New Scratchers!  smile


Why are the secret organizations getting all the attention?  mad

Offline

 

#9 2012-06-06 14:05:22

humhumgames
Scratcher
Registered: 2012-03-26
Posts: 1000+

Re: Tutorial: Simple Arrow-key Controlled Sprite Motion

This will be very helpful to new scratchers.

Offline

 

#10 2012-06-06 17:23:39

fetzer
Scratcher
Registered: 2009-06-11
Posts: 70

Re: Tutorial: Simple Arrow-key Controlled Sprite Motion

*Request to be stickied*

Offline

 

Board footer