Kivy小部件不接受属性和命令

2024-09-29 23:31:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做我的第一个Kivy游戏,有敌人在屏幕上跑,玩家应该通过点击来杀死敌人。 我创建了一个敌方类,它是level类的一部分,这两个类都是class小部件的子类。我做了一个函数,它自动将类敌人的实例添加到类级别。我在类敌人中做了一个if循环,它应该检查敌人是否到达了屏幕的末尾。 然后它应该从变量zicie中删除一个数字,然后它应该删除敌人,但这两种方法都不起作用。你知道吗

错误消息包括:

   File "bbgsa1.py", line 47, in Update
     self.parent.remove_widget(self)
 AttributeError: 'NoneType' object has no attribute 'remove_widget'

以及

   File "bbgsa1.py", line 45, in Update
     self.parent.zicie = self.parent.zicie - 1
 AttributeError: 'NoneType' object has no attribute 'zicie'

下面是出现错误的代码部分:

class level(Widget):
    zicie = NumericProperty(10)# the variable containg the life of the player
    zloto = NumericProperty(0)
    e_killed = NumericProperty(0)
    intv1 = NumericProperty(2/1.)
    def __init__(self, **kwargs):
        super(level, self).__init__(**kwargs)
        Clock.schedule_interval(self.Update, self.intv1)
    def Update(self, *args):# this funktion generates enemys
        obj = ROOT.ids.place.ids.level
        obj.add_widget(Enemy(pos=(800,100))) # the widget enemy is added here

class Enemy(Widget):
    imgp = StringProperty('enemy.png')
    velocity = NumericProperty(1)
    intv = NumericProperty(0/60.)
    def __init__(self, **kwargs):
        super(Enemy, self).__init__(**kwargs)
        Clock.schedule_interval(self.Update, self.intv)

    def Update(self, *args):# the funktion that lets the enemy move
        self.x -= self.velocity
        if self.x < 1:# checks if the enemy widget reached the end
            self.velocity = 0#m akes the enemy stop moving
            self.parent.zicie = self.parent.zicie - 1# the variable zicie that is not found
            self.parent.remove_widget(self) # this command is also not working

    def on_touch_down(self, touch):# the funktion, that lets the enemy die
        if self.collide_point(*touch.pos):
            self.velocity = 0
            self.imgp = 'enemyd.png'
            self.parent.e_killed += 1
            self.parent.zloto += 10
            self.parent.remove_widget(self)

Tags: theselfifinitdefupdatewidgetlevel
1条回答
网友
1楼 · 发布于 2024-09-29 23:31:42

这些错误是因为自我父母在行运行的点处没有。我还没有详细检查过,但有一种可能会出现这种情况,那就是时钟已经安排好了自我更新即使敌人已从其父级移除,函数也会被调用。你知道吗

你应该注意在敌人被从屏幕上移除后取消计划功能(以避免计划功能的数量增加),但也可以通过检查自我父母在尝试根据价值做事之前是没有的。你知道吗

相关问题 更多 >

    热门问题