iPad スプライト ゲームで 8 つのランダムなスプライト オブジェクトをスポーンする必要があります。オブジェクトはかなり大きく、さまざまなサイズがあります。オーバーレイしないでください。シーン オーバーレイにスポーンされているものは削除されます (オプションで、下にあるものを削除します)。スプライト キットのピクセル単位の完全な衝突検出フレームワークまたはヘルパー クラスを探していました。これまでのところ、チュートリアルなどは見つかりませんでした。オブジェクトが大きいため、ほとんどの人は通常の衝突検出を使用していますが、これは役に立ちません。私は標準的なアプローチをテストしましたが、私の場合はスプライト領域をさらに大きくする長方形を作成します。これは私のスプライト キット テンプレート テスト プロジェクトです。
#import "WBMMyScene.h"
static const uint32_t randomObjectCategory = 0x1 << 0;
@interface WBMMyScene () <SKPhysicsContactDelegate>
@end
@implementation WBMMyScene
-(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, 0);
self.physicsWorld.contactDelegate = self;
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches)
{
CGPoint location = [touch locationInNode:self];
SKSpriteNode *spaceship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
//SKSpriteNode *spaceship = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(50, 50)];
spaceship.position = location;
[spaceship setSize:CGSizeMake(50, 50)];
[spaceship.texture setFilteringMode:SKTextureFilteringNearest];
//spaceship.texture setFilteringMode
spaceship.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:spaceship.size];
spaceship.physicsBody.dynamic = YES;
spaceship.physicsBody.categoryBitMask = randomObjectCategory;
spaceship.physicsBody.contactTestBitMask = randomObjectCategory;
spaceship.physicsBody.collisionBitMask = 0;
[self addChild:spaceship];
}
}
- (void)didBeginContact:(SKPhysicsContact *)contact
{
// 1
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
// 2
if (firstBody.categoryBitMask == secondBody.categoryBitMask)
{
[self projectile:(SKSpriteNode *) firstBody.node didColliteWithEachOther:(SKSpriteNode *) secondBody.node];
}
}
- (void)projectile:(SKSpriteNode *)object1 didColliteWithEachOther:(SKSpriteNode *)object2
{
NSLog(@"Hit");
[object1 removeFromParent];
[object2 removeFromParent];
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}
@end
御時間ありがとうございます :)