Getting Position.Y of parent node with physicsBodies - sprite-kit

Im trying to see when my circleGroup Class Object has left the screen
(am pretty new at this so any suggestions welcome!)
including any other way to remove the class once it is off screen
circleGroup Class contains a blank SKSpriteNode, and 8 other sprites are grouped around the blank SKSpriteNode as children (these have physics),
Gamelayer:
Always logs (0,0)
NSLog(#"_mycircleGroup.position.y #%f",_mycircleGroup.position.y);
//outputs myArc.position.y #0.000000
which means I can't check when Class is off screen
if (_mycircleGroup.position.y < 0)
{
NSLog(#"_mycircleGroup off screen");
//[_mycircleGroup removeFromParent];
//[self myMethod];
}
Any ideas how I might read position?
with thanks
:)
Solution,
I needed to be logging the parent node inside of the class
so in this case I have a class and in it is a parent sprite which i need to track (Since it seems like nodes with physics don't track)
if (self.myCircleClass.parentSpriteThatEveryThingIsAddedTo.position.y < 0)
{
NSLog(#"my arc off screen");
//[twoSpritesWithParticlesBridge removeFromParent];
//[self addStaticLinkedSpriteWithParticles];
}

As above with thanks to WangYudong for jogging my logic :P
Solution, I needed to be logging the parent node inside of the Class so in this case I have a class and in it is a parent sprite which I need to track (Since it seems like nodes with physics don't track, so I couldn't track te entire object, just the object without physics...)
if (self.myCircleClass.parentSpriteThatAllOtherSpritesParentedTo.position.y < 0)
{
NSLog(#"my arc off screen");
//[twoSpritesWithParticlesBridge removeFromParent];
//[self addStaticLinkedSpriteWithParticles];
}
Take everything with a pinch of salt, non definite and completely questionable! (since I'm new at coding), but hope it helps someone :)

Related

How to check for gameObject while Grid Snaping in Unity2D

I am trying to make a game which allows moving objects by snapping them on the grid, I already figured out to snape them to grid but there is one thing little problem, I want to check if there is already a game object placed on that same grid so that I won't let the dragging game object snap to that same spot but the thing is that I have a different game object shapes.
see for yourself
Click to see the image
how can I achieve that?
Since you're on a square grid I think the best way to do this is with Physics2D.Boxcast(). Basically what you're doing is casting a box at the snap vector before moving the game object.
So in your code before you move the game object to the snap location:
RaycastHit2D hit = Physics2D.BoxCast(snapTo, boxSize, 0.0f, Vector2.zero);
if (hit == null)
{
// We're clear to move
}
else
{
// Something is in the way
}
Where snapTo is the Vector2 of the location you're going to snap to and boxSize is a Vector2 equal to the size of one grid position (you might need to play around with this a bit). The last two arguments, 0.0f refers to the angle of the box, which we don't need so just set it to zero and Vector2.zero is the direction of the cast, but we're casting in one spot so this also doesn't matter.
I'm assuming that only one game object can occupy the space at once, so there will only ever be one hit. If there's a chance for more than one you can change it to RaycastHit2D[] hits and Physics2D.BoxCastAll then check if hits.Length is greater than 0.
I had some troubles with Physics2D.Boxcast() , so instead i used Physics2D.OverlapBox() and it is working just fine.
isColl = Physics2D.OverlapBox(ObjectToMove.position, size, 0f, layerM);
if (isColl == true)
{
// Something is in the way
}
else
{
//Clear to go
}

How can I remove all nodes from a scenekit scene?

Hi I am trying to remove all nodes from my Scenekit scene but I cannot for the life of me figure out a way.
It seems logical to me that there must be a function for doing this automatically but I cannot find it.
In context, I am trying to remove all nodes so I can reset my scene, which will happen reasonably often. Perhaps there is another way of doing this and I would be fine with that, I'm not stuck with having to remove all nodes.
Thanks!
Try this (assuming you are using Swift):
rootNode.enumerateChildNodes { (node, stop) in
node.removeFromParentNode()
}
Works for me.
For me worked like below:
sceneView.scene.rootNode.enumerateChildNodes { (node, stop) in
node.removeFromParentNode() }
you can either create a new scene or call -[SCNNode removeFromParentNode] on every child node of the scene's rootNode
Where you need to remove all of your nodes, call this (if your scene isn't self, change it):
for (SCNNode *node in [self children]) {
[node removeFromParent]
}
Additionally, if you need to remove each node except for some, call this (say, we don't want to remove 3 nodes, and they're named a, b, and c)
for (SCNNode *node in [self children]) {
if (![node.name isEqualToString:#"a"] && ![node.name isEqualToString:#"b"] && ![node.name isEqualToString:#"c"]) {
[node removeFromParent]
}
}
Hope this helps!
Here is a solution in Objective-C (Yes it still exists!)
while (self.sceneView.scene.rootNode.childNodes.count) {
NSArray<SCNNode *> *nodes = self.sceneView.scene.rootNode.childNodes;
[nodes[0] removeFromParentNode];
}
Modifying a collection while looping through it is not good practice; here's an alternative:
while let n = node.childNodes.first { n.removeFromParentNode() }
the simplest way I found to remove all nodes from a scene is:
self.removeAllChildren()
This worked well for me in XCode version 7.2

SpriteKit - How do I check if a certain set of coordinates are inside an SKShapeNode?

In my game, I'm trying to determine what points to dole out depending on where an arrow hits a target. I've got the physics and collisions worked out and I've decided to draw several nested circular SKShapeNodes to represent the different rings of the target.
I'm just having issues working out the logic involved in checking if the contact point coordinates are in one of the circle nodes...
Is it even possible?
The easiest solution specific to Sprite Kit is to use the SKPhysicsWorld method bodyAtPoint:, assuming all of the SKShapeNode also have an appropriate SKPhysicsBody.
For example:
SKPhysicsBody* body = [self.scene.physicsWorld bodyAtPoint:CGPointMake(100, 200)];
if (body != nil)
{
// your cat content here ...
}
If there could be overlapping bodies at the same point you can enumerate them with enumerateBodiesAtPoint:usingBlock:
You can also compare the SKShapeNode's path with your CGPoint.
SKShapeNode node; // let there be your node
CGPoint point; // let there be your point
if (CGPathContainsPoint(node.path, NULL, point, NO)) {
// yepp, that point is inside of that shape
}

Creating a sprite during didBeginContact

I've been working on some SpriteKit tutorials. I understand the whole collision thing and have verified with NSLog that a collision is being registered between my two objects. However for some really strange reason my sprite is not being created (or rather shown) when it's done during didBeginContact.
- (void)didBeginContact:(SKPhysicsContact *)contact
{
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (CNPhysicsCategoryPlayer | CNPhysicsCategoryRock))
{
NSLog(#"ouch");
SKSpriteNode *bigOuch = [SKSpriteNode spriteNodeWithImageNamed:#"star"];
bigOuch.position = CGPointMake(200, 200);
[self addChild:bigOuch];
}
}
I get the ouch log message but no sprite appears.
I have tried the same (sprite creation) code in other parts of my program and have no issues. What am I doing wrong?
I was stuck on the exact same issue a while back. You can create a SKSpriteNode and add it to the view but it does not get displayed. The short of it is that I ended up creating an array and adding any sprites I needed to create during the didBeginContact phase. During the update phase I checked the array and added them to my view. Just remember to empty the array after you are done. Otherwise you will end up with the same sprite being added over and over again.

enum in Singleton cocos2d

What do you recommend me if I have two layers (which are added to a parallaxNode) and on each layers I've added sprites which I want to restrict the order that the player won't be able to remove sprite5 until it removes first the previous ones: sprite1, sprite2, sprite3 and sprite4 and so one.
I'm using Juan's example for dynamically touch detection
What will be the dynamical option for this?
UPDATE:
if([sprite tag] == tag_collection_1 && [sprite GetCanTrack]) {
//Set others to NO & call remove sprite method
[[TSprite FindByTag:tag_collection_number] SetCanTrack:NO];
[self removeSpriteMethod:touchLocation];
} else if ([sprite tag] == tag_collection_number && [sprite GetCanTrack]) {
// and so on
}
Use a fifo stack implementation. If you want to remove the sprite, you first check if it's at the bottom of the stack and when you do remove it, you pop it from the stack as well. Practically you will have an NSMutableArray and look at the first item if the object is in that position before you remove it. If you have added 1,2,3,4,5 in that order and want only to be able to remove them in the same order you check if the object you are trying to remove is at index 0 in the array first.