I am new to this coding language and I have basic knowledge of JS and better of python
I am trying to make a sprite “jump” by changing the sprite y by a hundred and waiting 1 second and changing the y by -100.
Here is the code if there is a problem or you have an example code you can give me to lean pls do so.
init = function()
height = -50
end
update = function()
if keyboard.SPACE then
height += 100
sleep 1000 seconds
height -= 100
end
end
draw = function()
screen.drawSprite("sprite",-150,height,20,20)
end
If you want realistic jumping, track the sprite's y position and velocity.
init = function()
sprite = object
y = 20
vy = 0
end
end
update = function()
sprite.vy -= 0.5
if keyboard.press.SPACE then
sprite.vy = 7
end
sprite.y += sprite.vy
// example of floor at fixed height
if sprite.y < -10 then
sprite.y = -10
sprite.vy = 0
end
end
draw = function()
screen.clear()
screen.drawSprite("sprite", 0, sprite.y, 20, 20)
end
You can also use Quick Engine, which takes care of platformer physics.
Can you go into depth how this works more, I have made similar codes and I do not know why this works versus other codes.
I can’t make the sprite go up and go down after a second. The sprite does not move after I put a if Space and y += 100 sleep 1 second and y -= 100
sprite = object
y = 20
vy = 0
end
that will make an object called sprite (and object is an variable name with stuff on it so now sprite.y and sprite.vy)
sprite.vy -= 0.5
if keyboard.press.SPACE then
sprite.vy = 7
end
sprite.y += sprite.vy
// example of floor at fixed height
if sprite.y < -10 then
sprite.y = -10
sprite.vy = 0
end
sprite.vy = 7 will set in the velocity of the sprite to 7
sprite.y += sprite.vy will update the sprite position
if sprite.y < -10 then
sprite.y = -10
sprite.vy = 0
end
this is like if sprite y is less than -10 then we will sprite y wil be - 10 so it will hold the position
and then the sprite vy will be set to 0 so that it is moving stops
There are a couple issues here. First, the sleep command halts your program immediately. What you want is probably the after command. I don't use it much but it can work here just fine for what you want.
Second, you are testing the state of the keyboard 60 times per second, which might cause height to increase very quickly. Instead of testing if the key is down, you'll want to test if the keyboard was just pressed this frame. Use keyboard.press.SPACE instead of keyboard.SPACE to do this.
Third, you'll need to clear the screen each frame by calling screen.clear(), otherwise it will look like you have two sprites on the screen (one at y = -50 and one at y = 150).
init = function()
height = -50
end
update = function()
if keyboard.press.SPACE then
height += 100
after 1 seconds do height -= 100 end
end
end
draw = function()
screen.clear()
screen.drawSprite("sprite",0, height,20,20)
end
The code in the other suggestions are how you would do "mario-style" jump physics, if you are interested in that. Also you can use three backticks (`) to start and end a code block in your comments here. You can look up "markdown code section" on google to see how it works. It'll preserve your code's formatting and make it easier to read.
@Pesant First, like @JensGameStudios said, sprite
is an object with properties that can be accessed with sprite.y
and sprite.vy
. sprite.y
represents the sprite's Y (vertical) position, where increasing it moves the sprite up and decreasing it moves the sprite down. sprite.vy
contains the Y velocity, or speed. The Y speed is added to the Y position in update()
. When an object is thrown into the air, its vertical speed slowly decreases until it starts falling down. This is why the Y velocity is decreased each frame.