Skip to content

Commit dcee9a2

Browse files
authored
add keyboard nuances to popup docs
1 parent 6bb649a commit dcee9a2

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

tutorials/popup.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,31 @@ class $modify(MenuLayer) {
128128
};
129129
```
130130
This will make the popup show correctly by adding it as a child to the new `MenuLayer` instead of the previous scene.
131+
### Fixing keyboard input
132+
If you use the example above, you will find that keyboard input still goes to the main layer instead of the popup. For `MenuLayer` specifically this can be solved by delaying the addition by a single frame:
133+
```cpp
134+
class $modify(MenuLayer) {
135+
bool init() {
136+
if (!MenuLayer::init())
137+
return false;
138+
139+
Loader::get()->queueInMainThread([self = Ref(this)] {
140+
auto alert = FLAlertLayer::create(
141+
"Title",
142+
"Hi mom!",
143+
"OK"
144+
);
145+
alert->m_scene = self;
146+
alert->show();
147+
148+
return true;
149+
});
150+
}
151+
};
152+
```
153+
This can be useful for alerts that only show up when you open the game, however the issue still applies when the layer uses a transition fade. Since this is the most common scenario for showing popups on layer init, it is generally accepted as good enough.
154+
155+
However, this solution is not completely foolproof for `MenuLayer` either, which uses a transition fade when backing out of submenus like `CreatorLayer`. The most reliable solution is adding the popup in a function like `onEnterTransitionFadeFinished`, however be aware that most functions do not have a custom implementation of this function, which means that you cannot hook it directly. A potential workaround is to create a custom `CCNode` class, which implements this function, and add it to the given layer in `init`.
131156

132157
## Examples
133158

0 commit comments

Comments
 (0)