Ursina模块中的invoke()方法是什么作用?

3

我导入了Ursina模块,它是一个游戏引擎。

我查找了教程,在代码中使用了invoke()。我尝试查找文档,但似乎没有相关信息。以下是教程中的代码:

from ursina import *

# create a window
app = Ursina()

# most things in ursina are Entities. An Entity is a thing you place in the world.
# you can think of them as GameObjects in Unity or Actors in Unreal.
# the first paramenter tells us the Entity's model will be a 3d-model called 'cube'.
# ursina includes some basic models like 'cube', 'sphere' and 'quad'.

# the next parameter tells us the model's color should be orange.

# 'scale_y=2' tells us how big the entity should be in the vertical axis, how tall it should be.
# in ursina, positive x is right, positive y is up, and positive z is forward.

player = Entity(model='cube', color=color.orange, scale_y=2)

# create a function called 'update'.
# this will automatically get called by the engine every frame.

def update():
    player.x += held_keys['d'] * time.dt
    player.x -= held_keys['a'] * time.dt

# this part will make the player move left or right based on our input.
# to check which keys are held down, we can check the held_keys dictionary.
# 0 means not pressed and 1 means pressed.
# time.dt is simply the time since the last frame. by multiplying with this, the
# player will move at the same speed regardless of how fast the game runs.


def input(key):
    if key == 'space':
        player.y += 1
        invoke(setattr, player, 'y', player.y-1, delay=.25)


# start running the game
app.run()

请帮助我。

附注:我正在使用Linux Mint操作系统。

1个回答

4

它用于延迟调用函数,类似于Unity中的Invoke()。因此,在这种情况下,它将在0.25秒后使player.y减1。

invoke(function, *args, **kwargs)

编辑:来自文档的示例:

def test_func(item, x=None, y=None):
    print(item, x, y)

test_func('test')
invoke(test_func, 'test', delay=.1)
invoke(test_func, 'test1', 1, 2, delay=.2)
invoke(test_func, 'test2', x=1, y=2, delay=.3)`

它解除了我的疑惑。但是为什么'y'是一个参数呢?难道player.y-1不足以达到同样的效果吗? - Adarsh TS
P.S. 我刚刚意识到 Ursina 的开发者已经回答了我的问题。感觉很幸运! - Adarsh TS
1
这就是Python的setattr工作原理。我在我的答案中添加了文档中的示例。 - pokepetter
我完全从你的例子中理解了。我查看了网站上关于这些示例的文档,但没有找到任何信息。如何获取这些文档?这是我查找的地方:链接 - Adarsh TS
1
它在“ursinastuff”下面(我知道这个名字很糟糕)。你也可以使用ctrl+f查找“invoke”。 - pokepetter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接