我試圖用Sprite Kit的聯合系統進行實驗。到目前爲止,我很難製作一串「串」/「繩」。最終目標是生產一種像籃球網那樣的左邊繩索(垂直),1條右邊繩索(垂直),至少兩條繩索之間連接左邊的&右邊繩索(水平) - 共4條繩索。之後,弄清楚如何使2條水平繩索在穿過它們時不與物體(炮彈?)碰撞。有誰知道如何做這樣的事情?我一直在嘗試這2周,我的眼睛&手指疼痛。Sprite Kit SKPhysicsJoint
0
A
回答
1
您可以連接幾個物理體與限位接頭一起創建一個像受到與其他物體碰撞影響的結構的繩索。我的代碼非常混亂,但這裏是一個Sprite套件項目的Scene類。它有一個籃球網,當你點擊屏幕時,它會產生一個球。
與截圖這裏https://dl.dropboxusercontent.com/s/flixgbhfk2yysr8/screenshot.jpg?dl=0
編輯:我們對此深感抱歉,代碼現在在OBJ-C
編輯:粘貼這些方法到您的SKScene子類:
-(void)addBasketAtPos:(CGPoint)pos WithSize:(CGSize)size HoopThickness:(CGFloat)hoopThickness RopeThickness:(CGFloat)ropeThickness GapSize:(CGFloat)gapSize {
int nSect=ceil(size.height/(ropeThickness*4));
SKSpriteNode *hoop=[SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(size.width+hoopThickness, hoopThickness)];
hoop.position=pos;
//[self addChild:hoop];
SKNode *lHoopEdge=[SKNode node];
lHoopEdge.position=CGPointMake(pos.x-size.width/2, pos.y);
lHoopEdge.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:hoopThickness/2];
lHoopEdge.physicsBody.dynamic=false;
lHoopEdge.physicsBody.restitution=0.9;
[self addChild:lHoopEdge];
SKNode *rHoopEdge=[SKNode node];
rHoopEdge.position=CGPointMake(pos.x+size.width/2, pos.y);
rHoopEdge.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:hoopThickness/2];
rHoopEdge.physicsBody.dynamic=false;
rHoopEdge.physicsBody.restitution=0.9;
[self addChild:rHoopEdge];
NSMutableArray *rope1=[self makeRopeAtPos:lHoopEdge.position node:lHoopEdge num: nSect gap:size.height/nSect width:ropeThickness];
NSMutableArray *rope2=[self makeRopeAtPos:rHoopEdge.position node:lHoopEdge num: nSect gap:size.height/nSect width:ropeThickness];
CGFloat insetStep=(size.width-gapSize)/2/nSect;
for (int i=0;i<rope1.count;++i) {
SKNode *a=[rope1 objectAtIndex:i];
SKNode *b=[rope2 objectAtIndex:i];
a.position=CGPointMake(a.position.x+i*insetStep, a.position.y);
b.position=CGPointMake(b.position.x-i*insetStep, b.position.y);
}
for (int i=0;i<rope1.count;++i) {
/*
SKNode *n1=[rope1 objectAtIndex:i];
SKNode *n2=[rope2 objectAtIndex:i];
SKPhysicsJointLimit* joint=[self joinBodyA:n1.physicsBody bodyB:n2.physicsBody ropeThickness:ropeThickness];
[self.physicsWorld addJoint:joint];
*/
if (i>0) {
[self.physicsWorld addJoint:[self joinBodyA:((SKNode*)[rope1 objectAtIndex:i]).physicsBody
bodyB:((SKNode*)[rope2 objectAtIndex:i-1]).physicsBody
ropeThickness:ropeThickness]];
[self.physicsWorld addJoint:[self joinBodyA:((SKNode*)[rope2 objectAtIndex:i]).physicsBody
bodyB:((SKNode*)[rope1 objectAtIndex:i-1]).physicsBody
ropeThickness:ropeThickness]];
}
if (i==rope1.count-1) {
[self.physicsWorld addJoint:[self joinBodyA:((SKNode*)[rope1 objectAtIndex:i]).physicsBody
bodyB:((SKNode*)[rope2 objectAtIndex:i]).physicsBody
ropeThickness:ropeThickness]];
}
}
[self addChild:hoop];
}
-(NSMutableArray*)makeRopeAtPos:(CGPoint)p node:(SKNode*)node num:(int)num gap:(CGFloat)gap width:(CGFloat)width {
NSMutableArray *array=[[NSMutableArray alloc] init];
SKNode *last=node;
CGPoint pos=p;
CGPoint anchor=pos;
for (int i=0; i<num; ++i) {
pos=CGPointMake(pos.x, pos.y-gap);
last=[self makeRopeSegAtPos:pos prev:last anchor:anchor first:(i==0) width:width];
anchor=pos;
[array addObject:last];
}
return array;
}
-(SKNode*)makeRopeSegAtPos:(CGPoint)pos prev:(SKNode*)prev anchor:(CGPoint)anchor first:(BOOL)first width:(CGFloat)width {
//SKNode *r=[SKNode node];
SKSpriteNode *r=[SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(anchor.y-pos.y, width)];
r.position=pos;
r.anchorPoint=CGPointMake(0, 0.5);
if (first) {
//r.size=CGSizeMake(10, 20);
[email protected][[SKConstraint orientToPoint:anchor inNode:self offset:nil]];
}else {
[email protected][[SKConstraint orientToNode:prev offset:nil]];
}
r.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:width];
r.physicsBody.allowsRotation=NO;
//r.physicsBody.density=0.2;
r.physicsBody.linearDamping=0.8;
[self addChild:r];
SKPhysicsJointLimit *j=[SKPhysicsJointLimit jointWithBodyA:r.physicsBody bodyB:prev.physicsBody anchorA:r.position anchorB:anchor];
[self.physicsWorld addJoint:j];
return r;
}
-(SKPhysicsJoint*)joinBodyA:(SKPhysicsBody*)bodyA bodyB:(SKPhysicsBody*)bodyB ropeThickness:(CGFloat)ropeThickness {
SKPhysicsJointLimit *j=[SKPhysicsJointLimit jointWithBodyA:bodyA bodyB:bodyB anchorA:bodyA.node.position anchorB:bodyB.node.position];
SKSpriteNode *rope=[SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(j.maxLength, ropeThickness)];
rope.anchorPoint=CGPointMake(0, 0.5);
[email protected][[SKConstraint distance:[SKRange rangeWithUpperLimit:1] toNode:bodyA.node],[SKConstraint orientToNode:bodyB.node offset:nil]];
SKAction *keepLength=[SKAction repeatActionForever:[SKAction sequence:@[[SKAction waitForDuration:1/60],[SKAction runBlock:^{
rope.size=CGSizeMake(sqrtf(powf(bodyA.node.position.x-bodyB.node.position.x, 2)+powf(bodyA.node.position.y-bodyB.node.position.y, 2)), rope.size.height);
}]]]];
[rope runAction:keepLength];
[self addChild:rope];
return j;
}
使用
[self addBasketAtPos: CGPointMake(self.size.width/2, self.size.height*0.7)
WithSize: CGSizeMake(100, 100)
HoopThickness: 10
RopeThickness: 5
GapSize: 80
];
添加一個籃子
在這裏看到新的截圖:https://dl.dropboxusercontent.com/s/xe07ri70rgpxp47/screenshot%202.jpg?dl=0
相關問題
- 1. Sprite Kit Objective C
- 2. Sprite-Kit錯誤
- 3. Collsion Detection Sprite-Kit Bitmasking
- 4. Swift 3.0 - Sprite Kit - Multitouch
- 5. Sprite Kit - 隱藏SKSpriteNode
- 6. Swift Sprite Kit錯誤
- 7. backgorund color flash sprite kit
- 8. iOS7 Sprite Kit如何使用Sprite Kit重置/開始新遊戲?
- 9. Sprite Kit物理上的字旋轉 - 碰撞和重力Sprite-kit
- 10. ios sprite kit bodyWithBody not working
- 11. Sprite Kit生長物理體
- 12. Sprite Kit:防止SKSpriteNode滑過
- 13. Sprite Kit檢測碰撞?
- 14. Sprite Kit碰撞錯誤 - Swift
- 15. Sprite Kit創建一個iAdBanner
- 16. Sprite Kit瓷磚SKTexture與CIFilter?
- 17. Sprite Kit物理:NSArray對象
- 18. Sprite Kit翻轉球員XScale
- 19. Sprite Kit和位圖字體
- 20. Sprite Kit中的混色
- 21. Sprite Kit等價於clipsToBounds:
- 22. 啓用Sprite Kit Multiple Touch
- 23. Sprite Kit SKActions未開火
- 24. Sprite Kit iOS 7.1崩潰wasRemovedFromTargetAtTime
- 25. Sprite-Kit縮放問題UIPinchGestureRecognizer
- 26. 如何將Sprite「釘」到Sprite Kit中的其他Sprite?
- 27. Swift Sprite-Kit - 在角落中的位置
- 28. Sprite Kit失敗斷言:(typeA == b2_dynamicBody || typeB == b2_dynamicBody)
- 29. Sprite Kit調用隨機方法
- 30. 如何取消分配Sprite Kit視圖
SWIFT CODE。甜!我第一次嘗試使用它。我將製作一個Objective-C橋接頭文件,測試代碼,並告訴您這是如何工作的。 – Krekin 2014-11-12 11:18:02
我很難將你的Swift代碼實現到我的Objective-C代碼中。我有一個MyScene:SKScene Objective-C類。我爲我的Net.swift類創建了一個Bridging-Header.h。 Net.swift包含你的上面的代碼。由於我的能力不足,我不知道如何完成這兩項工作。在Objective-C中你不會碰到上述情況? – Krekin 2014-11-12 12:03:58
是啊對不起,我應該注意到,你在obj-c中編寫了你的代碼,將用obj-c代碼編輯(我不明白橋接):) – Okapi 2014-11-12 16:51:18