I have multiple spritenodes in the Scene with the same Name. If I want to edit them all in the .swift file, just one reacts to the conditions and all of the other Nodes stays like in the scene.
For example if I want to let them move, just one moves... How can I fix it? All nodes are completely the same except size and position...
Thanks!
Just because nodes share the same name doesn't mean they share the same properties. This is the same with people. If I meet 2 people named John, and I shaved the first Johns head, it doesn't mean the second John is bald.
What you need to do is enumerate through all of the children with the same name.
In Sprite Kit, we have a method for that, called enumerateChildNodes and you would use it like this:
node.enumerateChildNodes(withName:"John")
{
//[unowned self] Include this if you are using self to avoid a retain cycle
node,stop in
node.head = shaved
}
Related
I create game with spriteKit in Xcode. I need to create carousel and move 4 nodes around one pillar.
carousel
How can I move some bind nodes? It`s necessary to change zPosition when node approaches the pillar and when node comes out from behind the pillar. How it can be implemented?
To group your nodes together, just create a new SKNode, add it to the scene, and anything that needs to be grouped to this, just call move(toParent:) on the nodes that need to be moved.
let groupedNode = SKNode()
self.addChild(groupedNode)
node1.move(toParent:groupedNode)
As for zPosition, it is relative to the parent, so if your pillar is at 2, and your group is at 1, to make your individual nodes above the pillar, you need to make the node 2. This will make the node zPosition really 3 (node + group) placing it above the pillar.
I have more SKSpriteNodes, randomly generated (then 3, 4 or more) that all belong to the category CNPhysicsCategoryA and I have another single sprite that belong to the category CNPhysicsCategoryB. I need to check when all sprites in category CNPhysicsCategoryA collide all toghether simultaneously to the one in CNPhysicsCategoryB. There's a way to do this? Maybe with allContactedBodies, but how use it, can anybody help me with the code? Thank you so much...
Make a variable called numberOfCollisions. Inside the update function make it equal to 0. The update function is the first thing that gets called in the rendering cycle so at the start of every frame numberOfCollisions is equal to zero.
Then every time the didBeginContact function is called increase it by one and check if it is equal to the number of objects in CNPhysicsCategoryA.
I have a symbol of a person standing, he have child movieClips like hands, legs, head, body....so then if i press a button or click in the stage how do i need to change the animation instantly?
I suppose:
1) create a new movieClip that has all the same parts but animated as walking(for example) and then make something like:
if(keypressed)
{
person.replaceSymbol(myNewAnimatedSymbol); //method that not exist
}
2) for example if my standing animation longs from 1 to 30, i can make another animation in the same line let's say from 31 to 60 where he is running and then write:
if(keypressed)
{
person.legs.gotoAndPlay(31); //in the 60 frame he go back to 31 by using gotoAndPlay(31)
person.hands.gotoAndPlay(31);
person.body.gotoAndPlay(31); //and so on...
}
Can you give me an advice of which of methods I need to use? or if you have a better idea how to make it...
Hm-m-m. I'd say your second approach is easier to both draw and control, especially if it'll eventually come to reskinning your MC, say your person will get dressed in a cloak that's attached to the body and arms, your "new animated symbol" will likely require a change to accommodate that clothing, while in case of one symbol you can assign its parts another look, like say person.hands.cover=clothing.handpart; person.legs.cover=clothing.legpart etc, and then the animation will contain references to cover being changed, so that correct parts of clothing will appear around all the moving parts.
In fact, you might not need separating your person into different body, hands, legs etc MCs, but instead make it so that your person's legs are a container that can hold either a bare body part (naked leg drawing) or a dressed body part (a boot, pants, etc), and animate the main part by making these containers move according to how a person should walk, stand, run, attack etc etc., so that you will have a single point to dress the entire set of your person's animations.
It depends for example my character has around 100 animations. Putting them in a single timeline and telling them to go from this label to that label (or frame) is a huge mess to manage.
So I have 100 library items with export symbols. I keep on switching between them. This makes it easy to manage.
So, I think the question is how many animations do you have? Can you manage them in a single timelines? If you can , I'd say go with labels or frame jumps.
Both of your methods are correct and your second method would be faster because you're not adding or removing anything from the display list.
I have a little logical problem over here.
As the title says, I try to build a boardgame as a computer-program (maybe with internet-support, but thats another story)
As for now, I have a map, which has some crossroads in it, hence I cannot simply define the fields as '1, 2, 3, 4, ...' because if there is a crossroad at field 10, I would have more than one field which has to be labeled 11 (Because then there is a field left and right of field 10, for example.)
So the problem is, if I cannot define the Board in numbers then I cannot simply get the possible positions a player can take when he rolls 2d6-dices with calculating 'Field-Nr. + RandomRange(1,6) + RandomRange(1,6)'
Does anybody have an idea, how to define a Map like this on another way, where I still can calculate the possible new-fields for Player X with a 2d6-dice-roll?
Thanks in advance.
If i understand well... (i don't thing so) this might help you. Just use dynamic arrays for your boardgame field and change your actions after the dimensions x,y .... Look at this "type Name = array of {array of ...} Base type; // Dynamic array"
It sounds like you have a graph of connected vertices. When a player is at a particular vertex of N edges, assuming N < 12, the new Field will be reached from traversing edge number N % ( rand(6) + rand(6) ).
You could also just do rand(12), but that would have an even distribution, unlike 2d6.
Instead of dynamic arrays, I would recommend using a linked-list of records to describe the surrounding cells, and traverse the player's location and possible moves using that linked-list.
First, define a record that describes each cell in your board's playable grid (the cells on the grid can be four-sided like a chessboard, or hexagonal like in Civilization V) ... each cell record should contain info such as coordinates, which players are also in that cell, any rewards/hazards/etc that would affect gameplay, etc. (you get the idea).
Finally, the linked-list joins all of these cells, effectively pointing to any connected cells. That way, all you'd need is the cell location of Player X and calculate possible moves over n amount of cells (determined by the dice roll), traversing the adjoining cells (that don't have hazards, for example).
If all you want is to track the possible roads, you can also use this approach to identify possible paths (instead of cells) Player X can travel on.
ok, here´s a first time noob question, sorry if that´s stupid.
I was just wondering, for a battleship kind of game, would it be a waste of memory to build a set of objects for each cell (10X10=100), with position(x,y) and state(empty,hit,missed) properties?
I was wondering if it would be better to only create an object Grid and use methods to calculate the cell positions whenever necessary (when handling cell selection with touches or drawing for example)..
The former is problematic because you may have ships that sit side-by-side or end to end and it will become difficult to know when one is completely destroyed just from the data structures you described. Two hits side-by-side could be two hits on the same ship, two hits to two different ships, or even a sinking for the smallest ship.
Go with the latter for sanity's sake.
If I was doing this, I would keep it simple, Have a 2 dimensional array, that's your 10 by 10 grid.
When someone takes a turn, calculate the position and;
if it's a miss, insert a 0 in that array cell
if it's a hit, insert a 1 in that array cell