gpt4 book ai didi

ios - 重新定义 SKNode 意味着我必须将节点重新添加为子节点?

转载 作者:行者123 更新时间:2023-11-29 12:49:00 25 4
gpt4 key购买 nike

我的代码中有一个案例,如果我重新定义它指向的内容,我似乎必须重新添加一个子节点,但我不太明白为什么。

例如:

_worldNode = [SKNode node];
_childNode = [SKNode node]; //set childNode to point to a node here.
_childChildNode = [SKSpriteNode spriteNodeWithImageNamed:@"test"];

[self addChild:_worldNode];
[_worldNode addChild:_childNode]; //add childNode to the worldNode
[_childNode addChild:_childChildNode];

//Later on...

[_childNode removeAllChildren]; //1. do I need to do this to clean up _childChildNode first?
_childNode = [SKNode node]; //set childNode to another node.
_anotherChildChildNode = [SKSpriteNode spriteNodeWithImageNamed:@"test"];
[_childNode addChild:_anotherChildChildNode];

//2. do I need to re-add _childNode to _worldNode now?

执行 1 似乎释放了我游戏中的很多节点,但我不认为这是必要的,因为我将 _childNode 指向正下方的另一个节点,所以我认为我最初指向的节点(和_childChildNode) 的引用计数将为 0 并会被释放?

我也不认为我需要执行 2,因为我之前已经将 _childNode 添加到 _worldNode,但如果我不添加此行(可能来自 removeAllChildren 调用),我的游戏似乎就会变成空白。

你知道我是否遗漏了什么吗?

最佳答案

Doing 1 seems to free up a lot of nodes in my game, but I didn't think that was necessary as I'm pointing _childNode to another node right below, so I thought the node I was originally pointing to (and _childChildNode) would have a refcount of 0 and would be deallocated?

您需要从其父节点中删除 _childNode:

[_childNode removeFromParent];

在您这样做之前,_childNode 会保留在内存中,因为父节点持有对 _childNode 的引用。此外,如果您调用 removeFromParent,则调用 removeAllChildren 是多余的。

_childNode中分配了一个新的节点后,你必须再次将它添加到_worldNode中,因为它是一个全新的节点:

_childNode = [SKNode node];
_anotherChildChildNode = [SKSpriteNode spriteNodeWithImageNamed:@"test"];
[_childNode addChild:_anotherChildChildNode];
[_worldNode addChild:_childNode];

关于ios - 重新定义 SKNode 意味着我必须将节点重新添加为子节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22879127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com