绘制移动 SKSpritenode 的路径



我正在尝试使用新的SKSpritenode,我已经设法创建了一个精灵节点在屏幕上移动它,尽管我希望精灵节点在它移动的地方留下痕迹(颜色)。

创建精灵节点的代码和我尝试为精灵节点创建一个形状节点作为子节点(这不起作用)。

-(void)movetherayoflight{
    ray1=[[lightray1 alloc]initWithColor:[SKColor redColor] size:CGSizeMake(6,6)];
    [ray1 setPosition:CGPointMake(50, 50)];
    ray1.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:ray1.size];
    ray1.physicsBody.restitution=1;
    ray1.physicsBody.linearDamping=0;
    ray1.physicsBody.friction=0;
    ray1.physicsBody.allowsRotation=NO;
    ray1.physicsBody.velocity=CGVectorMake(0.0f, 300.0f);
    [self addChild:ray1];
    SKShapeNode *raayyy=[[SKShapeNode alloc]init];
    CGMutablePathRef rayPath = CGPathCreateMutable();
    CGPoint fff=CGPointMake(ray1.position.x, ray1.position.y);
    CGPoint startpoint=CGPointMake(50, 100);
    //CGPathAddLines(rayPath, NULL, &fff, 2);
    CGPathAddLines(rayPath, NULL, &startpoint, 5);
    //CGPathAddLineToPoint(rayPath, NULL, ray1.position.x, ray1.position.y);
    raayyy.path=rayPath;
    raayyy.lineWidth = 1.0;
    //raayyy.fillColor = [SKColor whiteColor];
    raayyy.strokeColor = [SKColor greenColor];
    raayyy.glowWidth = 0.5;
    [ray1 addChild:raayyy];

}

如果您有更好的解决方案,请告诉我!

与其将 SKShapeNode 作为 SKSpriteNode 的子节点,不如将其声明为 SKSceene 中的同级。

首先,将 SKShapeNode 和 CGPath 声明为实例变量。

在场景的-initWithSize:方法中,

raayyy=[[SKShapeNode alloc]init];
//Additional initialization here.
rayPath = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, ray1.position.x, ray1.position.y);
rayyy.path = rayPath;
[self addChild:rayyy];

然后在您的-update:方法中,

CGPathAddLineToPoint(rayPath, NULL, yar1.position.x, ray1.position.y);
rayyy.path = rayPath;

这只是一个建议,我自己没有尝试过类似的东西。

好吧,我已经用解决方案管理了它,但到目前为止我真的不喜欢它

    -(SKShapeNode*)gravityline{
    SKShapeNode *lolo = [[SKShapeNode alloc] init];
    CGPoint fff=CGPointMake(ray1.position.x, ray1.position.y);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, fff.x, fff.y);
    CGPathAddLineToPoint(path, 0,rayoriginpoint.x,rayoriginpoint.y );
    CGPathCloseSubpath(path);
    lolo.path = path;
    lolo.name=@"gravityline";
    lolo.strokeColor=[SKColor greenColor];
    lolo.glowWidth=.1;
    lolo.physicsBody=[SKPhysicsBody bodyWithPolygonFromPath:path];
    lolo.physicsBody.categoryBitMask=raylightCategory;
    lolo.physicsBody.collisionBitMask=batCategory;
    lolo.physicsBody.contactTestBitMask=batCategory;
    lolo.physicsBody.dynamic=NO;
    CGPathRelease(path);
    return lolo;
}

一旦光线点击中某物,rayoriginpoint就会改变其值

if (firstBody.categoryBitMask == rayCategory && secondBody.categoryBitMask==mirrorCategory )
{
    [self addChild:[self gravityline]];
    CGPoint p = contact.contactPoint;
    rayoriginpoint=p;
    NSLog(@"Contact have been made");
}

我想做的是非常基本的:1-更改SKS精灵节点经过的每个CG点的颜色2-要么创建向某个方向扩展的精灵节点,直到它击中某物然后改变方向

最新更新