- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
如果没有配对 SKFieldNode
,我的游戏中就没有 Sprite ,所以我的解决方案是创建 SKSpriteNode
的子类,并为 SKFieldNode
但它不起作用,因为 SKSpriteNode
表现得很奇怪(我不记得到底发生了什么)。所以我的下一个方法是将子类更改为 SKNode
然后我将 SKSpriteNode
和 SKFieldNode
作为这个新 的属性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/
我发现如果你想在 Sprite Kit 中使用物理,你必须在世界坐标中指定 anchor 。假设您有一个大节点树,这里有一辆汽车,那里有一个轮子。您只知道它们在父节点中的坐标。您想要在汽车和车轮之间定
我正在为场景创建一个 SKNode,并在每个单独的 SKNode 上记录触摸事件。我可以根据需要添加任意数量的内容,如果我触摸节点并且只有可见的节点,我才能看到日志消息,并且触摸会按预期工作。现在,如
基本上我想实现这样的目标:https://github.com/moqod/iOS-Scratch-n-See在 SpriteKit 中。 在 SKScene 中,我有一张汽车图像,2-3 种不同类型
我从一个函数返回一个 SKNode,我需要将它转换为自定义 SKNode。我收到错误消息无法将 SKNode的值分配给类型GroundNode。如果我强制转换,它会编译,但在运行时失败。我错过了什么?
我正在尝试使用 SpriteKit 模拟一只眼睛。 当用户的手指在屏幕上移动时,眼睛的瞳孔会跟踪用户的手指,但必须保持在眼睛的范围内。 我曾尝试使用 SKConstraint 解决此问题,但未成功。
我不明白为什么我在最后一行收到这个错误:'SKNode!'不是'SKNode'的子类型 func move_background(){ let enumerationBlock: (SKNod
我有一个场景,其中有许多子节点,它们是 SKNode 的子类。我正在尝试访问每个子节点的方法,但收到错误“‘SKNode’类型的值没有成员‘[成员名称]”。 全部基于 Xcode 10.2 上的 Sw
我正在使用 Sprite Kit 创建一个 iOS 应用程序,并且我正在尝试选择 (onTouchBegan) 位于另一个 SKNode 下方的 SKNode。结构是:第一层(SKNode)-第二层(
我在带有 SKScene 的 UIView 上有一个 UITapGestureRecognizer 和一个 UIPanGestureRecognizer。平移手势识别器从左到右移动一个 SKNode,
当事件发生时,SKNode 子类应该如何通知父场景? 我有一个 SKNode 子类,它根据游戏逻辑通过适本地重绘或删除节点来处理触摸事件。通知父场景(或其他同级 SKNode 实例)事件发生的 iOS
我有正确的字符串格式的 SKNODE。我想将字符串名称“A2Square”转换为名为 A2Square 的节点。 这可能吗? 是否有办法将字符串“A2Square”并以某种方式将其转换为同名的 SKN
我已经设置了场景,一切都运行良好。它看起来像这样: 但是 Controller 是在 Main.storyboard 中设置的,我想摆脱它。所以我做了这样的事情: window = UIWin
我知道为什么会出现该错误,但我无法找到解决方法。我试图让对象出现然后被删除,玩家应该在它们被删除之前尝试点击它们,但每次下一个节点即将出现时它都会崩溃。如果我在它的 func 中声明它,那么一切都会出
我的场景是,我正在沿着拱形移动现有节点,并希望它在到达另一侧时停止。一个简单的比喻是,我想让一些东西沿着彩虹移动。 虽然我当前的代码确实执行了正确的椭圆形,但它随后在我的 y 原点下方继续返回到其起点
我正在获取 int SpriteKit。并且想知道如何在 SKNode 对象上创建运动效果。 对于 UIView,我使用以下方法: +(void)registerEffectForView:(UIVi
我在不同的文件中有两个类。 在我的场景所在的“主”文件中,我调用此函数: override func touchesBegan(touches: Set, withEvent event: UIEve
好的,所以我有代码会在 1 和 2 之间选择一个随机数,并基于该数生成一个 metalCrate: 或只是一个 crate :。我想,金属 crate 应该需要轻按 2 次才能消失。不幸的是,使用我的
我目前正在同时使用 ARKit 和 CoreLocation 将元素放置在屏幕上。这些元素(例如建筑物、房屋等)基于它们自己的坐标(经度/纬度)和用户坐标放置。如您所知,即使设置了最佳精度,CoreL
我创建了一个运行良好的 SKNode 类。但是,我无法在实例化后更改其 .position。 按钮确实出现并正常工作,但在编程时不会改变位置。我已经包含了为下面的按钮制作的类,我还添加了实例化该类并调
出于学习目的,我正在使用 SpriteKit 和 Swift 5 编写一个类似 Pong 的游戏。我试图让玩家的 Racket (一个 SKNode 矩形)在 x 坐标的屏幕上移动但不能。 我试过这个
我是一名优秀的程序员,十分优秀!