Need help with changing the velocity of my character

I cant seem to be able to change the character speed, here is my code:
let movx = 75
let movy = 75
let mySprite = sprites.create(img.., SpriteKind.Player)
controller.moveSprite(mySprite, movx, movy)
pizza = sprites.create(img.., SpriteKind.Food)
timer = 3
forever(function () {
if (info.score() == 10) {
timer = 2.5
scene.setBackgroundColor(10)
movx = 300
movy = 300

}

})
as you can see i set the speed of the character movx for the x axis and movy for the y axis, and when the if statement comes true it changes the 2 variables to 300 which i think should change the speed but doesnt, anyone please help

When you call moveSprite, it passes the current value stored in the variables; in this case, that is 75 for movx and movy. This is stored as a separate variable inside that function, and can be changed / used independent of the outer movx and movy – it’s the same as just passing in the number directly. Here’s an article that tries to explain this behavior, but it can be very confusing (when I taught sections of the intro to programming class at UW, it was probably the most common point that people got caught up on near the final exam). The main thing to note is that when you pass a variable it passes what is stored in the variable, not the variable itself.

In this case, the easiest way to fix this would just be to call controller.moveSprite again within the forever loop when you want the speed to change to 300 – in the if statement end it with controller.moveSprite(mySprite, movx, movy) or just controller.moveSprite(mySprite, 300, 300)

2 Likes

Thanks a lot, makes much more sense now, Thank you!