0

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

创建 Sprite 节点的代码和我尝试创建一个 shapenode 作为 spritenode 的子节点(这不起作用)。

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


}

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

4

2 回答 2

1

不要将 SKShapeNode 作为 SKSpriteNode 的子节点,而是将其声明为 SKScene 中的兄弟节点。

首先,将 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;

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

于 2013-12-26T07:32:32.163 回答
0

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

    -(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-要么更改 SKSpritenode 通过的每个 CGPOINT 的颜色 2-要么创建在某个方向上放大的 Sprite 节点,直到它碰到某个东西然后改变方向

于 2013-12-29T15:14:24.160 回答