gpt4 book ai didi

ios - SKNode 子类不移动子类

转载 作者:可可西里 更新时间:2023-11-01 05:51:01 24 4
gpt4 key购买 nike

如果没有配对 SKFieldNode,我的游戏中就没有 Sprite ,所以我的解决方案是创建 SKSpriteNode 的子类,并为 SKFieldNode 但它不起作用,因为 SKSpriteNode 表现得很奇怪(我不记得到底发生了什么)。所以我的下一个方法是将子类更改为 SKNode 然后我将 SKSpriteNodeSKFieldNode 作为这个新 的属性SKNode。但事实证明,touchesMoved 只会移动其中一个属性(以顶部为准),结果始终是 SKSpriteNode

解决这个问题的最佳方法是什么,我该如何解决它,以便我可以为每个 SKSpriteNode 拥有一个 SKFieldNode,同时仍然确保 Action 和方法仍然存在正常工作。

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;
}
}
}

希望对您有所帮助。

关于ios - SKNode 子类不移动子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35635185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com