返回介绍

Project: A Platform Game

发布于 2025-02-27 23:45:58 字数 1826 浏览 0 评论 0 收藏 0

Game over

The most obvious solution would be to make lives a variable that lives in runGame and is thus visible to the startLevel closure.

Another approach, which fits nicely with the spirit of the rest of the function, would be to add a second parameter to startLevel that gives the number of lives. When the whole state of a system is stored in the arguments to a function, calling that function provides an elegant way to transition to a new state.

In any case, when a level is lost, there should now be two possible state transitions. If that was the last life, we go back to level zero with the starting amount of lives. If not, we repeat the current level with one less life remaining.

Pausing the game

An animation can be interrupted by returning false from the function given to runAnimation . It can be continued by calling runAnimation again.

To communicate that the animation should be interrupted to the function passed to runAnimation so that it can return false , you can use a variable that both the event handler and that function have access to.

When finding a way to unregister the handlers registered by trackKeys , remember that the exact same function value that was passed to addEventListener must be passed to removeEventListener to successfully remove a handler. Thus, the handler function value created in trackKeys must be available to the code that unregisters the handlers.

You can add a property to the object returned by trackKeys , containing either that function value or a method that handles the unregistering directly.

This is a book about getting computers to do what you want them to do. Computers are about as common as screwdrivers today, but they contain a lot more hidden complexity and thus are harder to operate and understand. To many, they remain alien, slightly threatening things.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文