将精灵添加到其他精灵位置
Add sprite to other sprite position
我有两个body
SKShapeNode *hero = [SKShapeNode shapeNodeWithCircleOfRadius:HERO_SIZE];
hero.lineWidth = 1;
hero.fillColor = [SKColor orangeColor];
hero.name = @"Hero";
hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:HERO_SIZE];
hero.physicsBody.categoryBitMask = hero;
hero.physicsBody.collisionBitMask = friendlyCategory | enemyCategory;
hero.physicsBody.contactTestBitMask = friendlyCategory | enemyCategory;
hero.position = [self getStartPosition];
SKShapeNode *circle = [SKShapeNode shapeNodeWithCircleOfRadius:BALL_SIZE];
circle.position = [self randomPossition];
circle.lineWidth = 2;
circle.strokeColor = [SKColor blueColor];
circle.fillColor = color;
circle.name = @"Circle";
circle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:BALL_SIZE];
circle.physicsBody.categoryBitMask = friendlyCategory;
circle.physicsBody.collisionBitMask = hero;
circle.physicsBody.contactTestBitMask = hero;
然后,
- (void)didEndContact:(SKPhysicsContact *)contact
{
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (friendlyCategory | hero ))
{
SKShapeNode* node = (SKShapeNode*)contact.bodyA.node;
if ([node.name isEqualToString:@"Hero"])
{
self.activeFriendlyBall = (SKShapeNode*)contact.bodyB.node;
}
else
{
self.activeFriendlyBall = (SKShapeNode*)contact.bodyA.node;
}
self.hero.position = self.activeFriendlyBall.position;
}
}
Hero 必须添加到 activeFriendlyBall 上面,让他处于中心位置。但它在他附近增加了。我认为这是因为 physic body 添加了。但是我需要使用 physic body 来进行其他逻辑。
http://cl.ly/image/0g1S0d3h1h0w
一定是这样的,怎么上屏。
我相信这是因为您为两者都启用了碰撞检测,这意味着它们不能相互叠加。如果您只是想确定一个精灵是否与另一个精灵接触,您所需要的就是接触检测。从碰撞检测位掩码中删除 hero 或 friendlyball 将允许它们被放置在每个 other/go 之上,通过彼此等
如果您根本不关心其中之一的碰撞(该精灵不会 'bounce' 关闭任何东西)我建议您将该精灵的碰撞检测设置为 0。
最佳
我找到了解决方案。
self.heroBall.physicsBody.velocity = CGVectorMake(0, 0);
[self.heroBall runAction: [SKAction moveTo:self.activeFriendlyBall.position duration:0.0]];
我有两个body
SKShapeNode *hero = [SKShapeNode shapeNodeWithCircleOfRadius:HERO_SIZE];
hero.lineWidth = 1;
hero.fillColor = [SKColor orangeColor];
hero.name = @"Hero";
hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:HERO_SIZE];
hero.physicsBody.categoryBitMask = hero;
hero.physicsBody.collisionBitMask = friendlyCategory | enemyCategory;
hero.physicsBody.contactTestBitMask = friendlyCategory | enemyCategory;
hero.position = [self getStartPosition];
SKShapeNode *circle = [SKShapeNode shapeNodeWithCircleOfRadius:BALL_SIZE];
circle.position = [self randomPossition];
circle.lineWidth = 2;
circle.strokeColor = [SKColor blueColor];
circle.fillColor = color;
circle.name = @"Circle";
circle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:BALL_SIZE];
circle.physicsBody.categoryBitMask = friendlyCategory;
circle.physicsBody.collisionBitMask = hero;
circle.physicsBody.contactTestBitMask = hero;
然后,
- (void)didEndContact:(SKPhysicsContact *)contact
{
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (friendlyCategory | hero ))
{
SKShapeNode* node = (SKShapeNode*)contact.bodyA.node;
if ([node.name isEqualToString:@"Hero"])
{
self.activeFriendlyBall = (SKShapeNode*)contact.bodyB.node;
}
else
{
self.activeFriendlyBall = (SKShapeNode*)contact.bodyA.node;
}
self.hero.position = self.activeFriendlyBall.position;
}
}
Hero 必须添加到 activeFriendlyBall 上面,让他处于中心位置。但它在他附近增加了。我认为这是因为 physic body 添加了。但是我需要使用 physic body 来进行其他逻辑。
http://cl.ly/image/0g1S0d3h1h0w
一定是这样的,怎么上屏。
我相信这是因为您为两者都启用了碰撞检测,这意味着它们不能相互叠加。如果您只是想确定一个精灵是否与另一个精灵接触,您所需要的就是接触检测。从碰撞检测位掩码中删除 hero 或 friendlyball 将允许它们被放置在每个 other/go 之上,通过彼此等
如果您根本不关心其中之一的碰撞(该精灵不会 'bounce' 关闭任何东西)我建议您将该精灵的碰撞检测设置为 0。
最佳
我找到了解决方案。
self.heroBall.physicsBody.velocity = CGVectorMake(0, 0);
[self.heroBall runAction: [SKAction moveTo:self.activeFriendlyBall.position duration:0.0]];