OStack程序员社区-中国程序员成长平台

标题: ios - 将 Sprite 添加到其他 Sprite 位置 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:59
标题: ios - 将 Sprite 添加到其他 Sprite 位置

我有两个 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)didEndContactSKPhysicsContact *)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;

}
}

必须在 activeFriendlyBall 上方添加英雄,以使他处于中心位置。但它在他附近增加了。我认为是因为添加了物理体。但我需要使用物理 body 进行其他逻辑。

http://cl.ly/image/0g1S0d3h1h0w

必须是,如何在顶部屏幕中。



Best Answer-推荐答案


我找到了解决办法。

self.heroBall.physicsBody.velocity = CGVectorMake(0, 0);
[self.heroBall runAction: [SKAction moveTo:self.activeFriendlyBall.position duration:0.0]];

关于ios - 将 Sprite 添加到其他 Sprite 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32073030/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4