在initWithSize中调用一个不寻常的方法

时间:2014-11-02 18:21:31

标签: objective-c methods sprite-kit

我在Sprite Kit游戏中找到了制作绳索的代码。我将在下面发布。问题是,方法标题非常长&我的initWithSize方法中有一个我无法调用的语法。我试着写[self addRopeJointItems];但无济于事。是。我是Objective-c的新手。希望有人可以提供帮助。

代码:

+(void)addRopeJointItems:(CGPoint)leftStartPosition countJointElements:(int)countJointElements game:(SKScene*)game
{
int itemJointWidth = 25;

//Left Physics Anchor
SKSpriteNode * leftAnchor = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(1, 1)];
leftAnchor.position = CGPointMake(leftStartPosition.x, leftStartPosition.y);
//leftAnchor.size = CGSizeMake(1, 1);
leftAnchor.zPosition = 2;
leftAnchor.physicsBody = [SKPhysicsBody
                          bodyWithRectangleOfSize:
                          leftAnchor.size];
leftAnchor.physicsBody.affectedByGravity = false;
leftAnchor.physicsBody.mass = 99999999999;
[game addChild:leftAnchor];

//add RopeElements
for (int i=0; i<countJointElements; i++)
{
    SKSpriteNode * item = [SKSpriteNode spriteNodeWithImageNamed:@"rope_ring.png"];
    item.name = [NSString stringWithFormat:@"ropeitem_%d", i];
    item.position = CGPointMake(leftStartPosition.x + (i*itemJointWidth) + itemJointWidth/2, leftStartPosition.y+60);
    item.size = CGSizeMake(itemJointWidth + 5, 5);
    item.zPosition = 2;
    item.physicsBody = [SKPhysicsBody
                        bodyWithRectangleOfSize:
                        item.size];
    item.physicsBody.categoryBitMask = kNilOptions;
    [game addChild:item];

    //Add Joint to the element before
    SKPhysicsBody* bodyA;
    if (i == 0)
    {
        bodyA = leftAnchor.physicsBody;
    }
    else
    {
        bodyA = [game childNodeWithName:[NSString stringWithFormat:@"ropeitem_%d", i-1]].physicsBody;
    }

    SKPhysicsJointPin* joint = [SKPhysicsJointPin jointWithBodyA:bodyA bodyB:item.physicsBody anchor:CGPointMake((item.position.x - item.size.width/2) + 5, item.position.y)];
    [game.physicsWorld addJoint:joint];
}

//Right Physics Anchor
SKSpriteNode * rightAnchor = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(1, 1)];
rightAnchor.position = CGPointMake((leftStartPosition.x + (countJointElements*itemJointWidth)),
                                   leftStartPosition.y+60);
//rightAnchor.size = CGSizeMake(1, 1);
rightAnchor.zPosition = 2;
rightAnchor.physicsBody = [SKPhysicsBody
                           bodyWithRectangleOfSize:
                           rightAnchor.size];
rightAnchor.physicsBody.affectedByGravity = false;
rightAnchor.physicsBody.mass = 99999999999;
[game addChild:rightAnchor];

//Add the Last Joint
SKPhysicsJointPin* jointLast = [SKPhysicsJointPin jointWithBodyA:[game childNodeWithName:[NSString stringWithFormat:@"ropeitem_%d", countJointElements - 1]].physicsBody
                                                           bodyB:rightAnchor.physicsBody
                                                          anchor:rightAnchor.position];
[game.physicsWorld addJoint:jointLast];
}

1 个答案:

答案 0 :(得分:0)

你可以在这里做两件事: -

1)使用前缀( - )将方法更改为实例方法。如果您希望使用self

调用实例方法
-(void)addRopeJointItems:(CGPoint)leftStartPosition countJointElements:(int)countJointElements game:(SKScene*)game

2)或者按照@Andrey Chernukha的评论

 [ClassName addRopeJointItems:leftStartPosition countJointElements:elements game:game];