【发布时间】:2014-03-28 06:34:23
【问题描述】:
有人有这方面的经验吗?我一直在使用 SKSpriteNodes,使用 BezierPaths 作为山丘(和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 设置。
【问题讨论】:
-
你能在创建山丘、动画等的地方发布一些代码吗?
-
Okie dokie,添加了它。够了吗?
-
Cocos2d 是制作像 Tiny Wings 这样的游戏的更好选择,至少在视觉上是因为使用自定义 OpenGL 绘制平滑弯曲的山丘看起来更好。这是一个教程:raywenderlich.com/32954/…
标签: ios sprite-kit