我在我的遊戲,無需配對SKFieldNode
不能存在的精靈,所以我的解決方案是創建的SKSpriteNode
一個子類,併爲SKFieldNode
一個屬性,但它沒有工作,因爲SKSpriteNode
表現奇怪(我不記得發生了什麼)。所以我的下一個方法是將子類更改爲SKNode
,然後我將SKSpriteNode
和SKFieldNode
作爲這個新的SKNode
的一個屬性。但後來事實證明,touchesMoved
將只移動其中一個屬性(以最高者爲準),其結果始終是SKSpriteNode
。SKNode子類不流動兒童
什麼是解決這個問題的最佳方法,以及如何解決這個問題,這樣我就可以爲每個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;
}
}
}
你可以發佈你的'touchesMoved'嗎? –
@ChrisSlowik用代碼更新了帖子。 –