小编Vin*_*oso的帖子

Rust - 将事件侦听器添加到 WebAssembly 游戏中

我正在尝试在网络程序集中创建一个游戏。我选择用 Rust 来准备它并使用 Cargo-Web 进行编译。我设法获得了一个有效的游戏循环,但由于 Rust 借用机制,我在添加 MouseDownEvent 侦听器时遇到了问题。我非常喜欢编写“安全”代码(不使用“不安全”关键字)

此时,游戏只需将红色框从 (0,0) 移动到 (700,500),速度取决于距离。我希望下一步使用用户单击更新目的地。

这是游戏的简化且有效的代码。

静态/index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <title>The Game!</title>
</head>

<body>
    <canvas id="canvas" width="600" height="600">
    <script src="game.js"></script>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

src/main.rs

mod game;

use game::Game;

use stdweb::console;
use stdweb::traits::*;
use stdweb::unstable::TryInto;
use stdweb::web::document;
use stdweb::web::CanvasRenderingContext2d;
use stdweb::web::html_element::CanvasElement;

use stdweb::web::event::MouseDownEvent;

fn main()
{
    let canvas: CanvasElement = document()
        .query_selector("#canvas")
        .unwrap()
        .unwrap()
        .try_into()
        .unwrap();

    canvas.set_width(800u32);
    canvas.set_height(600u32);


    let context = canvas.get_context().unwrap();

    let game: Game = Game::new();

    // canvas.add_event_listener(|event: MouseDownEvent|
    // { …
Run Code Online (Sandbox Code Playgroud)

events game-loop rust webassembly

2
推荐指数
1
解决办法
3620
查看次数

在 Dropdown onchange 事件上更改小部件

我有一个包含多个选项的下拉菜单。为了简单起见,让我们说它们是:“A”、“B”和“C”。

这是我的代码片段:

children: <Widget>[
                      FormBuilder(
                        key: _fbKey,
                        autovalidate: true,
                        child: Column(
                          children: <Widget>[
                            FormBuilderDropdown(
                              attribute: "value",
                              decoration: InputDecoration(
                              labelText: "Choose something?"),
                              hint: Text('Select Option'),
                              validators: [FormBuilderValidators.required()],
                              items: user.option.map((v) {
                                return DropdownMenuItem(
                                    value: v,
                                    child: ListTile(
                                      leading: Image.asset(
                                        'assets/img/image.png',
                                        width: 50,
                                        height: 50,
                                      ),
                                      title: Text("${v.option}"),
                                    ));

                              }).toList(),
                            ),
                          ],
                        ),
                       // if v.option == "A" is selected here build Widget A()
                       // if v.option == "B" is selected here build Widget B()
                       // if v.option == "C" is selected here …
Run Code Online (Sandbox Code Playgroud)

dart drop-down-menu flutter

0
推荐指数
1
解决办法
3380
查看次数

标签 统计

dart ×1

drop-down-menu ×1

events ×1

flutter ×1

game-loop ×1

rust ×1

webassembly ×1