Discord
Login
Community
DARK THEME

Take Control Away From Player?

The idea behind the code is that I want some way to not let the player control the sprite, for example during dialogue or something. No matter how much I press Q though, I’m still able to move the sprite even when pressing arrow keys while holding down Q.

https://microstudio.io/i/Trondei/prac9givecontrol/

And is it possible to use playerHasControl as a string instead of a boolean? I’d like to be able to switch between "has" and "has not".

init = function()
  playerHasControl = false
  playerAnimation = "front"
  playerX = 0
  playerY = 0
  facing = 1
end

update = function()
  
  if playerHasControl = true then
    if keyboard.UP then
      playerY += 1
      playerAnimation = "up"
      facing = 1
    end
  
    if keyboard.DOWN then
      playerY -= 1
      playerAnimation = "front" // do NOT use 'and' after 'then'
      facing = 1
    end
  
    if keyboard.RIGHT then
      playerX += 1
      playerAnimation = "side"
      facing = 1
    end
  
    if keyboard.LEFT then
      playerX -= 1
      playerAnimation = "side"
      facing = -1
    end
  end
  
  if keyboard.Q then playerHasControl = true
  else playerHasControl = false
  end
  
end

draw = function()
  HSbackGround ()
  screen.setDrawScale ( facing, 1 )
  screen.drawSprite ( playerAnimation, playerX, playerY, 30 )
  screen.setDrawScale (1)
  
end

Yes! Just set playerHasControl to a string, and then you can check that string (i.e.

update = function()
  if keyboard.Q then
    playerHasControl = "no"
  else
    playerHasControl = "yes"
  end
  if playerHasControl == "yes" then
    (control code)
  end
end

) As for the issues, there are a few:

  1. In your if statement checking if playerHasControl, you are using = instead of ==. I don't know if this specifically can cause issues, but it's best practice to avoid this sort of thing, as sometimes it might be interpreted as trying to set the variable instead of reading it
  2. Your if statement is reversed. It's setting playerHasControl to true when you press Q, instead of false like you described in your post

Thanks for that, it’s working now.

Post a reply

Progress

Status

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