Discord
Login
Community
DARK THEME

how do I fix this code?

Im new to microstudio, Ive started a couple days ago, and this is the code i have. I'm making a simple platformer but, it can jump, but cannot go right. How do I go left AND right?

if keyboard.RIGHT then player.vx = 100 player.name = "running" else player.vx = 0 player.name = "idle" end

if keyboard.LEFT then
  player.vx = -100
  player.name = "running2"
  else
    player.vx = 0
    player.name = "idle2"
end
  
if keyboard.UP then
  player.vy = 100
end

if not player.grounded then
  player.name = "jump"
end
  if player.y<-200 then 
    gameover=1
  end
  
  Quick.camera.move(player.x,player.y)
end

what is the function? It has a extra end.

You have an if-else which sets player.vx to either 100 or 0 if you are pressing right, but then you follow it up with another if-else that sets it to either -100 or 0. So those are the only two possible values for player.vx when you actually wanted there to be three possible values. You can fix this by using the elsif statement:

if keyboard.RIGHT then 
  player.vx = 100 
  player.name = "running" 
elsif keyboard.LEFT then
  player.vx = -100
  player.name = "running2"
else
  player.vx = 0 
  player.name = "idle" 
end

You have two idle images, so you're probably going to need another variable to keep track of what direction the player is facing. Maybe:

if keyboard.RIGHT then 
  player.vx = 100 
  player.name = "running"
  player.facing = 1 
elsif keyboard.LEFT then
  player.vx = -100
  player.name = "running2"
  player.facing = -1
else
  player.vx = 0 
  if player.facing == 1 then 
    player.name = "idle"
  elsif player.facing == -1 then
    player.name = "idle2"
  end 
end

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community