在Ursina引擎中,如何隐藏实体对象Python 3.8

huangapple go评论183阅读模式
英文:

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=&#39;sphere&#39;, scale=4, color=color.azure) 
    invoke(test_func, f, delay=.2)



def update(): 
    if held_keys[&#39;1&#39;]:  flashlight()

EditorCamera()
app.run()

huangapple
  • 本文由 发表于 2023年3月12日 14:15:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定