Reputation: 16338
Anyone got Velocity Field in SpriteKit working?
Granted, I'm not sure I understand how it's supposed to work, but I'm wiring it up inside the Xcode UI, not code. My intention is to have dynamic spritenodes be pulled to a direction if they enter the area of that velocity field. Seeing as the velocity field node does not have a size of its own, I just assume I have to add it as a child of another spritenode (calling that areaofvelocityfield) which is not physical, but has a size, so that other dynamic spritenodes can enter into it and get affected by the force. So selecting the Velocity Field node, I select areaofvelocityfield.
Now, whatever I do, any dynamic node is affected by that force, whether or not they are inside areaofvelocityfield or in any vicinity of it. Whatever distance they are from the node containing the velocity field, and the velocity field itself, they are always affected and pulled in the vector of the field.
iOS 9.2.1. Xcode 7.2.
Upvotes: 2
Views: 466
Reputation: 16338
So it seems that .region must be set to the SKFieldNode. This cannot be done from Xcode. At least it cannot be done like this, which is my current code which works well:
SKSpriteNode* belt = (SKSpriteNode*)[self childNodeWithName:@"belt"];
SKFieldNode* myfield = (SKFieldNode*)[self childNodeWithName:@"myfield"];
myfield.region = [[SKRegion alloc] initWithSize:CGSizeMake(belt.size.width * 0.5, 10)];
Note that I am trying to make something like a "conveyor belt". The belt sprite is just a rectangle. The myfield is positioned at the same position as the belt. All nodes are placed directly onto the world, rootnode, no parenting is needed. Placing a field inside another node doesn't really have any effect as to the region it affects (or it might have, not sure here). For some reason I had to set the force field's width to half of the visible belt, or it would keep affecting whatever dynamic node that fell down onto the belt for too long. Other values I used for the field are: strength 1, falloff 10, min. radius 1. Not sure what really made a difference, as this cannot be easily tested in the Xcode interface. A lot of trial and error is needed to figure out how this all works.
Regarding the size of a region: as the docs states, the size protrudes from the origin in all directions which means the width and height will actually be twice what you set.
Upvotes: 1