# Moving Sprites a certain amount

This may be too complicated for where we are. But in lesson 13 a student wants a sprite to move a certain amount- but the loop has it always move. Is it possible to have a sprite only move a certain amount?

Salutations @slpruit,
As per your current request within your situation i would assume your student has probably done 1 of the 2 things that i can think on the top of my head

1: Sprite.x / Sprite.y is being increased because itâ€™s in the draw loop

2: Student is using velocityX / velocityY / setSpeedAndDirection which is applied regaurdless of weather itâ€™s invoked in a loop or outside of one

for the sake of simplicity of using movement we will just be modifying x & y properties of a given sprite in our programâ€¦ hereâ€™s my little demo

``````var duck = createSprite(200, 200);
var duckSpeed = 5;
function draw() {
background(255);
// moves the duck left
if(keyDown("left")) {
duck.x = duck.x - duckSpeed; // can also be written as duck.x -= duckSpeed;
}
// moves the duck right
if(keyDown("right")) {
duck.x = duck.x + duckSpeed; // can also be written as duck.x += duckSpeed;
}
// moves the duck up
if(keyDown("up")) {
duck.y = duck.y - duckSpeed; // can also be written as duck.y -= duckSpeed;
}
// moves the duck down
if(keyDown("down")) {
duck.y = duck.y + duckSpeed; // can also be written as duck.y += duckSpeed;
}
drawSprites();
}
``````

Iâ€™m not sure how far unit 13 is into Gamelabâ€¦ but i feel like it shouldnâ€™t be to hard to learn what this does in fact maybe just hand your student this and see what they can figure out. I remember starting out with unrecognizable code demos in fact i still learn from examples! the worst thing is being told itâ€™s not possible when it is or being told your too far ahead.

Anyways, hope this little demo helps you out

2 Likes

Hi @slpruit,

@varrience has a great solution, I know in Lesson 13, you havenâ€™t yet learned keyboard movement(but @varrience gave you a great preview). Another good method is to think about why the sprite should move or for how long? For example, if sprite.x <300 then sprite.x = sprite.x + 5. This will tell the sprite to move right as long as it is before 300 on the grid and then stop. And yes, youâ€™ll eventually learn many other ways to stop the sprite (if it is colliding with another sprite, if the key is down, etc.)
Hope this helps!
~Michelle