我正在尝试使用活塞窗口(0.77.0)库在 Rust 中编写游戏。从他们的hello world示例开始,我想我会先将渲染逻辑分离为使用 Event 作为参数的方法,因为根据文档,它是由window.next()
.
use piston_window::*;
pub struct UI<'a> {
window: &'a mut PistonWindow,
}
impl <'a> UI<'a> {
pub fn new(window: &mut PistonWindow) -> UI {
UI {
window,
}
}
fn render(&self, event: &Event) {
self.window.draw_2d(&event, |context, graphics| {
clear([1.0; 4], graphics);
rectangle(
[1.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 100.0, 100.0],
context.transform,
graphics);
});
}
pub fn run(&mut self) {
use Loop::Render;
use Event::Loop;
while let Some(event) = self.window.next() {
match event {
Loop(Render(_)) => self.render(&event),
_ => {}
}
}
}
}
然而,这以错误结束:
self.window.draw_2d(&event, |context, graphics| {
特征piston_window::GenericEvent
未实现&piston_window::Event
没有提取渲染方法的代码按预期工作。
pub fn run(&mut self) {
use Loop::Render;
use Event::Loop;
while let Some(event) = self.window.next() {
match event {
Loop(Render(_)) => {
self.window.draw_2d(&event, |context, graphics| {
clear([1.0; 4], graphics);
rectangle(
[1.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 100.0, 100.0],
context.transform,
graphics);
});
},
_ => {}
}
}
}
我怎样才能提取这个?有什么我忽略的吗?