Adding velocity to a character

Hello, its me again, im trying to add 15 speed to my character every time it overlaps with the pizza, but the speed doesnt change at all.Any help?
sprites.onOverlap(SpriteKind.Player, SpriteKind.Food, function (sprite, otherSprite) {
info.changeScoreBy(1)
movx += movx + 15
movy += movy + 15
controller.moveSprite(mySprite, movx, movy)
info.startCountdown(timer)
pizza.setPosition(Math.randomRange(20, 140), Math.randomRange(20, 100))
})
let timer = 0
let pizza: Sprite = null
let mySprite: Sprite = null
let movx = 0
let movy = 0
movy = 75
movx = 75
scene.setBackgroundColor(11)
mySprite = sprites.create(img, SpriteKind.Player)
controller.moveSprite(mySprite, movx, movy)
pizza = sprites.create(img, SpriteKind.Food)
timer = 3

If you want to change movx and movy by 15 each time, then the syntax that you want is

movx += 15
movy += 15

Keep in mind that controller.moveSprite merely changes the speed at which the sprite moves when they’re operated by the controller. If you want to change the actual velocity of the sprite (i.e. you want the sprite to be moving all the time), then you want this instead:

mySprite.vx += 15
mySprite.vy += 15

Without seeing your game, I’m not sure which of the two you need.

1 Like

Thanks for the help, I have already tried doing your first example, but it doesnt seem to work, and yes, i want to change the speed at which the sprite moves when i press the keys. I have no idea what to do.

What i can tell by using the debug mode, is that the variable does go up, but not the characters speed

Give this a try, so that you can see how the different speeds behave:

Press A to increase the speed, and press B to decrease the speed. Move the sprite with the arrow keys. The score reflects the current speed.

Your initial speed is set to 75, which is already pretty high. Changing the speed by 15 when it’s that high might not be very noticeable.

2 Likes

Thanks!! it works great now, thank you

1 Like

Thanks helped me as well! Just wondering why do you need to set the movement again for each button A&B when it’s already in On Start: Move My Sprite with buttons vx (mySpeed) xy (mySpeed)?

Hi, @Buller !

Sorry about the delay in my response. I haven’t been able to hop on the forum in a couple of months.

We’re changing the variable mySpeed each time a button is pressed. The sprite’s velocity, though, doesn’t change just because mySpeed changes. They’re not linked in any way. If we want to change the sprite’s velocity, we need to say so in our code. That’s why the blocks are repeated.

1 Like