从方法Objective-c访问对象的局部变量

时间:2014-09-26 15:47:54

标签: objective-c sprite-kit

我试图从didBeginContact方法访问对象的局部变量。怎么可能?

让我们说我有一个球对象,每当它从玩家反弹时,该球的弹跳变量从玩家那里得到一个标识符。

这是我的initBall方法,它初始化一个球。每隔3-5(随机)秒调用它。

-(void)initBall {
NSString *bouncedFrom;
bouncedFrom = @"";

ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];
ball.name = ballCategoryName;
ball.zPosition = 0;
int cannonPos = (arc4random() % 3 ) + 1;
SKSpriteNode *cannon = (SKSpriteNode *)[self childNodeWithName:[NSString stringWithFormat:@"%i",cannonPos]];
ball.zPosition = 3;
ball.position = cannon.position;
ball.physicsBody.categoryBitMask = ballCategory;
ball.physicsBody.contactTestBitMask = wallCategory;
ball.physicsBody.collisionBitMask = wallCategory;
ball.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:ball];

ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/2];
ball.physicsBody.friction = 0.0f;
//Bounce back
ball.physicsBody.restitution = 1.0f;
//
ball.physicsBody.linearDamping = 0.0f;
ball.physicsBody.allowsRotation = NO;
switch (cannonPos) {
    case 1:
        ball.position = CGPointMake(ball.position.x+20, ball.position.y+20);
        [ball.physicsBody applyImpulse:CGVectorMake(-1.0,1.0)];
        break;
    case 2:
        ball.position = CGPointMake(ball.position.x-20, ball.position.y+15);
        [ball.physicsBody applyImpulse:CGVectorMake(-1.0,1.0)];
        break;
    case 3:
        ball.position = CGPointMake(ball.position.x-20, ball.position.y-15);
        [ball.physicsBody applyImpulse:CGVectorMake(-1.0,-1.0)];
        break;
    case 4:
        ball.position = CGPointMake(ball.position.x+20, ball.position.y-20);
        [ball.physicsBody applyImpulse:CGVectorMake(-1.0,-1.0)];
    default:
        break;
}

我想要的是didBeginContact中的类似内容:

-(void)didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody;
SKPhysicsBody *secondBody;

if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
    firstBody = contact.bodyA;
    secondBody = contact.bodyB;
} else {
    firstBody = contact.bodyB;
    secondBody = contact.bodyA;
}

if ((firstBody.categoryBitMask & ballCategory) !=0) {
    SKNode *ball = contact.bodyB.node;
    SKNode *player = contact.bodyA.node;

    ball.bouncedFrom = PlayerCategory;


}

if ((firstBody.categoryBitMask & wallCategory) !=0) {
    SKNode *ball = contact.bodyB.node;
    SKAction *addScore = [SKAction runBlock:^{
        if (ball.position.y < 0) playerScore--;
        if (ball.position.x < 0) cpu2Score--;
        if (ball.position.y > self.frame.size.height) cpu1Score--;
        if (ball.position.x > self.frame.size.width) cpu3Score--;
        }];
    SKAction *removeNode = [SKAction removeFromParent];
    SKAction *sequence = [SKAction sequence:@[addScore, removeNode]];
    [ball runAction:sequence];
}
}

ball和xCategories都在我的代码顶部声明为静态变量。 这样的事情可能吗?

1 个答案:

答案 0 :(得分:0)

我的第一个想法是你应该将.bouncedFrom声明为.h中的类变量而不是在init:方法中声明它。

所以.h应该看起来像

@interface Ball: NSObject{<br>
    NSString *bouncedFrom;<br>
}

@end