在spriteKit中有粒子发射器跟踪跟随手指路径

时间:2014-03-25 20:47:54

标签: ios ios7 sprite-kit particle-system skemitternode

我在Xcode中创建了一个具有相当长的路径的粒子发射器。当我在粒子生成器内移动它时,它会沿着我的鼠标路径留下一条线索。

关于我的目标的一些背景信息: 在我的spriteKit游戏中,用户将手指拖动到屏幕周围以拍摄移动物体。我正试图创建一个"子弹时间"对象减速时的效果,当当前手指位置接触它们时高亮显示。当手指停止移动或者弹药耗尽时,会触发touchesEnded方法拍摄所有突出显示的对象。目前我有他们旅行的路径显示为使用SKShapeNode&屏幕绘制到屏幕的线条。 CGPath,但我希望用发射器路径突出显示路径。

在触摸开始的方法中,我创建了一个圆形SpriteNode,它可以在手指移动到的任何地方移动(物理碰撞附加到圆而不是路径)。我已将粒子轨迹发射器连接到此圆圈,并且当我在屏幕上移动时,随着圆圈移动,但不会留下痕迹。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];

    pathToDraw = CGPathCreateMutable();
    startPoint = touchLocation;
    CGPathMoveToPoint(pathToDraw, NULL, touchLocation.x, touchLocation.y);

    lineNode = [SKShapeNode node];
    lineNode.path = pathToDraw;
    lineNode.lineWidth = 2;
    lineNode.zPosition = 110;
    lineNode.strokeColor = [SKColor whiteColor];
    [self addChild:lineNode];

    circle = [SKSpriteNode spriteNodeWithTexture:[animationsAtlas textureNamed:@"anim_countdown_9"]];
    circle.name = @"circle";
    circle.scale = 0.3;
    circle.alpha = 0.5;
    circle.zPosition = 110;
    circle.position = touchLocation;

    circle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
    circle.physicsBody.categoryBitMask = weaponCategory;
    circle.physicsBody.dynamic = NO;
    circle.physicsBody.contactTestBitMask = billyCategory;
    circle.physicsBody.collisionBitMask = 0;

    [self addChild:circle];

    pathEmitter = [SKEmitterNode skt_emitterNamed:@"FollowPath"];
    //pathEmitter.position = circle.position;
    //pathEmitter.targetNode = self;
    //pathEmitter.scale = 0.2;
    //pathEmitter.zPosition = 60;
    [circle addChild:pathEmitter];
}

在touchesMoved方法中,我将圆圈相应地移动到新位置

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {

UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInNode:self];

circle.position = currentPoint;

SKAction *wtf = [SKAction followPath:pathToDraw asOffset:NO orientToPath:YES duration:0.1];
//pathEmitter.position = currentPoint;
//[pathEmitter runAction:wtf];
//pathEmitter.particleAction = wtf;
pathEmitter.particleAction = [SKAction moveTo:currentPoint duration:1.0];

CGPathAddLineToPoint(pathToDraw, NULL, currentPoint.x, currentPoint.y);

lineNode.path = pathToDraw;

我已经尝试设置 pathEmitter.targetNode = self; ,就像这篇帖子Making a particle follow a path in spriteKit建议的那样,但是发射器根本没有出现。

如果我将particleAction设置为followPath,它也不会留下痕迹。

在我的代码中你可以看到我已经注释了一些内容,基本上我已经尝试过targetNode和amp;的每个组合。我能想到的粒子动作。

关于如何让发射器在手指路径上留下痕迹的任何建议?

感谢

2 个答案:

答案 0 :(得分:2)

实际上是pathEmitter.targetNode = self;当你的物体移动时,它会让你有一个粒子留下痕迹,我没有看到任何理由为什么它不适合你因为我很久以前一直在使用这种方法,检查你的位置专门为方法touchesMoved

答案 1 :(得分:0)

我认为这段代码就是您所需要的;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];


    circle = [SKSpriteNode spriteNodeWithTexture:[animationsAtlas textureNamed:@"anim_countdown_9"]];
    circle.name = @"circle";
    circle.scale = 0.3;
    circle.alpha = 0.5;
    circle.zPosition = 110;
    circle.position = touchLocation;

    circle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
    circle.physicsBody.categoryBitMask = weaponCategory;
    circle.physicsBody.dynamic = NO;
    circle.physicsBody.contactTestBitMask = billyCategory;
    circle.physicsBody.collisionBitMask = 0;

    [self addChild:circle];

    pathEmitter = [NSKeyedUnarchiver unarchiveObjectWithFile: [[NSBundle mainBundle] pathForResource:@"FollowPath"ofType:@"sks"]];
    pathEmitter.position = CGPointMake(0, -60);
    [circle addChild:pathEmitter];

}


- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {

    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInNode:self];

    circle.position = currentPoint;

}

- (void)update:(CFTimeInterval)delta
{
    if (!pathEmitter.targetNode) {
        pathEmitter.targetNode = self;
    }
}