skyhacker2 / ECParticleBackgroundView

Particles travel in the universe
MIT License
4 stars 0 forks source link

step function occassionally fails #1

Open mbalex99 opened 8 years ago

mbalex99 commented 8 years ago
- (void) step
{
    for (int i = 0; i < _particleNumber; i++) {
        ECParticle* p = _particles[i]; <- sometimes this index is out of range
        if (p.x <= 0 || p.x >= self.bounds.size.width) {
            p.d = CGPointMake(-p.d.x, p.d.y);
        }
        if (p.y <= 0 || p.y >= self.bounds.size.height) {
            p.d = CGPointMake(p.d.x, -p.d.y);
        }
        p.x += p.v * p.d.x;
        p.y += p.v * p.d.y;
    }
    [self setNeedsDisplay];
}
skyhacker2 commented 8 years ago

I think it could be two line to make it out of range.

[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

and

- (void) setParticleNumber:(NSUInteger)particleNumber

I did not consider the multi-threading. I will test I will test this issue. Thank you.