-1

I have a custom LineBall class as shown below:

@implementation LineBall

-(instancetype) init {

    self = [super initWithImageNamed:LINE_BALL_IMAGE];

    self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.size.width/2];
    self.physicsBody.categoryBitMask = BBPhysicsCategoryLineBall;

    self.name = @"lineBall";

    self.physicsBody.friction = 0.0f;
    self.physicsBody.restitution = 1.0f;
    self.physicsBody.linearDamping = 0.0f;
    self.physicsBody.allowsRotation = NO;
    self.physicsBody.dynamic = YES;

    return self;
}

Later on I add this to the GameScene and it works as expected. The problem is that now I want to draw lines wherever the LineBall travels. How can I do that?

john doe
  • 9,220
  • 23
  • 91
  • 167
  • Have you considered attaching a particle emitter to your ball? – sangony Dec 24 '14 at 01:22
  • Yes but I don't really want a particle emitter. I actually want to draw a line which other bodies can collide against. – john doe Dec 24 '14 at 03:16
  • You mean something like the Tron light cycle? – sangony Dec 24 '14 at 13:45
  • I don't know what is that? Basically I want to draw a path where ever the sprite moves. – john doe Dec 24 '14 at 16:36
  • Obviously you need a start and end point to draw a line. So in your case the only solution is probably to draw either small squares, rectangles or circles which would make up a line as your ball moves. – sangony Dec 24 '14 at 18:12

1 Answers1

0

Here's one way to draw a trail behind a moving ball.

In the didSimulatePhysics method:

  1. Store the ball's positions over time in a mutable array. Insert at index = 0.
  2. Remove the oldest (last) array element if the number of elements exceeds the max trail size
  3. Create a mutable CGPath by looping over the elements of the array using CGPathAddLineToPoint to connect each point
  4. Remove the old trail from the scene if it exist
  5. Create the ball's trail by creating an SKShapeNode from the path using shapeNodeWithPath
  6. Add the SKShapeNode to the scene
0x141E
  • 12,613
  • 2
  • 41
  • 54
  • 1
    Be careful with this approach. SKShapeNodes are _huge_ resource hogs. A way more elegant approach would be to take a look at SKEmitterNode. – Git.Coach Aug 17 '15 at 05:27
  • @ThomasJohannesmeyer you may be right but that's counter-intuitive given the complexity of a shape node compared to an emitter node. – 0x141E Aug 17 '15 at 05:38
  • But the little bit of extra work is worth it, because SKShapeNode has a huge performance impact. Especially if you use them with customised paths. http://sartak.org/2014/03/skshapenode-you-are-dead-to-me.html http://stackoverflow.com/questions/24553245/poor-performance-with-skshapenode-in-sprite-kit (I did not vote this answer down by the way.) – Git.Coach Aug 17 '15 at 05:42
  • Also: IF you end up using lots of SKShapeNodes (sometimes you just have to), make sure you recycle them using a pool. – Git.Coach Aug 17 '15 at 05:44
  • Like this? http://stackoverflow.com/questions/30514706/create-endless-cgpath-without-framedrops/30717729#30717729 – 0x141E Aug 17 '15 at 05:47
  • You’re welcome. Converting them to SKSpriteNodes is another good way to handle performance. You could also create a local pool where you put your old ShapeNodes and reuse them. That’s what I ended up with. It does not scale as well as your conversion to SpriteNode though. I just use a lot of ShapeNodes, but no more than 100 at a time. – Git.Coach Aug 17 '15 at 05:53