我正在尝试使用新的SKSpritenode,我已经设法创建了一个Spritenode在屏幕上移动它,虽然我希望Spritenode在它移动的地方留下痕迹(颜色)。
创建Sprite节点的代码&我试图为spritenode创建一个shapenode作为孩子(这不起作用)。
-(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];
}
如果你有更好的解决方案,请告诉我!
答案 0 :(得分: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;
这只是一个建议,我自己没有尝试过类似的事情。
答案 1 :(得分: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节点,它在某个方向上向上扩展,直到它碰到某个东西然后改变方向