Ho un metodo di supporto che si estende SKPhysicsContact
didBeginContact passato PKPhyicsObject
extension SKPhysicsContact {
/// - returns: `[SKPhysicsBody]` containing all the bodies that match `mask`
func bodiesMatchingCategory(mask: UInt32) -> [SKPhysicsBody] {
let bodies = [bodyA, bodyB]
return bodies.filter { ($0.categoryBitMask & mask) != 0 }
}
}
in didBeginContact()
Io chiamo questo metodo sul passato in contact
.
func didBeginContact(contact: SKPhysicsContact) {
let ballMask: UInt32 = 0x1 << 2
let ball = contact.bodiesMatchingCategory(ballMask)
...
ricevo questo messaggio di errore a volte (come 1 a 5), che blocca l'applicazione:
-[PKPhysicsContact bodiesMatchingCategory:]: unrecognized selector sent to instance 0x165f2350
Alzai gli occhi PKPhysicsContact
ed è parte di un quadro privato (link). SKPhysicsContact
sembra che sia solo una definizione di classe vuota che espone solo determinate proprietà di PKPhysicsContact
.
Mi sembra che questo è un hack Objective-C sul team SpriteKit che interrompe la digitazione forte di Swift.
Aiuto?
Come assicurarsi di ottenere sempre SKPhysicsContact
indietro?
ho aggiunto un controllo per verificare SKPhysicsContact
let test = contact as Any
print("Test is: \(test)")
guard test is SKPhysicsContact else {
return
}
che cattura correttamente il tipo di mis-match.
Infatti, MAI restituisce un SKPhysicsContact
!!?
Ho provato a farlo in Objective-C (come suggerito dal risponditore) e sto ottenendo lo stesso risultato.
Ho una discussione sullo Apple Dev Forums che potrebbe fornire un aiuto in futuro ai cercatori di risposte.
Ecco il codice Objective-C per riferimento:
@interface SKPhysicsContact (MatchingBodies)
- (NSArray *)bodiesMatchingCategory:(UInt32)category;
@end
@implementation SKPhysicsContact (MatchingBodies)
- (NSArray *)bodiesMatchingCategory:(UInt32)category {
NSArray *bodies = @[self.bodyA, self.bodyB];
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(SKPhysicsBody *body, NSDictionary *bindings) {
return (body.categoryBitMask & category) != 0;
}];
NSArray *matching = [bodies filteredArrayUsingPredicate:predicate];
return matching;
}
@end
dice qui,
-(void)didBeginContact:(SKPhysicsContact *)contact
{
static const uint32_t MarbleContact = 0x1 <<1; // 2
static const uint32_t GoalContact = 0x1 <<2; // 4
SKPhysicsBody *ball = [contact bodiesMatchingCategory:MarbleContact].firstObject;
NSLog(@"Ball: %@", ball);
...
restituisce questo incidente:
-[PKPhysicsContact bodiesMatchingCategory:]: unrecognized selector sent to instance 0x17dad9e0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PKPhysicsContact bodiesMatchingCategory:]: unrecognized selector sent to instance 0x17dad9e0'
Aggiunto Bug Report per Apple, # 23332190
Dove è definito 'bodiesMatchingCategory'? È in un'estensione di 'SKPhysicsContact'? –
@BenKane sì, è in cima alla mia domanda –
Oh, wow. Non so come mi sia perso quello :) Comunque, vedi la mia risposta. Sono andato avanti con l'ipotesi che fosse così. –