SKSpriteNode,添加到父钩子/从父钩子中删除 [英] SKSpriteNode, added to/removed from parent hook
问题描述
当 SKSpriteNode(或 SKNode)被添加到父节点或从父节点移除时,类中是否有任何(最佳实践)方法来挂钩事件?
Is there any (best practice) way within the class to hook the events when a SKSpriteNode (or SKNode) is added onto and removed from a parent node?
推荐答案
不需要 Kobold Kit,您可以子类化一个 SKSpriteNode(或任何 SKNode 实际上)并扩展 removeFromParent
SKNodes拥有的功能.
Without the need of Kobold Kit, you can sub-class a SKSpriteNode (or any SKNode for the fact) and extend the removeFromParent
function that SKNodes own.
例如:
FLSprite.m:
#import "FLSprite.h"
@implementation FLSprite
-(void)removeFromParent {
[super removeFromParent];
NSLog(@"I print when I'm Removed");
//here's where you'll add your hooking functions
}
@end
MyScene.m:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
FLSprite* sprite = [FLSprite spriteNodeWithColor:[UIColor blackColor] size:CGSizeMake(200, 100)];
[sprite setPosition:CGPointMake(100.0, 50.0)];
[self addChild:sprite];
[sprite removeFromParent];
}
return self;
}
至于添加一个孩子,因为你添加了一个孩子(在大多数情况下)[self addChild:node];
你需要扩展 addChild
在您要添加的视图中.例如,您将在 MyScene.m
中添加以下内容,因为我们正在将精灵添加到该视图中
As for adding a child, since you add a child as such (in most cases) [self addChild:node];
you'll need to extend the addChild
in the view you are adding to. For example you'll add the following to your MyScene.m
, since we are adding the sprite to that view
-(void)addChild:(SKNode *)node {
[super addChild:node];
NSLog(@"added child");
}
<小时>
正如他在帖子中解释的那样,这几乎就是 Steffen Itterheim 为实现此功能所做的工作.
This is pretty much what Steffen Itterheim did to acheive this functions, as he explained in his posting.
这篇关于SKSpriteNode,添加到父钩子/从父钩子中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!