简体   繁体   English

在cocos2D中更改CCSprite的父级

[英]change parent of a CCSprite in cocos2D

I'm kind of new in cocos2Dm and i'm facing a problem that i can't solve So far I have 2 sprites (let's call them tables) in the layer , that is touchable, one of them has added other sprites, that I wanna move from one table to another one, i'm recognizing the objets that I touch, & moving them around, My problem is that I can't change the parent to be the other table, I tried to remove the object from parent in different ways & add the no the new parent, but it doesn't seem to work, the object is duplicated cause it's not being removed, I even tried to remove them when I touch it. 我是cocos2Dm的新手,我面临的一个问题是我无法解决。到目前为止,我在该图层中有2个Sprite(我们称它们为表),这是可触摸的,其中一个添加了其他Sprite,我想从一张桌子移动到另一张桌子,我认识到我触摸的对象,然后四处移动,我的问题是我无法将父对象更改为另一张桌子,我试图从父对象中删除对象以不同的方式添加no并添加新的父对象,但它似乎不起作用,该对象已被复制,因为未将其删除,我什至尝试在触摸它们时将其删除。 I'm storing the objects in _objectsToDrag & calling this function in touchbegan: 我将对象存储在_objectsToDrag中,并在touchbegan中调用此函数:

- (void) lookForObjectWithTouchLocation:(CGPoint)touchLocation
{
    for (RICCObject *object in _objectsToDrag) {
        if (CGRectContainsPoint(object.boundingBox, touchLocation)) { 
            _selectedObject = object;
        }
    }
    if (_selectedObject) {
        [self objectSelectedWithLocation:touchLocation];
    }
}

And then using _selectedObject to move it around, is this the problem? 然后使用_selectedObject移动它,这是问题吗? should I use aributes instead local objects in an array? 我应该在数组中使用aributes而不是局部对象吗?

Any help is welcome Thank you in advance 欢迎任何帮助,提前谢谢

To move an instance of a class that derives from CCNode (like CCSprite, CCLabelTTF etc) from one parent node to another, follow this process: 要将从CCNode派生的类的实例(例如CCSprite,CCLabelTTF等)从一个父节点移动到另一个父节点,请遵循以下过程:

// get yourNode in whatever way fits your implementation ...
CCNode* nodeToMove = yourNode;
// not cleaning up leaves actions running
[nodeToMove removeFromParentAndCleanup:NO];
// add the removed node to its new parent node
[newParentNode addChild:nodeToMove];

This process works regardless of how or where else you store the nodes. 无论您如何或在何处存储节点,此过程都有效。

Note that if you see nodes being duplicated, you either create a new node without removing the old node or you have two nodes to begin with. 请注意,如果看到节点重复,则可以创建一个新节点而不删除旧节点,或者可以从两个节点开始。 In cocos2d, a CCNode can only have one parent and trying to add a node that already has a parent to another node will prompt you with an error message. 在cocos2d中,CCNode只能有一个父节点,并且尝试将已经具有父节点的节点添加到另一个节点将提示您一条错误消息。 If you experience duplicated nodes respectively removing a node from its parent still keeps it on screen try to find the cause for that first. 如果您分别遇到重复的节点,则从其父节点中删除一个节点仍会使其停留在屏幕上,请尝试首先查找原因。 This is something that doesn't happen under normal circumstances, except when you actually create multiple versions of the same node. 这是通常情况下不会发生的事情,除非您实际上创建了同一节点的多个版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM