2014-02-21 3 views
9

화면의 첫 번째 포인트에서 화면의 마지막 포인트까지 사인파 모션을 만들고 싶습니다. 여기 SpriteKit의 사인파 모션

내 코드이지만, 제대로 작동하지 않습니다 :

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); 
+0

spritekit 쉐이더에서 이런 종류의 일을 수행 유용한 링크 : http://www.ymc.ch/en/making-a-pixel-shader-for-ios8-with-sprite-kit – MoDJ

답변

0

나는 기반의 게임 루프를 만들 것이라고 업데이트 게임의 모든 요소 대신 SKActions에 의존. SKActions는 일반적으로 애니메이션을 수행하는 방법입니다. 게임 요소를 연속적으로 이동하면 간단한 엔진이 만들어지고 빨간색 새의 위치가 업데이트됩니다.

1

이전 게시물이지만 Google에서 답변 할 수 있도록 답변을 게시하고 싶습니다. 스프라이트가 한면에서 다른면으로 근사 된 사인파로 이동하게하는 SKSpriteNode의 하위 클래스에서이 함수를 만들었습니다.

신속하지만 객관적인 C에서 쉽게 수행 할 수 있습니다. 또한 더 지능적이어야하고 경로를 루프에 넣고 싶지만 수학을 쉽게 이해할 수 있도록 루프를 유지해야합니다. .

(나는 게임 루프 응답에 동의하지 않는 그러나 이러한 종류의 스프라이트 키트의 철학에 반하는) 나는 SKAction s는 가장 좋은 방법 생각에, 나는 @Gord에 동의

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)) 

} 
+0

ABakerSmith의 대답은 좋습니다. 저는 아직 SKOSction을 확장 할 생각이없는 iOS 프로그래밍에 충분히 익숙합니다. – Gord

10

가다. 그러나 sin 기능을 사용할 수있을 때 사인 곡선을 근사 할 필요는 없습니다.

// Defined at global scope. 
let π = CGFloat(M_PI) 

을 두 번째로, 쉽게를 진동 SKAction을 생성 (이이 범주와 함께 할 것입니다 목표 - C에서) 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은 1 사이클 동안의 시간이고, 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)) 
    } 
} 
+0

아주 좋은 대답, 내 하루를 구해줘, 고마워. –