Swift SKSpriteNode: Complex Sprite Textures? - swift

This is a question regarding best-practice for implementation. For an example, I will reference a simple game called Pixel Claw.
Suppose I had an SKSpriteNode akin to the claw from Pixel Claw, in that what the SKSpriteNode might be ''holding'' is variable (but finite in possibilities, e.g. four different objects).
What the node SKSpriteNode is holding has no agency of itself.
Thus my question is: is it better to use different textures such as a claw, a claw holding object a, a claw holding object b, etc or have two SKSpriteNodes and position the SKSpriteNode with the claw texture to be next to the SKSpriteNode with an object texture?
I am not making a claw game, it was just the first example that popped to my head where both could be plausible solutions. The former being more simplistic - just switch the texture, the latter being more generalizable.
If the latter is the best solution all around, how can one ''pair'' the movements of the two sprites?

I would use the second approach especially if the objects a and b can fall out of the claw at some point.
You should make the texture of a claw, the texture of object a, and the texture of object b.
To make the objects move in sync with the claw, simply add the object as a child of the claw node. This way, moving the claw will cause the object to move as well!
Note that before adding the object as a child, you need to make sure that the object does not have a parent. If it does, you must remove it from its parent before adding it as a child of the claw. The same thing applies when you "release" the object from the claw: you need to first remove the object from its parent (the claw) and then add it as a child of the desired parent.

Related

Parent Game Object to Follow Child

Is there a way to have a parent object follow the combined bounds box of the child objects?
I have child objects which I apply force to for movement (via `AddForceAtPosition). The problem is that they leave the parent behind in world space. I understand that children are relative to the parents position. But is there anything I can do to address this?
I'd really like to keep to keep the force being applied to the child object's rigid bodies as it gives me the physics behaviour I need.
I have experimented with getting the bounds of all child objects, then setting the transform of the parent to the children. But this causes jittery behaviour since moving the parent also moves the children.
There are two ways of doing this, the best way is to change the structure following:
A (parent)
B (child 1)
C (child 2)
to:
Root (new parent with nothing on it)
A (old parent)
B (old child 1)
C (old child 2)
Then move A the way you tried before. You might also want to look into Joints, and see if this might suit your need better. If you don't want two colliders to interact with each other, you might also want to look into Layer Based Collision Detection and Physics.IgnoreCollision().
An alternative that I really wouldn't recommend, is counteracting the movement of the parent with the children. So whenever you move the parent you move the local position of the child you want to stay still the opposite amount. This however could possibly make your physics wonky.

Move a GameObject by its child objects

I wanted to create some evolution like "game". I make some bones, muscles, etc. When I hit play the creature moved but when I've checked the distance it stayed 0. What I've discovered was that all individual child elements moved but the containing GameObject was stil at the same position.
What is the (correct) way to make changes of a child object affect the parent object? Like moving legs to move the person.
The question seemed clear in my head but after reading your questions, remarks and comments I needed elaborate more:
I understand the basic idea of walking animations and such (i think). But here I'm trying to move a creature by itself. At first purely random movement and hopefully later on via a neural network.
In the sample screenshot below you see a creature, the spheres are colored for its weight and the beams between the spheres are the bone/muscle. And my creature limbs do move by pulsating the bone/muscles... but the position of the parent object stay the same. The magenta line is the position of the parent. I would like that the position of the parent would follow the center of its elements.
Your question is a bit unclear to what exactly it is you are after. However, if you want the child-parent behaviour become reveresed in a way that child becomes parent and parent becomes child just for function A() , you can simply swap the child and parents relation in A() itself,
As an example,
void A()
{
child.transform.SetParent(parent.transform.parent);
parent.transform.SetParent(child.transform);
... // your A function
parent.transform.SetParent(child.transform.parent);
child.transform.SetParent(parent.transfotm);
}

SKEmitterNode explosion with force to another Object

I've an explosion in my SKScene and I want this Explosion even effect another objects close to its , Object are in all sides .I want to give them force to run away with this explosion .
It sounds like you need an SKFieldNode implementing a vortexField():
https://developer.apple.com/reference/spritekit/skfieldnode#1654415
SKFieldNode:
A node that applies physics effects to a portion of the scene.
vortextField()
Creates a field node that applies a perpendicular force to physics bodies.

Stored Child transform variable does not update

I have a unity game where I have a bunch of objects that are children of the player object. So when the player runs around, the children objects also run around relative to the player.
At the beginning of the game, I have a transform variable that stores the transform of one of the child objects, then i use that transform variable for the rest of the game to have the enemies use that to chase the children.
Transform childTransform=GameObject.Find("Parent").FindInChildren("child_1").transform;
But it seems this variable that points to the child transform, doesnt get updated to the current value when the parent moves. It just stays in the original position when i assigned it, so the enemy dont follow the current position, it just stays at the original position.
Maybe my code is wrong?
I thought it would get updated automatically to be the world space value of the child relative to the parent.
Anybody knows why? Do I have to constantly look up the child and reassign the variable each Update()? Isnt that a waste of CPU time?
Thanks

TiledMaps and real objects

I'm creating a Super Mario Bros. remake for the iPhone using Cocos2d.
I'm having a hard time understanding how add real objects to a tiled map.
I know that you can add an object-layer to the tiled map and set the position of it,
but I'm not sure how to define WHAT object if should be.
For example, I'm creating a simple Super Mario Bros.
I want to add different blocks to the map, one should hold a star, one should hold a coin, and so on.
So my question, how do I correctly create class-objects in a tiled map?
I have used object layers in the past. For you needs you could create an object layer and drop an object on that layer. Type of box. It could have variables in the object of powerup then the value could be whatever you want. Coin, 1Up, Star etc..!