英文:
How to hide entity object in ursina engine python 3.8
问题
我想创建一个手电筒,当你按下1键时出现光束,但我希望它在一秒后消失。我已经写了代码,但我的代码不起作用。
我的代码:
import time
from ursina import *
app = Ursina()
def flashlight(): # 手电筒代码
f = Entity(model='sphere', scale=4, color=color.light_gray)
time.sleep(1.0) # 暂停
f.visible = False # 消失
def update():
if held_keys['1']:
flashlight()
app.run()
英文:
I want to create a flashlight, when you press 1 beam appears, but I want it to disappear after a second. I made it, but my code doesn't work.
My code:
from ursina import *
app = Ursina()
def flashlight(): # flashlight code
f = Entity(model='sphere', scale=4, color=color.light_gray)
time.sleep(1.0) #pause
f.visible = False #disappearing
def update():
if held_keys['1']: flashlight()
app.run()```
</details>
# 答案1
**得分**: 0
为了使`Entity`在**1秒**后出现和消失,您可以使用Ursina的[invoke][1]函数。以下是它在您的代码中的实现:
```python
from ursina import *
app = Ursina()
def test_func(e):
e.visible = False
def flashlight(): # flashlight code
f = Entity(model='sphere', scale=4, color=color.azure)
invoke(test_func, f, delay=1.0)
def update():
if held_keys['1']: flashlight()
EditorCamera()
app.run()
英文:
To make the Entity
appear and disappear after 1 second, you can use Ursina' invoke function. Here is its implementation in your code:
from ursina import *
app = Ursina()
def test_func(e):
e.visible = False
def flashlight(): # flashlight code
f = Entity(model='sphere', scale=4, color=color.azure)
invoke(test_func, f, delay=.2)
def update():
if held_keys['1']: flashlight()
EditorCamera()
app.run()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论