change parent of a CCSprite in cocos2D - iphone

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.
I'm storing the objects in _objectsToDrag & calling this function in 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? should I use aributes instead local objects in an array?
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:
// 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. 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.

Related

Switch between parents - SpriteKit

In my game I have two SKNodes called "Center" rotating.
I have several objects as the child of Center (the SKNode). I did this so I could let these objects orbit around Center. Although at certain points I want to switch to other SKNodes - "Center2". In this I have named one of the objects "Onject", which switched between parents I need to attach and detach at certain points in my game. I have tried, after doing self.addChild(Object) then doing Center.addChild(Object). My game crashed.
How do I switch between parents for objects?
there is a method called move(toParent:SKNode) to do exactly what you want, it will also fix the coordinates for you.
If you call self.addChild(Object) and then Center.addChild(Object) your game will crash, because the object will then have two parents. You cant add the object twice as a child for different nodes.
If you want to switch the parent, you have to remove it first from its parent, so the flow will be as follows:
self.addChild(Object) (then just perform your game logic)
once its time to switch the parent, follow these steps:
Object.removeFromParent()
Center.addChild(Object)
Then your object has a new parent

Manipulating alpha property vs adding/removing node

If I want a node or nodes (in this case a "panel" with two "buttons" - e.g.: a node to represent the panel, and two more nodes on that panel to represent buttons) to appear and be available to call actions (with the buttons using touchesBegan()), I seem to be able to do it by creating the nodes (color Sprites) in my .sks scene and using alpha = 0/1. I can also do it programatically by creating the node in .swift class and using .addChild()/.removeFromParent() functions.
Are these interchangeable approaches or is there a danger of using alpha since the nodes are still present, though not seen?
In Spritekit hidden and alpha properties are equal to 0 as default value.
From Apple regarding hidden objects
objects are not rendered. However the still exist in the scene and
continue to interact with it in other ways
So that would be the same thing as having created the object and not added it to a parent
Having just run a test on an object that was detected in the touchesBegan func.
When the object had .alpha = 0 it was still detected in the touchesBegan
When the object was created but not added to the parent it was still detected in the touchesBegan
So I think both methods are comparable
Edit> I stand corrected they are not comparable, please feel free to unselect my answer. Please comments to see pitfalls of using alpha = 0

What does self.addChild Do?

This is sort of a stupid question, but what does the function self.addChild() do?
I am familiar with this function, and how to use it, but I am not exactly sure what it adds the child to.
For instance, I have created and designed an SKShapeNode called spinnyNode. Then I call the function:
func touchDown(atPoint pos : CGPoint) {
if let n = self.spinnyNode?.copy() as! SKShapeNode? {
n.position = pos
n.strokeColor = SKColor.black
self.addChild(n)
}
What is the parent in this situation? Is it the view that the node is being created in?
Thank you so much for your time, and your answering of stupid questions.
Lets break it down:
self in this case, refers to the SKScene which your are currently in. So if you're in your gameScene, the node will be added to the gameScene. Note that you can add nodes to other nodes, so if you have an SKNode named gameLayer, you could add a node to gameLayer, which would then be added to the scene. (That would look like this: gameLayer.addChild(node)) If there is no specified location for the node, it default chooses self
addChild(node) is the function that actually adds the specified node to the specified location (see above). You tell the compiler which node to add to the scene by putting its declared name in the brackets.
Make sure you're setting the nodes attributes (position, size, etc...), as they stay the default values until you change them.
The parent is essentially the scene, and what you are doing is adding that node to the scene. Without this call, the node would be created, but never passed to the scene, so it wouldn't spawn.
Think of it as doing some homework for a teacher, then the addChild submits it. If you don't submit it, it isn't used. So whatever class your function is in, be it SKScene etc, that is the parent the node is being passed to.
And don't worry, it isn't a stupid question, learning the foundations of how these functions work is a great way to build better apps!

Wrong node removed when using removeFromParent in spritekit

I want a node to be removed from parent after it makes contact with an object, but the node is spawned continuously, with multiples of the same node on the screen at once. The issue is, whenever the node makes contact and removeFromParent is called, it isn't removed, but another node that hadn't made contact yet is. I'm wondering what I can do to make sure that the node that is removed is the one that made contact and not a node of the same type.
You need a way to make each instance unique. One way to do this is to assign a unique name to each node instance. First you need to create a counter:
#property (nonatomic) int myCounter;
Then you use the counter as part of a node's name:
myCounter++;
myNode.name = [NSString stringWithFormat:#"myNode-%i",myCounter];
Instead of adding the nodes to self as childs. Add them to another node you make. Then you could use this line of code to remove all of the nodes within the bigger node:
bigNode.addChild(smallNode) //Add smallNode to bigNode instead of self
bigNode.removeAllChildren() //Removes all of the smallNodes since they all are children of bigNode

Creating a class to run a single action on multiple sprites

I'm trying to rotate numerous sprites (about 48 different ones) around an arbitrary point (using this article: http://indiedevstories.com/2012/08/14/custom-cocos2d-action-rotating-sprite-around-arbitrary-point/ ) and I managed to create the custom category for it, but it only works on a single sprite. I've looked around on the site and tried to use runAction:[action copy] but it makes the copies rotating points crazy numbers for some reason. Then I tried to create a method for the actions and just call the method, but I keep getting errors for that as well. I've tried so many different solutions but no luck.
So my question is, is there a way I can create another class that holds all of my sprites, and then run a single method to run an action on all of the sprites of the class?
Assuming you have an array called spriteArray containing all sprites you wish to rotate, it's as simple as:
for(CCSprite *sprite in spriteArray)
{
CCRotateAroundBy *rotateAround = [CCRotateAroundBy actionWithDuration:1.0 angle:90 rotationPoint:screenCenter];
[sprite runAction:rotateAround];
}