0
我一直有xcode的問題。例如,NSLog不打印,case語句不工作等等。我在xcode 6出來之前就創建了這個項目,並且認爲如果我將它切換到在xcode 6中創建的新文件,這些東西將會被修復。到目前爲止,他們已經修復。但是,更多的問題發生了。其中一個主要原因是didBeginContact方法現在沒有被調用。我已經盡力解決它。我更改了類別掩碼,它們的值,collisionBitMask,contactBitMask,放入了不同的基礎,但沒有任何工作。它在xcode 6出來之前生成的文件中工作,但不在xcode 6中。didBeginContact在xcode 6之後沒有被調用
應該在玩家和對手之間檢測到聯繫,但它不再工作。 我把斷點放在didBeginContact方法上,並且在玩家和對手之間有聯繫時它應該調用的方法,但程序沒有退出。
在此先感謝!
GameScene.m:
#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
有沒有人知道它沒有被稱爲? – foodie101