didBeginContact 在 xcode 6 之后未被调用



总的来说,我一直在xcode上遇到问题。例如,NSLog 无法打印、案例陈述不起作用等。我确实在 xcode 6 出现之前创建了这个项目,并认为如果我将其切换到用 xcode 6 制作的新文件,这些事情就会被修复。到目前为止,它们已被修复。但是,发生了更多的问题。一个主要的问题是现在没有调用didBeginContact方法。我已经尝试尽一切努力修复它。我更改了类别掩码,它们的值,碰撞BitMask,contactBitMask,放入不同的基础,但还没有奏效。它适用于 xcode 6 发布之前制作的文件,但不适用于 xcode 6。

玩家和对手之间应该检测到接触,但它不再起作用了。我在 didBeginContact 方法和玩家和对手之间接触时应该调用的方法放置了断点,但程序没有退出。

提前感谢!

游戏场景:

#import "GameScene.h"
@interface GameScene ()

@end

@implementation GameScene
- (void)didMoveToView:(SKView *)view
{
    if (!self.contentCreated)
    {
        [self createSceneContents];
        self.contentCreated = YES;
        [self addplatform];
        [self addPlayer];
        [self addButtons];
        [self addOpponent];
    }
}
typedef NS_OPTIONS(uint32_t, CollisionCategory) {
    CollisionCategoryPlayer     = 1 << 0,
    CollisionCategoryOpponent     = 1 << 1,
    CollisionCategoryPlatform   = 1 << 2,
    CollisionCategoryPlatformBorder = 1 << 3,
};

//static inline CGFloat skRandf() {
//  return rand() / (CGFloat) RAND_MAX;
//}
- (void)createSceneContents
{
    self.backgroundColor = [SKColor purpleColor];
    self.scaleMode = SKSceneScaleModeAspectFit;
    self.multipleTouchEnabled = YES;
}
-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        self.physicsWorld.contactDelegate = self;
    }
    return self;
}
- (SKSpriteNode *)addPlayer {
    self._player = [SKSpriteNode spriteNodeWithTexture: [SKTexture textureWithImageNamed:@"TurnipSmall"]];
    self._player.position = CGPointMake(100,450);
    self._player.anchorPoint = CGPointMake(0.5,0.5);
    self._player.physicsBody = [SKPhysicsBody bodyWithTexture:self._player.texture size:self._player.texture.size];
    self._player.name = @"player";
    self._player.physicsBody.dynamic = YES;
    self._player.physicsBody.allowsRotation = FALSE;
    self._player.physicsBody.affectedByGravity = TRUE;
    self._player.physicsBody.friction = 5;
    self._player.physicsBody.mass = 10;
    self._player.physicsBody.usesPreciseCollisionDetection = YES;
    self._player.physicsBody.restitution = 0.0;
    self._player.physicsBody.categoryBitMask = CollisionCategoryPlayer;
    self._player.physicsBody.collisionBitMask = CollisionCategoryPlayer | CollisionCategoryOpponent | CollisionCategoryPlatform;
    self._player.physicsBody.contactTestBitMask = CollisionCategoryPlayer | CollisionCategoryOpponent | CollisionCategoryPlatform;
    self.playerPunching = false;
    [self addChild:self._player];
    return self._player;
}
- (SKSpriteNode *)addOpponent {
    self._opponent = [SKSpriteNode spriteNodeWithTexture: [SKTexture textureWithImageNamed:@"Tomato"]];
    self._opponent.position = CGPointMake(300, 450);
    self._opponent.anchorPoint = CGPointMake(0.5, 0.5);
    self._opponent.physicsBody = [SKPhysicsBody bodyWithTexture:self._opponent.texture size:self._opponent.texture.size];
    self._opponent.name = @"opponent";
    self._opponent.physicsBody.dynamic = YES;
    self._opponent.physicsBody.allowsRotation = NO;
    self._opponent.physicsBody.affectedByGravity = YES;
    self._opponent.physicsBody.friction = 5;
    self._opponent.physicsBody.mass = 10;
    self._opponent.physicsBody.density = 5;
    self._opponent.physicsBody.usesPreciseCollisionDetection = YES;
    self._opponent.physicsBody.restitution = 0;
    self._opponent.physicsBody.velocity = CGVectorMake(0, 0);
    self._opponent.physicsBody.categoryBitMask = CollisionCategoryOpponent;
    self._opponent.physicsBody.collisionBitMask = CollisionCategoryPlatform| CollisionCategoryPlayer;
    self._opponent.physicsBody.contactTestBitMask = CollisionCategoryPlayer | CollisionCategoryPlatform;
    [self addChild:self._opponent];

    return self._opponent;
}
- (void)didBeginContact:(SKPhysicsContact *)contact
{
    SKPhysicsBody *firstBody, *secondBody;
    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
    {
        firstBody = contact.bodyA;
        secondBody = contact.bodyB;
    }
    else
    {
        firstBody = contact.bodyB;
        secondBody = contact.bodyA;
    }
    if ((firstBody.categoryBitMask & CollisionCategoryPlayer) != 0 && (secondBody.categoryBitMask & CollisionCategoryOpponent))
    {
        [self playerTouchingOpponent];
    }
}

GameScene.h:

//
//  GameScene.h
//  TEST
//
//  Copyright (c) 2014 G Hui. All rights reserved.
//
#import <SpriteKit/SpriteKit.h>
@interface GameScene : SKScene <SKPhysicsContactDelegate>
@property bool multipleTouchEnabled;
@property BOOL contentCreated;
@property SKSpriteNode * _donut;
@property SKSpriteNode * _player;
@property SKSpriteNode * _opponent;
@property SKSpriteNode *platform1Scene1;
@property BOOL movementBegins;
@property NSArray *level1;
@property BOOL playerPunching;
@property bool alreadyPunching;
@property float characterNumber;
@property SKSpriteNode *platform2Scene1;
@property SKSpriteNode *platform3Scene1;
@property float playerHealth;
@property SKSpriteNode *_healthBar;
@property SKSpriteNode *rightplatformBorder;
@property SKSpriteNode *mask;
@property SKNode *_playerHealthBar;
@property SKNode *_opponentHealthBar;
@property SKNode *_playerPowerUpBar;
@property SKNode *_opponentPowerUpBar;
@property int _playerHP;
@property int _opponentHP;
@property const int MaxHP;
@property int _playerPowerUp;
@property int _opponentPowerUp;
@property const int MaxPowerUp;
@property const float healthBarWidth;
@property const float healthBarHeight;
@property const float powerBarWidth;
@property const float powerBarHeight;
@property bool touchingPlatform;
@property SKSpriteNode *sideBorder;
@property SKSpriteNode *frontBorder;
@property BOOL playerOpponentContact;
@property float distanceBetweenPlayerAndOpponent;
@property float distanceBetweenOpponentAndPlayer;
@end

固定!这是因为联系人代表在 initWithSize 中,由于某种原因没有被调用......

相关内容

最新更新