在SpriteKit中,如何使粒子发射器沿着手指路径移动?

7
我在Xcode中创建了一个粒子发射器,它有一个相当长的拖尾。当我将其移动到粒子生成器内时,它会沿着我的鼠标路径留下一条轨迹。
关于我的目标的一些背景信息: 在我的SpriteKit游戏中,用户在屏幕上拖动手指来射击移动的物体。我试图创建一种“子弹时间”效果,在当前手指位置触摸到它们时,物体会减速并突出显示。当手指停止移动或他们用完弹药时,touchesEnded方法被触发,射出所有突出显示的物体。目前,我使用SKShapeNode和CGPath将它们行进的路径显示为绘制到屏幕上的线条,但我希望使用发射器拖尾来突出显示路径。
在touches began方法中,我创建一个圆形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;

我尝试过像这篇文章Making a particle follow a path in spriteKit建议的那样设置pathEmitter.targetNode = self;,但是发现粒子发射器根本不会出现。

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

在我的代码中,你可以看到我已经注释掉了一些代码,基本上我已经尝试了我能想到的所有targetNode和particleAction的组合。

有什么建议可以让发射器在我的手指路径上留下轨迹吗?

谢谢

2个回答

3

实际上,pathEmitter.targetNode = self; 这句话是让你的粒子随着物体移动留下痕迹的关键。我不明白为什么它在你的项目中无法正常工作,因为我早就使用了这种方法很长一段时间了。请检查你的位置,特别是touchesMoved方法。


谢谢,我一直在努力让我的粒子发射器不再留下尾迹。我没有意识到这就是造成问题的原因。 - AJ B

2
我认为这段代码是你所需要的全部内容;
- (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;
    }
}

我在我的项目中尝试了你的代码,但是它没有起作用 :( 然后我创建了一个只包含你的代码的新项目,它可以正常工作。我的游戏场景中的某些内容使发射器代码失效。我接受了你的答案,因为它有效,现在我只需要弄清楚为什么它在我的游戏中不起作用。 - Ron Myschuk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接