当在if语句中创建时,我的变量仅在if语句为true时存在

时间:2013-12-18 20:27:35

标签: python variables if-statement

我正在为游戏创建一个代码,当你的鼠标在敌人上方并点击敌人时,你的角色追逐敌人。所以,我创造了敌人作为一个变量因为我不希望角色追逐敌人,如果被点击的只是一个任意对象。然后代码检查属性“敌人”是否在单击的对象内部,如果是,则对象追踪该对象。

if click.positive:
    obj = mouse.hitObject
    if 'enemy' in obj:
        own['attack'] = 1
    else:
        own['attack'] = 0

当拥有['attack'] = 1时,玩家追逐敌人,但编译器说obj不存在。然而,当最初点击鼠标但之后没有点击它时确实存在。我知道该变量仅在单击鼠标时才存在,因为如果未单击鼠标,那么它将永远无法绕过分配变量。但是我怎么能纠正它,所以如果敌人被点击一次,玩家将继续追逐敌人,直到点击其他东西?

这是我得到的错误

NameError: global name 'obj' is not defined

这是完整的代码,如果你想通读它所有它也是在搅拌器中编码,如“import bge”所示:

import bge
cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()

flagEmpty = scene.objects['Flag Empty']
flag = scene.objects['Flag']
enemy = scene.objects['Enemy Physics Box']

mouse = cont.sensors[1]
click = cont.sensors[0]

track = cont.actuators[0]
message = cont.actuators[1]
blur = cont.actuators[2]

hit = mouse.hitPosition
distance = own.getDistanceTo(flagEmpty)
enemyDistance = own.getDistanceTo(enemy)

if 'init' not in own:
    own['init'] = True
    own['fric'] = .85
    own['maxspd'] = 5
    own['speed'] = 0.0
    own['accel'] = .2
    own['attack'] = obj

def main():
    if click.positive:
        global obj
        obj = mouse.hitObject
        if 'enemy' in obj:
            own['attack'] = obj
        if 'enemy' not in obj:
            own['attack'] = obj
    if own['attack'] == obj:
         attack()
    if own['attack'] == None:
         free()
def free():
    if own['attack'] == None:
        cont.deactivate(blur)
    if click.positive:
        if hit.z < -.34:
            if hit.z > -.36:
                flagEmpty.position = hit

    if distance > 1:
        own['speed'] += own['accel']
        cont.activate(message)
        cont.activate(track)
        flag.visible = True
    else:
        own['speed'] *= own['fric']
        cont.deactivate(track)
        cont.deactivate(message)
        flag.visible = False
    speed()
def attack():
    enemyDist = own.getDistanceTo(obj)
    if own['attack'] == obj:
        cont.activate(blur)
        blur.object = obj
    if enemyDist > 1.2:
        own['speed'] += own['accel']
        cont.activate(message)
    else:
        own['speed'] *= own['fric']
        cont.deactivate(message) 

    speed()
def speed():
    if own['speed'] > own['maxspd']:
        own['speed'] = own['maxspd']

    own.setLinearVelocity([0,own['speed'],0], True)
main()

3 个答案:

答案 0 :(得分:0)

你可以这样做:

if 'enemy' in obj:
    own['attack'] = obj

并在没有敌人受到攻击时设置own['attack'] = None

答案 1 :(得分:0)

当您拨打此代码时:

def main():
    if click.positive:
        global obj
        obj = mouse.hitObject
        if 'enemy' in obj:
            own['attack'] = obj
        if 'enemy' not in obj:
            own['attack'] = obj
    if own['attack'] == obj:
         attack()
    if own['attack'] == None:
         free()

每次循环时,您都会将own['attack']obj进行比较。 第一次循环时,obj将不会被定义并将抛出错误。一个简单的改变就是在鼠标点击之外声明global obj,如下所示:

def main():
    global obj
    if click.positive:
        obj = mouse.hitObject
        if 'enemy' in obj:
            own['attack'] = obj
        if 'enemy' not in obj:
            own['attack'] = obj
    if own['attack'] == obj:
         attack()
    if own['attack'] == None:
         free()

答案 2 :(得分:0)

obj未设置为全局范围。我看到contownsceneflagEmptyflagenemymouseclicktrackmessageblurhitdistanceenemyDistance,但不是obj。我试过你的代码和警告消失了,我只是把

obj = None

if 'init' not in own:之前的某个地方。

-Q