有没有人有这方面的经验?我一直使用BezierPaths的SKSpriteNodes作为山丘的半圆(和physicsBody),然后将它们从屏幕上加载,并将它们每帧移动1-10像素更新到左边。
它很迟钝,相机跟不上鸟儿,鸟儿从来没有足够的速度自己上山......
我尝试使用SKActions将山丘移动到屏幕的左侧,但是他们并没有总是排列到确切的像素,而鸟有时会落在它们之间。
有比这更好的方法吗?有4个半圈SKSpriteNodes,以固定的速率向左移动?我希望我可以让这只鸟停留在屏幕上固定的x位置,并且只在适当的时间移动山丘。
这是我创建山丘的代码
- (void) createNewHill {
// Hill 1
SKSpriteNode *hill = [[SKSpriteNode alloc] initWithColor:[UIColor clearColor] size:CGSizeMake([UIScreen mainScreen].bounds.size.width * 2, [UIScreen mainScreen].bounds.size.height * 0.5)];
SKSpriteNode *lastHill = [hillArray objectAtIndex:(hillArray.count - 1)];
[hill setPosition:CGPointMake(lastHill.frame.origin.x + lastHill.frame.size.width, hill.frame.size.height * 0.5)];
float x1 = -hill.frame.size.width / 2;
float x3 = -hill.frame.size.width / 4;
float x7 = hill.frame.size.width / 4;
float x9 = hill.frame.size.width / 2;
float y1 = - hill.frame.size.height / 2;
float y5 = hill.frame.size.height / 2;
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(x1, 0)];
[bezierPath addQuadCurveToPoint:CGPointMake(0, 0) controlPoint:CGPointMake(x3, y1 * 2)];
[bezierPath addQuadCurveToPoint:CGPointMake(x9, 0) controlPoint:CGPointMake(x7, y5 * 2)];
[bezierPath addLineToPoint:CGPointMake(x9, y1)];
[bezierPath addLineToPoint:CGPointMake(x1, y1)];
[bezierPath closePath];
[hill setPhysicsBody:[SKPhysicsBody bodyWithEdgeLoopFromPath:bezierPath.CGPath]];
hill.physicsBody.dynamic = NO;
hill.physicsBody.friction = 0;
hill.physicsBody.restitution = 0;
// Hill 2
SKSpriteNode *hill2 = [[SKSpriteNode alloc] initWithColor:[UIColor clearColor] size:CGSizeMake([UIScreen mainScreen].bounds.size.width * 2, [UIScreen mainScreen].bounds.size.height * 0.5)];
[hill2 setPosition:CGPointMake(hill.position.x + hill2.frame.size.width, hill.position.y)];
[hill2 setPhysicsBody:[SKPhysicsBody bodyWithEdgeLoopFromPath:bezierPath.CGPath]];
hill2.physicsBody.dynamic = NO;
hill2.physicsBody.friction = 0;
hill2.physicsBody.restitution = 0;
[scene addChild:hill];
[scene addChild:hill2];
[hillArray addObject:hill];
[hillArray addObject:hill2]; }
以下是我的代码中的一部分是通过计时器运行的(每1.0 / 60.0秒重复一次)
SKSpriteNode *firstHill = [hillArray objectAtIndex:0];
SKSpriteNode *secondHill = [hillArray objectAtIndex:1];
if (speedOfHills < 60 * 10)
speedOfHills++;
[firstHill setPosition:CGPointMake(firstHill.position.x - [self getDoubleBasedOnDevice:speedOfHills / 60 :speedOfHills / 60 :speedOfHills / 30], firstHill.position.y)];
for (int i = 1; i < hillArray.count; i++)
{
SKSpriteNode *thisHill = [hillArray objectAtIndex:i];
SKSpriteNode *previousHill = [hillArray objectAtIndex:i - 1];
[thisHill setPosition:CGPointMake(previousHill.position.x + (previousHill.frame.size.width / 2) + (thisHill.frame.size.width / 2), firstHill.position.y)];
}
if (firstHill.position.x < - ((firstHill.size.width / 2) + (secondHill.frame.size.width)) * 1.05)
{
[self createNewHill];
[firstHill removeFromParent];
[secondHill removeFromParent];
[hillArray removeObjectAtIndex:1];
[hillArray removeObjectAtIndex:0];
}
到目前为止,我已尝试了一些操作,但这只是我当前的timerUpdate设置。
答案 0 :(得分:1)
如果您使用计时器更新这些位置,那么您正在执行精灵工具包错误。阅读有关更新循环的信息
您应该使用SKScene
- (void)update:(NSTimeInterval)currentTime
方法移动您的山丘和事物。您将当前时间作为方法的一部分。如果你想自己移动东西,以便它们与像素对齐,你需要在这个方法中移动东西。您可以使用经典公式speed = distance / time来计算移动山丘的距离。如果你知道速度,那么距离=速度*时间。经过的时间是此更新时间与最后一次更新时间之间的差异。
我建议查找并运行一些精灵工具包教程