| 脚本编写(续)处理 Godot中的几个动作是由回调或虚函数触发,所以没有必要写代码不停的去检查,此外,可以为动画角色做很多事情。
 然而,有一种很常见的情况是每一帧都需要做脚本处理。有两种处理类型,idle处理和fixed处理
 idle处理是由Node.set_process()来激活。一旦激活,Node._process()回调函数会在每一帧都被调用。例如:[mw_shl_code=python,true]func _ready():
 set_process(true)
 
 func _process(delta):
 [dosomething..][/mw_shl_code]
 delta参数描述了自上次调用_process()以后经过的时间(以秒为单位的浮点数)。Fixed处理类似,不同的是只需要与物理引擎同步。
 
 简单的测试方式是在一个场景场景中创建单个的Label节点。如下:[mw_shl_code=python,true]extends Label
 
 var accum=0
 
 func _ready():
 set_process(true)
 
 func _process(delta):
 accum+=delta
 set_text(str(accum))[/mw_shl_code]这将会在每一秒都显示计数器的递增。
 组
 
 节点能被添加到组。对于组织大场景,这是一个简单而实用的功能。有两种方式可以操作,第一种是从UI上面的Groups按钮,如图:
 
  [size=0.83em]1.jpg (33.98 KB, 下载次数: 93)
 下载附件  保存到相册
 
 [color=rgb(153, 153, 153) !important]2014-2-17 11:34 上传 
 
 
 第二种方式是通过代码实现:[mw_shl_code=python,true]func _ready():
 add_to_group("enemies")[/mw_shl_code]  这样,如果所有的玩家偷偷潜入秘密基地,被发现后所有敌人都会被警报的声音通知到,用SceneMainLoop.call_group():
 [mw_shl_code=python,true]func _on_discovered():
 get_scene().call_group(0,"guards","player_was_discovered")[/mw_shl_code]
 在“guards”组中的每一个成员都有一个“player_was_discovered”的函数,可以调用“SceneMainLoop.get_nodes_in_group()”来获取“guards”中的全部节点列表:[mw_shl_code=python,true]var guards = get_scene().get_nodes_in_group("guards")[/mw_shl_code]
 
 SceneMainLoop参见:http://www.godotengine.org/wiki/doku.php?id=class_list:scenemainloop 消息
 Godot有一套消息系统。这通常不需要从脚本中使用,因为它太底层,并且为大多数提供了虚函数。知道他们的存在是很值得高兴的。只需要在你的脚本中添加Object._notification()函数:[mw_shl_code=python,true]func _notification(what):
 if (what==NOTIFICATION_READY):
 print("This is the same as overriding _ready()...")
 elif (what==NOTIFICATION_PROCESS):
 var delta = get_process_time()
 print("This is the same as overriding _process()...")[/mw_shl_code] 在类列表(http://www.godotengine.org/wiki/doku.php?id=class_list:class_list)中的所有类都可以接收消息,但是,重复一下,大多数情况下脚本提供了更简单的可重写函数。
 可重写函数
 
 像前面提到的,更好的使用这些函数。节点提供了许多有用的可重写函数,说明如下:[mw_shl_code=python,true]func _enter_scene():
 pass # When the node enters the active scene, this function is called. Children nodes have not entered the active scene yet. In general, it's better to use _ready() for most cases. # 当节点进入到活动场景,这个方法被调用。但是子节点还没有进入到活动场景中。通常在大多数情况下,更好的选择是用_ready()
 
 func _ready():
 pass # This function is called after _enter_scene, but it ensures that all children nodes have also entered the active scene, and they are all functional. # 这个函数在 _enter_scene()后被调用,但是它确保所有子节点都进入到了活动场景中,并且有效。
 
 func _exit_scene():
 pass # When the node exists the active scene, this function is called. Children nodes have all exited the active scene at this point. # 当节点要退出当前活动场景时调用此方法。子节点同时全部退出当前场景。
 
 func _process(delta):
 pass # When set_process() is enabled, this is called every frame # 当set_process()开启,此方法在每一帧中被调用。
 
 func _fixed_process(delta):
 pass # When set_fixed_process() is enabled, this is called every physics frame # 当set_fixed_process()开启,此方法在每一个物理帧被调用。
 
 func _paused():
 pass #Called when game is paused, after this call, the node will not receive any more process callbacks # 当游戏暂停时调用,调用后,节点将收不到任何更多的回调处理。
 
 func _unpaused():
 pass #Called when game is unpaused # 当游戏取消暂停时调用  [/mw_shl_code]
 |