Picking up and swapping 'keys', opening doors

0 favourites
  • 4 posts
From the Asset Store
Full game Construct 2 capx code source to post on Google Play
  • Hi folks, quite new to Construct(3), but not a new developer.

    I'm trying to figure out a good way to handle keys and doors in Construct. Here's a scenario.

    I have 5 keys and 5 doors. Each key opens the relative # door. Eg: Key 1 opens Door 1, Key 2-Door 2 etc.

    If I walk over a key (eg:1) I pick it up. If I'm holding a key the original key gets replaced on the screen in the original spot.

    Heres a quick 'action list'.

    Player hits key 1, picks it up (Key 1 disappears from screen). Player is holding key 1.

    Player hits key 3, Key 1 shows back in its spot. key 3 disappears from the screen and player is holding key 3.

    Player hits key 4, Key 3 shows back in its spot. key 4 disappears from the screen and player is holding key 4.

    Player opens door 4. Key (and door) 4 are now destroyed, player is holding NO key. Key 4 will not be displayed again.

    -----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----

    Since the graphic of each key/door are different, they aren't instances. (I decided that wasn't prudent since the keys are numbered) they are all separate objects. Door1, Door2, Key1, Key2. This can be changed if needed.

    In pseudo code, I think its something like this...

    If Player collides with Key

    If Key(KeyHeld Variable).destroyed = False. -- Only show the held key if it wasn't destroyed

    Key(KeyHeld Variable).Visible = true. -- Re-Show the old key back in it's spot.

    End

    KeyHeld.Variable = Key.KeyNumber. -- Update key held to the key we just hit.

    Key(KeyHeld Variable).Visible = false. -- Hide the key onscreen that we hit.

    end

    If Player collides with a door

    If DoorNumber = KeyHeld Variable. -- If the key matches the door

    Destroy DoorNumber -- Remove the door

    Destroy Key(KeyHeld Variable) -- Destroy the key object based on the variable held

    KeyHeld Variable = 0 -- Reset the variable since we're not actually holding a key

    end

    end

    __________________________________________________________________

    Based on the pseudo code, I'm not sure how in construct to nest the checks of keys 1-5 (I actually have 8).

    I also believe I can't use Key(KeyHeld Variable) either. I couldn't figure out how to easily link a variable to an object even after several forum/google sessions.

    I hope there is enough and not too much info on the post, I'd appreciate any input for this construct NooB!

    Thank you.

  • Hey graham-s,

    I made a little example:

    https://1drv.ms/u/s!Ap_-qxoGKbDcgQ8yTV1eWA0RsQ7X

  • graham-s

    Here is one way of doing it, one thing I wasn't sure is when you say >> Leave the key on the same "spot" do you refer to their starting Point Position or in the Place of the New key Found?

    anyway I leave you both examples to make it easier, hope it helps

    Leave the key on his Starting point capx:

    https://www.dropbox.com/s/kgqho9kjeua4m86/Doors%20%26%20Key%20Linking%201.capx?dl=0

    Leave the key on the spot of the Newly collected Key capx:

    https://www.dropbox.com/s/2gve7ifwfyupi80/Doors%20%26%20Key%20Linking%202.capx?dl=0

    You can Remove all the Txt boxes are just for Debugging

    Edit: I just saw that kriand was Faster and had similar Idea haha, but I leave it just in case, the more examples the better

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Oh, those are stupendous responses! You both totally nailed it, and boy it's a *LOT* simpler than I was going to make it.

    Thank you, that's a keeper. Gold Star to you both.

    Problem Solved.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)