我有一个带钟摆的工作时钟,但摆臂不能正确缩放。这是缩放之前的样子...
然后,在缩放之后……
钟面按比例缩小,摆臂(绿线)也按比例缩小,但看起来它围绕其中心点而不是钟面中心的支点进行缩放。我可以通过设置秋千的 anchorPoint
来修复这种缩放,使其向一端而不是中心缩放...
swing.anchorPoint = CGPointMake(0, 0.5);
但是这样做会导致钟摆不摆动。 (我认为是因为物理力被施加到摇摆的 anchorPoint
上,如果我将它移动到一端,它位于固定的销 anchor 上。
我怎样才能让 ARM 朝着支点缩放并仍然允许它摆动?
这是代码...
// in my SKScene
ClockFace *face = [ClockFace face]; // just an SKNode subclass
face.name = @"face";
face.position = CGPointMake(150, 150);
[self addChild:face];
// add the pendulum
SKSpriteNode *fulcrum = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(5, 5)];
[face addChild:fulcrum];
fulcrum.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:fulcrum.frame.size];
fulcrum.physicsBody.dynamic = NO;
SKSpriteNode *swing = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(190, 2)];
[face addChild:swing];
swing.position = CGPointMake(95, 0);
swing.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:swing.frame.size];
SKPhysicsJointPin *pinJoint = [SKPhysicsJointPin jointWithBodyA:fulcrum.physicsBody bodyB:swing.physicsBody anchor:face.position];
[self.physicsWorld addJoint:pinJoint];
在 Swift 3 中这个例子:
let nodeSize = CGSize(width: 10, height: 10)
let node = SKSpriteNode(color: .red, size: nodeSize)
node.physicsBody = SKPhysicsBody(rectangleOf: nodeSize)
node.physicsBody?.isDynamic = false
self.addChild(node)
let node2Size = CGSize(width: 60, height: 8)
let node2 = SKSpriteNode(color: .green, size: node2Size)
node2.position = CGPoint(x: 5, y: 0)
node2.anchorPoint = CGPoint(x: 0.0, y: 0.5) // <- New Line
node2.physicsBody = SKPhysicsBody(rectangleOf: node2Size)
node2.physicsBody?.mass = 1.0
self.addChild(node2)
// Scale Line
node2.run(SKAction.repeatForever(SKAction.sequence([
SKAction.scale(to: 0.2, duration: 1.0),
SKAction.scale(to: 1.5, duration: 0.5),
])))
// Anchor Point
let a = SKPhysicsJointPin.joint(withBodyA: node.physicsBody! , bodyB: node2.physicsBody!, anchor: CGPoint(x: 0.0, y: 0.0))
self.physicsWorld.add(a)
关于ios - 缩放用 SKSpriteNodes 制作的钟摆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45259118/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) | Powered by Discuz! X3.4 |