4

SKSpriteNode のサブクラス化中に問題が発生しました。サブクラスの実装は次のとおりです。

@implementation SSSquirrel
- (id) init {
    if (self = [super init]) {

        // Create Squirrel and physics body
        SKSpriteNode *squirrel = [SKSpriteNode spriteNodeWithImageNamed:@"squirrelNormal"];
        squirrel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:squirrel.size.width/2 - 10];
        squirrel.physicsBody.dynamic = NO;
        squirrel.physicsBody.affectedByGravity = NO;
        squirrel.physicsBody.restitution = 0.8;
        squirrel.physicsBody.friction = 0.1;
        squirrel.zPosition = 1.0;

        [self addChild:squirrel];

    }
    return self;
}

@end

次に、メインのゲーム シーンでリスを次のように作成しています。

    SSSquirrel *squirrel1 = [[SSSquirrel alloc] init];
    squirrel1.position = CGPointMake(offset, self.frame.size.height - offset);
    [self addChild:squirrel1];

すべてがうまく作成されます。ただし、ゲームプレイ中は、タッチの開始時にリスの物理ボディを「一時停止」し、タッチの終了時に再度有効にします (tempSquirrelBody はシーンの SKPhysicsBody インスタンス変数です)。基本的に、ユーザーが画面に触れている間、リスがユーザーに向かって移動し、他のオブジェクトの上を移動しているという錯覚を与えようとしています。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */

for (UITouch *touch in touches) {

    tempSquirrelBody = squirrel1.physicsBody;
    squirrel1.physicsBody = nil;

    CGPoint newPosition = squirrel1.position;
    newPosition.x -= 60;
    newPosition.y += 60;

    squirrel1.position = newPosition;
    squirrel1.texture = [SKTexture textureWithImageNamed:@"squirrelUp"];

}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch ends */

for (UITouch *touch in touches) {

    CGPoint newPosition = squirrel1.position;
    newPosition.x += 60;
    newPosition.y -= 60;
    squirrel1.position = newPosition;
    squirrel1.texture = [SKTexture textureWithImageNamed:@"squirrelNormal"];

    squirrel1.physicsBody = tempSquirrelBody;

}

}

残念ながら、これは機能していません。物理ボディが中断されず、物理ボディが nil に設定されているはずのときに、リスが他のオブジェクトと衝突します。SSSquirrel クラスからゲーム シーンにコードを移動すると、動作します。私は間違って何をしていますか?

ありがとう!

4

1 に答える 1

1

全体をに設定するのではなく、 に設定collisionBitMaskするだけの方が簡単ではないでしょうか?0physicsBodynil

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */

for (UITouch *touch in touches) {

    squirrel1.physicsBody.collisionBitMask = 0;

    CGPoint newPosition = squirrel1.position;
    newPosition.x -= 60;
    newPosition.y += 60;

    squirrel1.position = newPosition;
    squirrel1.texture = [SKTexture textureWithImageNamed:@"squirrelUp"];

}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch ends */

for (UITouch *touch in touches) {

    CGPoint newPosition = squirrel1.position;
    newPosition.x += 60;
    newPosition.y -= 60;
    squirrel1.position = newPosition;
    squirrel1.texture = [SKTexture textureWithImageNamed:@"squirrelNormal"];

    squirrel1.physicsBody.collisionBitMask = 0xFFFFFFFF;

}

}
于 2013-12-11T05:22:19.373 に答える