(I am new to BGE so I might sound like a noob) I am making a game with two doors in it. One is open and the other is closed. When you press the spacebar, the one that was open will close and the one that was closed will open. I have no idea how to do this. Can I get some help?
1 Answer
As you want to use the same event for both opening and closing I suggest to implement a state machine.
State machine
The states are at least:
- Closed,
- Opened
The transitions are:
Closed:
- on space: transit to Opened
Opened:
- on space: transit to Closed
Entry Actions:
Closed:
- play closing action
Opened:
- play closing action
Implementations
Flipper
We use a boolean property as current state. Call it "opened". When True it means status Open, when False it means status Closed.
Property: "opened" Type: Boolean Value: unchecked
Transition logic
Property Sensor -> AND -> Action Actuator
Property Sensor Mode: equal Property: opened Value: False
I assume you want to play the same animation reverse on closing
Action Actuator Mode: Flipper Action: opening animation
What will happen?
When opened is False, the sensor does nothing and the door remains as it is (closed). When the property opened turns True, the sensor triggers the AND controller which activates the action actuator. The action actuator begins to open the door till it is completely open.
When the property opened turns False, the sensor triggers the AND controller which deactivates the action actuator. The action actuator begins to play the opening animation reverse -> closing the door till it is closed.
Event logic
Now you need a way to switch the property.
keyboard sensor -> AND -> Property actuator
Keyboard sensor Key: space
Property Actuator Mode: toggle Property: opened
What it does: when you press space the sensor will trigger the AND which will activate the Property actuator. The property actuator will toggle the property opened (from True to False and False to True).
This will be the event on the above Transition logic, which will play the according animation.
Remarks
There are other ways to implement door logic. This is just a sample.