Skip to content

Commit 0fbddc1

Browse files
committed
handy helpers for looking up "my scene", "my layer" and killing something
1 parent 5db2c89 commit 0fbddc1

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

cocos/actions/instant_actions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class DoAction(InstantAction):
160160
Example::
161161
162162
action = Repeat( dance )
163-
sprite.do( go_home + DoAction( dance ) )
163+
sprite.do( go_home + DoAction( action ) )
164164
"""
165165
def init(self, action):
166166
self.action = action

cocos/cocosnode.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,26 @@ def get_ancestor(self, klass):
336336
if parent:
337337
return parent.get_ancestor( klass )
338338

339+
@property
340+
def scene(self):
341+
'''Get the Scene this cocosnode (eventually) belongs to.
342+
343+
Is None if there is no Scene ancestor.
344+
'''
345+
# late import to avoid circular reference
346+
from cocos import scene
347+
return self.get_ancestor(scene.Scene)
348+
349+
@property
350+
def layer(self):
351+
'''Get the Layer this cocosnode (eventually) belongs to.
352+
353+
Is None if there is no Layer ancestor.
354+
'''
355+
# late import to avoid circular reference
356+
from cocos import layer
357+
return self.get_ancestor(layer.Layer)
358+
339359
#
340360
# Transform properties
341361
#
@@ -417,6 +437,12 @@ def add(self, child, z=0, name=None ):
417437
child.on_enter()
418438
return self
419439

440+
def kill(self):
441+
'''Remove this object from its parent, and thus most likely from
442+
everything.
443+
'''
444+
self.parent.remove(self)
445+
420446
def remove( self, obj ):
421447
"""Removes a child from the container given its name or object
422448

cocos/scene.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ def __init__(self, *children):
103103
self.music = None
104104
self.music_playing = False
105105

106+
self.init()
107+
108+
def init(self):
109+
'''Perform scene initialisation at creation time.
110+
'''
111+
pass
112+
106113
def on_enter(self):
107114
for c in self.get_children():
108115
c.parent = self

0 commit comments

Comments
 (0)