因此,将玩家和敌人从SKShapeNode更改为.png文件后,碰撞效果(爆炸)不再起作用

时间:2014-02-15 15:29:57

标签: objective-c sprite-kit

我将播放器和敌人的自定义圆形形状(SKShapeNode *圆形)更改为.png文件。在将玩家和敌人更改为.png文件之前,碰撞后会出现爆炸效果。但是,在更改为.png文件后,爆炸效果不再发生。是的,图像相互碰撞,这基本上表明存在碰撞。但是相互碰撞后的爆炸效果不再显示。我怎样才能再次实现它,我需要一些帮助。非常感谢你!

#import "GameScene.h"
    #import "CGVector+TC.h"
    #import "ORBMenuScene.h"


enum {
    CollisionPlayer = 1<<1,
    CollisionEnemy = 1<<2,
    };

    @interface GameScene () <SKPhysicsContactDelegate>
    @end




@implementation GameScene
{
    BOOL _dead;
    SKNode *_player;
    NSMutableArray *_enemies;

    SKLabelNode *_scoreLabel;


}


-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];


        self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
        self.physicsWorld.contactDelegate = self;


        _enemies = [NSMutableArray new];


        _player = [SKNode node];
        _player = [SKSpriteNode spriteNodeWithImageNamed:@"Player.png"];
        _player.position = CGPointMake(size.width/2, size.height/2);
        _player.physicsBody.dynamic = YES;
        _player.physicsBody.mass = 0.2;
        _player.physicsBody.categoryBitMask = CollisionPlayer;
        _player.physicsBody.contactTestBitMask = CollisionEnemy;
        _player.physicsBody.collisionBitMask = CollisionEnemy;
        _player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
        _player.position = CGPointMake(size.width/2, size.height/2);
        _player.physicsBody.allowsRotation = YES;
        _player.physicsBody.usesPreciseCollisionDetection = YES;
        _player.name = @"Player";
        [_player setScale:1];

        [self addChild:_player];



    }
    return self;
}

- (void)didMoveToView:(SKView *)view
{
    [self performSelector:@selector(spawnEnemy) withObject:nil afterDelay:1.0];
        }

-(void)spawnEnemy
{
    [self runAction:[SKAction playSoundFileNamed:@"Spawn.wav" waitForCompletion:NO]];

    SKNode *enemy = [SKNode node];
    enemy = [SKSpriteNode spriteNodeWithImageNamed:@"Enemy.png"];
    enemy.physicsBody.categoryBitMask = CollisionEnemy;
    enemy.physicsBody.contactTestBitMask = CollisionPlayer;
    enemy.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:6];
    enemy.physicsBody.collisionBitMask = CollisionPlayer;
    enemy.physicsBody.allowsRotation = YES;
    enemy.physicsBody.dynamic = YES;
    enemy.name = @"Enemy";
    [enemy setScale:0.5];



    CGFloat x = arc4random() % 70 + self.size.width/2;
    CGFloat y = arc4random() % (int) self.size.height;

    if (arc4random() % 2 == 0)
        x = self.size.width/2 + x;
    else
        x = self.size.width/2 - x;

    enemy.position = CGPointMake(x, y);


    [_enemies addObject:enemy];
    [self addChild:enemy];

    if(!_scoreLabel) {
        _scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"Courier-Bold"];

        _scoreLabel.fontSize = 200;
        _scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame),
                                           CGRectGetMidY(self.frame));
        _scoreLabel.fontColor = [SKColor colorWithHue:0 saturation:0 brightness:1 alpha:0.5];
        [self addChild:_scoreLabel];
    }
    _scoreLabel.text = [NSString stringWithFormat:@"%02d", _enemies.count];

    if (_enemies.count == 18) {
        [self runAction:[SKAction sequence:@[
                                             [SKAction waitForDuration:99999],
                                             [SKAction performSelector:@selector(spawnEnemy) onTarget:self],
                                             ]]];
    }
    else {
        [self runAction:[SKAction sequence:@[
                                             [SKAction waitForDuration:1],
                                             [SKAction performSelector:@selector(spawnEnemy) onTarget:self],
                                             ]]];
    }

}

-(void)dieFrom: (SKNode*)killingEnemy
{
    _dead = YES;

    SKEmitterNode *explosion = [SKEmitterNode orb_emitterNamed:@"Explosion"];
    explosion.position = _player.position;
    [self addChild:explosion];

    [explosion runAction:[SKAction sequence:@[
        [SKAction playSoundFileNamed:@"Explosion.wav" waitForCompletion:NO],
        [SKAction waitForDuration:0.4],
        [SKAction runBlock:^{
            // TODO: Revove these more nicely
            [killingEnemy removeFromParent];

            [_player removeFromParent];
        }],
        [SKAction waitForDuration:0.4],
        [SKAction runBlock:^{
            explosion.particleBirthRate = 0;
            }],
            [SKAction waitForDuration: 1.2],

        [SKAction runBlock:^{
            ORBMenuScene *menu = [[ORBMenuScene alloc] initWithSize:self.size];
        [self.view presentScene:menu transition:[SKTransition doorsCloseHorizontalWithDuration:0.4]];
        }],
        ]]];

}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesMoved:touches withEvent:event];

}

-(void)touchesMoved: (NSSet *) touches withEvent:(UIEvent *)event
{
    [_player runAction:[SKAction moveTo:[[touches anyObject] locationInNode:self] duration:.01]];
}

-(void)update:(CFTimeInterval)currentTime
{
    CGPoint playerPos = _player.position;

    for(SKNode *enemyNode in _enemies)
    {
        CGPoint enemyPos = enemyNode.position;

        /* Uniform speed: */
        CGVector diff = TCVectorMinus(playerPos, enemyPos);
        CGVector normalized = TCVectorUnit(diff);
        CGVector force = TCVectorMultiply(normalized, 4);

        [enemyNode.physicsBody applyForce:force];

    }

    _player.physicsBody.velocity = CGVectorMake (0, 0);

    }
-(void)didBeginContact:(SKPhysicsContact *)contact
{
    if(_dead)
            return;

    [self dieFrom:contact.bodyB.node];
    contact.bodyB.node.physicsBody = nil;

    }

@end

1 个答案:

答案 0 :(得分:1)

在我看来,在设置其属性之前,您没有初始化physicsBody

创建玩家或敌人后,您应首先初始化要使用的物体。否则它将为零,表示该节点根本不参与物理模拟。所有的财产设置都没用。

这应该为你做,不要忘记为敌人移动那条线!

    _player = [SKSpriteNode spriteNodeWithImageNamed:@"Player.png"];
    _player.position = CGPointMake(size.width/2, size.height/2);

    _player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];

    _player.physicsBody.dynamic = YES;
    _player.physicsBody.mass = 0.2;