ios – SKNode Subclass Not Moving Children

我有一个精灵在我没有配对SKFieldNode的游戏中不存在,所以我的解决方案是创建一个SKSpieldNode的子类并为SKFieldNode创建一个属性,但它没有用,因为SKSpriteNode表现得很奇怪(我不知道)确切地记得发生了什么).所以我的下一个方法是将子类更改为SKNode,然后我将使SKSpriteNode和SKFieldNode成为这个新SKNode的属性.但后来发现touchesMoved只会移动其中一个属性(以顶部为准),结果总是SKSpriteNode.

解决此问题的最佳方法是什么,以及如何修复它以便我可以为每个SKSpriteNode创建一个SKFieldNode,同时仍然确保操作和方法仍能正常工作.

SKNode子类的当前代码:

@interface Whirlpool : SKNode
- (instancetype)initWithPosition:(CGPoint)pos region:(float)region strength:(float)strength falloff:(float)falloff;
@property (nonatomic, strong) SKFieldNode *gravityField;
@end

#import "Whirlpool.h"
#import "Categories.h"

@implementation Whirlpool
- (instancetype)initWithPosition:(CGPoint)pos region:(float)region strength:(float)strength falloff:(float)falloff {
    if (self = [super init]) {
        // whirlpool sprite
        SKSpriteNode *whirlpoolSprite = [[SKSpriteNode alloc] initWithImageNamed:@"whirlpool"];
        whirlpoolSprite.size = CGSizeMake(100, 100);
        whirlpoolSprite.position = pos;
        //removing physicsBody and associated attributes for now so that the boat does not collide with the whirlpool
        //whirlpoolSprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:whirlpoolSprite.size.width / 2];
        //whirlpoolSprite.physicsBody.dynamic = NO;
        whirlpoolSprite.zPosition = 1;
        whirlpoolSprite.name = @"whirlpool";
        [whirlpoolSprite runAction:[SKAction repeatActionForever:[self sharedRotateAction]]];

        // whirlpool gravity field
        _gravityField = [SKFieldNode radialGravityField];
        _gravityField.position = pos;
        _gravityField.strength = strength;
        _gravityField.falloff = falloff;
        _gravityField.region = [[SKRegion alloc] initWithRadius:region];
        _gravityField.physicsBody.categoryBitMask = gravityFieldCategory;
        _gravityField.zPosition = 1;

        [self addChild:whirlpoolSprite];
        [self addChild:_gravityField];
    }

    return self;
}

- (SKAction *)sharedRotateAction {
    static SKAction *rotateWhirlpool;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        rotateWhirlpool = [SKAction rotateByAngle:-M_PI * 2 duration:4.0];
    });

    return rotateWhirlpool;
}

@end
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // we don't want the player to be able to move the whirlpools after the button is pressed
    if (_isRunning) {
        return;
    }

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        SKNode *node = [self nodeAtPoint:location];
        // if the finger touches the boat or the whirlpool, update its location
        if ([node.name isEqualToString:@"boat"]) {
            node.position = CGPointMake(location.x, node.position.y);
        } else if ([node.name isEqualToString:@"whirlpool"]) {
            node.position = location;
        }
    }
}

最佳答案 我相信你的问题归结为“whirlpool”是你的SKNode子类的孩子.因此,当您确定您确实正在触摸“漩涡”时,您将在其父级(SKNode子类)和SKFieldNode以及父级保持放置中移动它.如果我正确理解了问题,那么对原始代码的这一小调整应该有效.

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // we don't want the player to be able to move the whirlpools after the button is pressed
    if (_isRunning) {
        return;
    }

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        SKNode *node = [self nodeAtPoint:location];
        // if the finger touches the boat or the whirlpool, update its location
        if ([node.name isEqualToString:@"boat"]) {
            node.position = CGPointMake(location.x, node.position.y);
        } else if ([node.name isEqualToString:@"whirlpool"]) {
            //move the SKNode subclass the whirlpool is a child of
            node.parent.position = location;
        }
    }
}

希望这会有所帮助.

点赞