我想让正弦波从屏幕上的第一个点到屏幕上的最后一个点,与屏幕的大小无关。
以下是我的代码,但它不能正常工作:
SKSpriteNode* RedBird = (SKSpriteNode*)[self childNodeWithName:@"RedBird"];
CGPoint currentPoint=CGPointMake(-60,0);
double width=self.frame.size.width/4;
CGPoint cp1=CGPointMake(width, self.frame.size.height/2);
CGPoint cp2=CGPointMake((width*3), 0);
CGPoint e=CGPointMake(self.frame.size.width+200, 0);
CGMutablePathRef cgpath = CGPathCreateMutable();
CGPathMoveToPoint(cgpath,NULL, currentPoint.x, currentPoint.y);
CGPathAddCurveToPoint(cgpath, NULL, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);
[RedBird runAction:[SKAction group:@[[SKAction repeatActionForever:RedBirdAnimation],[SKAction followPath:cgpath asOffset:YES orientToPath:NO duration:12.0]]]];
CGPathRelease(cgpath);发布于 2015-05-12 16:26:37
我同意@Gord的观点,因为我认为SKActions是最好的选择。但是,当您可以使用sin函数时,不需要近似正弦曲线。
首先,您需要π,因为它对计算很有用:
// Defined at global scope.
let π = CGFloat(M_PI)其次,扩展SKAction (在Objective-C中,这将使用categories来完成),以轻松地创建一个SKAction,它振荡有问题的节点:
extension SKAction {
static func oscillation(amplitude a: CGFloat, timePeriod t: CGFloat, midPoint: CGPoint) -> SKAction {
let action = SKAction.customActionWithDuration(Double(t)) { node, currentTime in
let displacement = a * sin(2 * π * currentTime / t)
node.position.y = midPoint.y + displacement
}
return action
}
}在上面的代码中:amplitude是振荡的高度;timePeriod是一个完整周期的时间,midPoint是振荡发生的点。displacement的公式来自Simple Harmonic Motion的方程式。
第三,把所有这些放在一起。可以将SKAction.oscillation动作和SKAction.moveByX结合使用,以使精灵沿曲线路径移动。
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
let node = SKSpriteNode(color: UIColor.greenColor(), size: CGSize(width: 50, height: 50))
node.position = CGPoint(x: 25, y: size.height / 2)
self.addChild(node)
let oscillate = SKAction.oscillation(amplitude: 200, timePeriod: 1, midPoint: node.position)
node.runAction(SKAction.repeatActionForever(oscillate))
node.runAction(SKAction.moveByX(size.width, y: 0, duration: 5))
}
}发布于 2015-05-12 14:09:50
这是一个旧的帖子,但我想张贴一个答案,使它可以谷歌。我在SKSpriteNode的一个子类中实现了这个函数,它使精灵从一边到另一边以近似的正弦波移动。
这是快速的,但可以很容易地在objective C中完成。此外,你可能想要更智能,并将路径构建放在一个循环中,但我只是保持它没有循环,以使数学易于理解。
(我并不反对游戏循环的答案,但它有点违背了Sprite Kit的理念)
func setSineAction()
{
let scene = self.parent as! SKScene
let y42 = scene.size.height/2
let x1 = scene.size.width
let x2 = scene.size.width * 0.875
let x3 = scene.size.width * 0.750
let x4 = scene.size.width * 0.625
let x5 = scene.size.width * 0.500
let x6 = scene.size.width * 0.375
let x7 = scene.size.width * 0.250
let x8 = scene.size.width * 0.125
let x9 = 0.0 as CGFloat
let path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, x1, y42)
CGPathAddQuadCurveToPoint(path, nil, (x1/2)+(x2/2), scene.size.height*0.7, x2, y42)
CGPathAddQuadCurveToPoint(path, nil, (x2/2)+(x3/2), scene.size.height*0.3, x3, y42)
CGPathAddQuadCurveToPoint(path, nil, (x3/2)+(x4/2), scene.size.height*0.7, x4, y42)
CGPathAddQuadCurveToPoint(path, nil, (x4/2)+(x5/2), scene.size.height*0.3, x5, y42)
CGPathAddQuadCurveToPoint(path, nil, (x5/2)+(x6/2), scene.size.height*0.7, x6, y42)
CGPathAddQuadCurveToPoint(path, nil, (x6/2)+(x7/2), scene.size.height*0.3, x7, y42)
CGPathAddQuadCurveToPoint(path, nil, (x7/2)+(x8/2), scene.size.height*0.7, x8, y42)
CGPathAddQuadCurveToPoint(path, nil, (x8/2)+(x9/2), scene.size.height*0.3, x9, y42)
self.runAction(SKAction.followPath(path, asOffset: false, orientToPath: false, duration: 10))
}发布于 2014-02-27 19:03:15
我会创建一个基于更新所有游戏元素的游戏循环,而不是依赖于SKActions。SKActions通常是一种执行动画的方法,因为游戏元素的连续移动创建了一个简单的引擎,并让它更新你的红色小鸟的位置。
https://stackoverflow.com/questions/21940495
复制相似问题