Lock and Key

One of the games I’m working on involves the use of a key, which once you pickup, you take it to the lock and open it. That’s how you win the level. My question is, how would I make this happen?

My guess is to put a playerHasKey = 0 counter on my player, and when then player collides with the key, changes the counter to 1. For the lock, it will check if the value is 1 before it opens. Does that sound about right?

Hi Nick yes this does sound about right. You would have a boolean that would be false then when the “player” collides with the key it will change it to true then when the player collides with the lock ,it will check to see if the boolean is true then if will open.

Need any more help message me.

Tyler Jacobson

Cool cool, thanks man. I’m having some trouble getting this to work. I also can’t get my character to jump for some reason. If I message you, would you be willing to check out my code and tell me what I’m doing wrong?

Thanks!

I managed to figure things out on my own, thanks everyone.

I want to put a picture of the key at the top left of the screen to indicate the player is holding the key. I figured putting display.newImageRect in the grabKey event would work, but how would you set the position (x/y)?? Isn’t that only possible if it’s in a variable so you could object.x and object.y?

Hey Nick so are you trying to put the key in a certain position? If so then yes you would do “key.x = your position here"key.y = your position here”

Hey,

No, I’m actually removing the key upon player collision, then creating an icon to place at the top of the screen to show the player he’s holding the key. The icon will just be a picture of the key.

Hey Nick so when the player collides with the key then it willl remove it. This should work but I did not test it.

Good Luck!

local function getkey (self,event) key:removeSelf() local keyicon = display.newImage("key.png",50,50,50,50) end player.collision = getkey

Hi Nick yes this does sound about right. You would have a boolean that would be false then when the “player” collides with the key it will change it to true then when the player collides with the lock ,it will check to see if the boolean is true then if will open.

Need any more help message me.

Tyler Jacobson

Cool cool, thanks man. I’m having some trouble getting this to work. I also can’t get my character to jump for some reason. If I message you, would you be willing to check out my code and tell me what I’m doing wrong?

Thanks!

I managed to figure things out on my own, thanks everyone.

I want to put a picture of the key at the top left of the screen to indicate the player is holding the key. I figured putting display.newImageRect in the grabKey event would work, but how would you set the position (x/y)?? Isn’t that only possible if it’s in a variable so you could object.x and object.y?

Hey Nick so are you trying to put the key in a certain position? If so then yes you would do “key.x = your position here"key.y = your position here”

Hey,

No, I’m actually removing the key upon player collision, then creating an icon to place at the top of the screen to show the player he’s holding the key. The icon will just be a picture of the key.

Hey Nick so when the player collides with the key then it willl remove it. This should work but I did not test it.

Good Luck!

local function getkey (self,event) key:removeSelf() local keyicon = display.newImage("key.png",50,50,50,50) end player.collision = getkey