Discord
Login
Community
DARK THEME

please help

i want to make it so that i can press R and change the player sprite so far: init = function()

subrace = "subrace" player = object position_x = 0 position_y = 0 setPosition = function(x,y) position_x = x // sets player.posidation_x position_y = y // sets player.position_y end end end

update = function() if keyboard.R then subrace = subrace + 1 end

if keyboard.W then position_y = position_y+1 else if keyboard.S then position_y = position_y-1 else if keyboard.D then position_x = position_x+1 else if keyboard.A then position_x = position_x-1 end

end end end end

draw = function() screen.drawSprite( "subrace_button", -150, -80, 64, 36 ) screen.clear

if keyboard.W or keyboard.S or keyboard.D or keyboard.A then screen.drawSprite( subrace, position_x, position_y, 16, 16 ) screen.clear() screen.drawMap( "level1", 0, 0, screen.width, screen.height ) else screen.drawSprite( subrace, position_x, position_y, 16, 16) end screen.drawSprite( subrace, position_x, position_y, 16, 16 ) screen.drawSprite( "subrace_button", -150, -80, 64, 36 ) end

i press R and it goes through all the sprites

If you want the code to only be executed when hitting the key, you need keyboard.press.R instead of keyboard.R. Please look at the code below.

Also, if you share code in your next community post, wrap the code in three backticks for the start and end of the code. If you do not know what key that is, a google search shall resolve it.

And finally, use preview to make sure that it looks right.

init = function()
  subrace = "subrace"
  player = object
    position_x = 0
    position_y = 0

    setPosition = function(x,y)
      position_x = x // sets player.position_x
      position_y = y // sets player.position_y
    end
  end
end

update = function()
  // Cycle to the next sprite
  if keyboard.press.R then subrace = subrace + 1 end
  // TODO: Overflow and/or underflow protection?

  // It allows diagonal movement
  if keyboard.W then position_y += 2
  else if keyboard.S then position_y -= 1 end
  if keyboard.D then position_x += 1
  else if keyboard.A then position_x -= 1 end
end

draw = function()
  screen.clear() // Always clear the screen first
  screen.drawMap( "level1", 0, 0, screen.width, screen.height ) // Next draw the map
  screen.drawSprite( subrace, position_x, position_y, 16, 16 ) // Now the player
  screen.drawSprite( "subrace_button", -150, -80, 64, 36 ) // and finally the GUI
end

Thank you so much

I have done overflow/underflow protection

Post a reply

Progress

Status

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