2 つのクラスを作成しました。1 つは発射体を処理するクラスで、もう 1 つはメイン シーン自体です。クラスメソッドを呼び出して発射物を生成すると、didMoveToView またはその他のメソッドで動作し、発射物を見ることができます。ただし、接触後に発射物をスポーンさせたい場合は機能しません。以前に連絡先をテストしましたが、すべて正常に動作しています。ここで何が欠けていますか?
didBeginContact メソッド
static inline Enemies *nodeFromBody(SKPhysicsBody *body1, SKPhysicsBody *body2, uint32_t category) {
Enemies *node = nil;
if (body1.categoryBitMask & category) {
node = (Enemies *)body1.node;
}
else if (body2.categoryBitMask & category) {
node = (Enemies *)body2.node;
}
return node;
}
-(void)didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody, *secondBody;
SKSpriteNode *projectile = nil;
SKSpriteNode *offScreen = nil;
Enemies *target = nil;
firstBody = contact.bodyA;
secondBody = contact.bodyB;
projectile = nodeFromBody(firstBody, secondBody, projectileCategory);
offScreen = nodeFromBody(firstBody, secondBody, offScreenCategory);
target = nodeFromBody(firstBody, secondBody, targetsCategory);
if (projectile && target) {
NSLog(@"firstShot");
[projectile removeFromParent];
// Below is the class method being invoked.
[ProjectilePatterns pattern1:self spawnPoint:CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))];
[target removeFromParent];
}
}
クラスメソッド
+(ProjectilePatterns *)pattern1:(SKScene *)scene spawnPoint:(CGPoint)location {
ProjectilePatterns *patternBomb = [ProjectilePatterns spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(20, 20)];
patternBomb.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:patternBomb.size];
patternBomb.physicsBody.categoryBitMask = projectileCategory;
patternBomb.physicsBody.contactTestBitMask = targetsCategory | offScreenCategory;
patternBomb.physicsBody.collisionBitMask = 0;
patternBomb.physicsBody.friction = 0;
patternBomb.physicsBody.linearDamping = 0;
patternBomb.position = CGPointMake(location.x + 20, location.y);
patternBomb.name = @"p1";
NSLog(@"works");
[scene addChild:patternBomb];
[patternBomb.physicsBody applyImpulse:CGVectorMake(3, 0)];
return patternBomb;
}