Python函数未定义
我一直在乌尔西纳制作python 3d游戏。到目前为止还不错,但有一个错误:Python函数未定义,python,function,Python,Function,我一直在乌尔西纳制作python 3d游戏。到目前为止还不错,但有一个错误: File "C:\Users\my name\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\main.py", line 117, in _update __main__.update() File "c:\Users\my name\Desktop\game456.py", lin
File "C:\Users\my name\AppData\Local\Programs\Python\Python39\lib\site-packages\ursina\main.py", line 117, in _update
__main__.update()
File "c:\Users\my name\Desktop\game456.py", line 14, in update
release()
NameError: name 'release' is not defined```
我不理解这个错误。我也见过其他人运行这样的代码。这段代码的灵感来自于另一个在ursina制作游戏的人,所以我自己复制了一些代码。我试着用谷歌搜索答案,但它只是关于一个未定义的变量,等等。查看下面的代码
from ursina import*
from ursina.prefabs.first_person_controller import FirstPersonController
gamebyme = Ursina()
def update():
if held_keys["left mouse"]:
shoot()
else:
release()
class Voxel(Button):
def __init__(self, position = (0,0,0)):
super().__init__(
parent = scene,
position = position,
model = 'map',
origin_y = 0.5,
texture = texture,
color = color.color(0,0,random.uniform(0.9,1)),
scale = 0.1)
class Gun(Entity):
def __init__(self):
super().__init__(
parent=camera.ui,
model='ak_47',
position=Vec2(0.8,-0.1),
color=color.gray,
scale=0.3)
def shoot(self):
self.rotation=Vec3(0,0,10)
def release(self):
self.rotation=None
def reload(self):
self.rotation=Vec3(0,0,0)
plr = FirstPersonController()
gun = Gun()
vxl = Voxel()
gamebyme.run()
在这个函数中
def update():
if held_keys["left mouse"]:
shoot()
else:
release()
您应该指定要从中调用函数的Gun类型对象,例如:
def update(gun):
if held_keys["left mouse"]:
gun.shoot()
else:
gun.release()
您是否定义了
release
和shot
函数?这些都是Gun
的方法,所以请尝试Gun.release()
和Gun.shoot()