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



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

关于我的目标的一些背景信息:在我的spriteKit游戏中,用户在屏幕上拖动手指来射击移动的物体。我正在尝试创建一个"子弹时间"效果,当当前手指位置触摸物体时,物体会变慢并高亮。当手指停止移动或耗尽弹药时,touchesEnded方法被触发射击所有高亮显示的对象。目前,我有他们旅行的路径显示为一条线绘制到屏幕使用SKShapeNode &CGPath,但是我希望用发射器的轨迹来突出显示。

在touchesbegan方法中,我创建了一个圆圈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。

在spriteKit中让粒子跟随路径,但这样发射器根本不会出现。

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

在我的代码中,你可以看到我已经注释掉了一些行,基本上我已经尝试了targetNode &

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

谢谢

实际上是pathEmitter。targetNode = self;是让粒子在物体移动时留下痕迹的方法,我不知道为什么它不适合你因为我已经用了很长时间了现在,检查你的位置特别是touchesMoved

方法

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

- (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;
    }
}

最新更新