我在游戏中使用对象并行滚动时出现问题,我的对象是应该重复的行,但是我的代码将该行滚出屏幕,然后它返回到第一个位置并再次滚动:
- (void)bricksEdgeAnimation {
SKSpriteNode *bEdge = [[SKSpriteNode alloc]initWithImageNamed:@"edge.png"];
bEdge.position = self.view.center;
bEdge.name = @"edge";
[self addChild:bEdge];
}
- (void)moveBg
{
[self enumerateChildNodesWithName:@"edge" usingBlock: ^(SKNode *node, BOOL *stop)
{
SKSpriteNode * bg = (SKSpriteNode *) node;
bg.position = CGPointMake(bg.position.x , bg.position.y - SPEED);
if (bg.position.y <= -bg.size.width)
{
bg.position = CGPointMake(bg.position.x ,
bg.position.y + bg.size.width*2);
}
}];
}
-(void)update:(CFTimeInterval)currentTime {
if (_lastUpdateTime)
{
_dt = currentTime - _lastUpdateTime;
}
else
{
_dt = 0;
}
_lastUpdateTime = currentTime;
[self moveBg];
}
它应该是这样的:
如何更改我的代码以重复此行?
答案 0 :(得分:2)
添加第二个“边缘”节点:
- (void)bricksEdgeAnimation {
SKSpriteNode *bEdge = [[SKSpriteNode alloc]initWithImageNamed:@"edge.png"];
bEdge.position = self.view.center;
bEdge.name = @"edge";
[self addChild:bEdge];
//2nd node - note the name is the same
SKSpriteNode *bEdge1 = [[SKSpriteNode alloc]initWithImageNamed:@"edge.png"];
bEdge1.position = CGPointMake(bEdge.position.x,bEdge.position.y+(bEdge.size.height));
bEdge1.name = @"edge";
[self addChild:bEdge1];
}
//note I changed "width" to "height"
- (void)moveBg
{
[self enumerateChildNodesWithName:@"edge" usingBlock: ^(SKNode *node, BOOL *stop)
{
SKSpriteNode * bg = (SKSpriteNode *) node;
bg.position = CGPointMake(bg.position.x , bg.position.y - SPEED);
if (bg.position.y <= -bg.size.width)
{
bg.position = CGPointMake(bg.position.x ,
bg.position.y + bg.size.height*2);
}
}];
}