Reputation: 21
I am trying to move a SKFieldNode (radialGravity) during runtime:
let movingField = SKFieldNode.radialGravityField(
movingField.position = location //location is a touch position
addChild(movingField)
It crashes as soon as I command the field node to change positions. I have no problem placing the field node in a static position at didMoveToView.
I'm guessing SKFieldNodes simply have to stay in place, but maybe theres a solution.
The main idea is to attach the radial fields to sprites (which have physics bodies attached) so that they have attraction/repulsion interactions. I realize this can be done in a more manual approach, but I was trying to take advantage of the physics available in SpriteKit.
Thanks!
Upvotes: 2
Views: 475
Reputation: 748
In my app I have a very similar approach like you. I can set new positions of field nodes just fine. Here is an excerpt:
// _targetPos is the touch location in UIView coordinates
CGPoint pos = [k.world convertToScenePoint:_targetPos]; // convert to scene coordinates
self.gravityField.position = pos;
self.dragField.position = pos;
I can only assume the crash you have mentioned occurs somewhere else in your code. Which line of code does Xcode show when your app crashes?
Upvotes: 0
Reputation: 4334
I can easily move field nodes by making them move to follow touch events. Works fine.
Inside of GameScene.m
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
self.backgroundColor = SKColorWithRGB(0, 0, 0);
// This uses SKTUtils to load my simple SKEmitter
SKEmitterNode *nd = [SKEmitterNode skt_emitterNamed:@"bokeh"];
nd.particleZPosition += 100;
nd.position = CGPointMake(self.size.width/2,self.size.height/2);
nd.fieldBitMask = 1;
field = [SKFieldNode radialGravityField];
field.position = CGPointMake(self.size.width/2,self.size.height/2);;
field.minimumRadius = 50;
field.categoryBitMask = 1;
[self addChild:field];
[self addChild:nd];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint t = [touch locationInNode:self];
field.position = t;
}
Upvotes: 2