【问题标题】:Calling an unusual method in initWithSize在 initWithSize 中调用一个不寻常的方法
【发布时间】:2014-11-02 18:21:31
【问题描述】:

我在我的 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];
}

【问题讨论】:

  • 不要用self调用它。这是一个类方法,+方法,而不是-。称之为 [ClassName addRopeJointItems:leftStartPosition countJointElements:elements game:game];

标签: objective-c methods sprite-kit


【解决方案1】:

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

1) 将您的方法更改为带有前缀 (-) 的实例方法。如果你想用self作为实例方法调用

-(void)addRopeJointItems:(CGPoint)leftStartPosition countJointElements:(int)countJointElements game:(SKScene*)game

2)或关注@Andrey Chernukha cmets

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多