I'm working on a game with a primary player as an SKSpriteNode. The game has a concept of powerups that can be acquired, and each player can have a set of attachments.
I have a protocol for attachments defined as:
@protocol ESPlayerAttachment <NSObject>
@required
-(ESPlayerAttachmentType)attachmentType;
@optional
-(void)willAttachToPlayer:(ESPlayerSpriteNode *)player;
-(void)didDetachFromPlayer:(ESPlayerSpriteNode *)player;
-(void)player:(ESPlayerSpriteNode *)player willUseAttachmentInParent:(SKNode *)parent;
@end
Additionally, I have a protocl for powerups defined as:
@protocol ESPlayerPowerup <NSObject>
-(void)acquiredByPlayer:(ESPlayerSpriteNode *)player;
@end
Finally, I have a physics body on the power up box, as well as one on the player so that I can detect the collision and attach the powerup. As an example, I have a shield powerup that is implemented as:
-(void)willAttachToPlayer:(ESPlayerSpriteNode *)player
{
[player detachAttachmentsOfType:ESPlayerAttachmentTypeShield];
__weak ESPlayerShipSpriteNode *weakPlayer = player;
[player.parent addChild:_shield];
_shield.position = CGPointMake(player.position.x, player.position.y + 0.1f * _shield.size.height);
[self performSelector:@selector(detachFromPlayer:) withObject:weakPlayer afterDelay:15.0f];
}
What's bothering me is that if I attach the shield powerup to the player in my SKScene class (in the update
or didMoveToView
methods), everything works. The shield is positioned correctly. However, if I add it through the ESPlayerPowerup
method (which gets called during a collision with the player), the node gets positioned relative to SKScene
, so it ends up in the bottom left corner of the screen. I can't seem to figure out what the difference is (there's only one instance of ESPlayerSpriteNode in the game, so I'm relatively confident that isn't the problem)