0

我正在尝试使用 Sprite Kit 的联合系统。到目前为止,我什至连一根“绳子”/“绳子”都难以制作。最终目标是生产类似于篮球网的东西,其中将有 1 根左绳(垂直)、1 根右绳(垂直)和至少 2 根连接左右绳(水平)之间的绳 - 总共 4 根绳. 之后,弄清楚如何使 2 条水平绳索在穿过它们时不会与物体(炮弹?)发生碰撞。有谁知道如何做这样的事情?我已经尝试了 2 周,我的眼睛和手指都很痛。

4

1 回答 1

1

您可以将多个物理实体与限制关节连接在一起,以创建一个绳索状结构,该结构会受到与其他实体碰撞的影响。我的代码很乱,但这里是精灵套件项目的场景类。它有一个篮球网,当你点击屏幕时它会产生一个球。

这里有截图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);
        r.constraints=@[[SKConstraint orientToPoint:anchor inNode:self offset:nil]];
    }else {
        r.constraints=@[[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);
rope.constraints=@[[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

于 2014-11-11T19:12:23.947 回答