Tag: skspritenode

来自Texture的SKPhysicsBody在Spritekit中给出冲突空值

我正在创build一个angular色跳跃和收集点(一些对象)的游戏。 当你点击字符从阿特拉斯执行一些animation。 当它触及分数时,分数提高1.我使用SKTexture来创buildangular色的物理体。 当我的angular色触及到这个点时,它会崩溃,并向我显示这个错误 fatal error: unexpectedly found nil while unwrapping an Optional value //码 Cat = SKSpriteNode(imageNamed: "cat"); Cat.size = CGSize(width: 100, height: 100) Cat.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2) //physics Cat.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "cat"), size:CGSizeMake(100, 100)) 但是,如果我从矩形或圆形创buildPhysicsbody它工作得很好。 也许是用我用来激活我的angular色的地图集。 Cat.physicsBody = SKPhysicsBody(circleOfRadius: self.frame.width/2) 当angular色碰到蘑菇时,它会崩溃。 工作演示: https : //github.com/nak1b/demo-game-error

在SpriteKit的SKScene和SKSpriteNode中接收触摸事件

用SKSpriteNode的button构build一个UI。 button精灵接收触摸事件来处理特殊效果(弹性缩放效果)但是我需要SKScene同时处理触摸事件来处理UI导航逻辑。 (精灵和场景)如何处理这些触摸事件?

SKAction scaleXTo:-1

我想用SKAction *mirrorDirection = [SKAction scaleXTo:-1 y:1 duration:0.0];镜像我的SKSpriteNode SKAction *mirrorDirection = [SKAction scaleXTo:-1 y:1 duration:0.0]; 但是每次我做的时候,这个精灵的物理体就会变得混乱起来,从屏幕上掉下来。 我在精灵下面有几个物体(楼层),它们落在所有的物体上,好像它们不在那里一样。 这只发生在我镜像精灵的时候。 有人知道怎么修这个东西吗? 更新:作为一个答案build议重置物理机构不幸的是没有解决这个问题。 似乎只有物理机构的接触部分出现故障。 当小家伙像一块石头一样落下时,引力仍然有效果。 我也尝试重新由self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(self.size.width, self.size.height)]; 在self.xScale = -1; 但是这也没有解决这个问题。

Swift平等需要ObjectIdentifier吗?

我们有一个自定义的Swift类的实例,它inheritance自SKSpriteNode,并且能够正确执行下面的代码(对于这个问题非常简单): let instance1 = CustomClass() let instance2 = CustomClass() let instance3 = CustomClass() let instance4 = CustomClass() let array1 = [instance1, instance2] let array2 = [instance3, instance4] func check(testInstance: CustomClass) -> Bool { return array1.filter({ $0 == testInstance }).count > 0 } check(testInstance: instance3) 换句话说,如预期的那样执行check(testInstance: instance3)返回false 。 然而,我们做了一堆的改变,并check停止工作。 CustomClass不实现Equatable协议。 我们只是想检测独特的实例。 它只在我们使用ObjectIdentifier时才开始工作,这意味着函数变成了这个: func check(testInstance: CustomClass) -> […]

如何从SWIFT中的SKSpriteNode创buildCGPath

我有一个SKSpriteNode与电平生成器创build。 我需要使用CGPath创build完全相同的形状。 self.firstSquare = childNodeWithName("square") as! SKSpriteNode var transform = CGAffineTransformMakeRotation(self.firstSquare.zRotation) let rect = CGRect(origin: self.firstSquare.position, size: self.firstSquare.size) let firstSquareCGPath:CGPath=CGPathCreateWithRect(rect, &transform) print(self.firstSquare.position) => firstSquare position(52.8359451293945,-52.9375076293945) 为了检查我的CGPath是否已经创build,我用CGPath创build了一个SKShapeNode: let shape:SKShapeNode=SKShapeNode(path:path) shape.fillColor = self.getRandomColor() addChild(shape) print(shape.position) =>形状位置(52.8359451293945,-52.9375076293945) 结果不是我所期望的。 所以我不知道我的CGPath是否是错误的,或者是当我在SKShapeNode中转换它,我失去了初始的精灵属性。 为了理解为什么我需要这样做,请阅读这个堆栈 编辑1,2 我补充说: shape.position = self.firstSquare.position 我得到了: 编辑3: 我上面更新了我的解释,现在我的firstSquare的定位点(0.5,0.5)

iOS SpriteKit如何使用应用程序捆绑包中的文件夹创build节点?

我试图创build一个随机怪物的精灵,我的图像存储在主包中引用的文件夹中。 NSString* bundlePath = [[NSBundle mainBundle] bundlePath]; NSString* resourceFolderPath = [NSString stringWithFormat:@"%@/monsters", bundlePath]; NSArray* resourceFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:resourceFolderPath error:nil]; NSInteger randomFileIndex = arc4random() % [resourceFiles count]; NSString* randomFile = [resourceFiles objectAtIndex:randomFileIndex]; SKSpriteNode* tile = [SKSpriteNode spriteNodeWithImageNamed:randomFile]; 当我运行我的代码上面,我得到这个错误 SKTexture: Error loading image resource: "random_monster.png" 如果我从主包中引用图像,代码将起作用。 我怎样才能从应用程序包中的文件夹中使用随机图像,并将其传递给SKSpriteNode?

当SKSpriteNode与类别一起使用时,纹理未find,精灵不显示

我开始使用SKSpriteNode类别系统来扩展SKSpriteNode的基本function,并且通过编辑器添加到.sks场景中的精灵现在从场景中丢失(而不是在运行时以编程方式添加到场景中)。 当我在.sks上logging一些精灵时,我可以看到它们的正确位置和缩放参数,但是在XCode的debugging控制台中纹理总是['nil']。 发生这种情况(见下面的NSLog行)当我例如在GameScene1.sks的场景编辑器中的静态SKNoe内的名为“house1”的日志元素。 在场景编辑器上,我可以看到所有的元素(包括house1元素),但是当我启动项目时,没有添加到该.sks文件的精灵。 这是相关的代码: GameScene头文件是: //GameScene.h #import "Hero.h" #import "SKSpriteNode+StaticLevelElement.h" @interface GameScene : SKScene <SKPhysicsContactDelegate> -(void) initStaticLevelElements; @end GameScene的实现文件是: //GameScene.m #import "SKSpriteNode+StaticLevelElement.h" @implementation SKScene (Unarchive) + (instancetype)unarchiveFromFile:(NSString *)file { /* Retrieve scene file path from the application bundle */ NSString *nodePath = [[NSBundle mainBundle] pathForResource:file ofType:@"sks"]; /* Unarchive the file to an SKScene object […]

SKTexture得到图像名称

有没有可能从SKSpriteNode从SKTexture获取当前的图像名称? 我是SpriteKit中的新成员,我在写一个小游戏。 我需要侦测忍者什么时候会击中敌人。 像这样的东西 我正在做SKAction – (void)setUpHit { SKTextureAtlas *hitAtlas = [SKTextureAtlas atlasNamed:@"Ninja_hit"]; SKTexture *hit1 = [hitAtlas textureNamed:@"Ninja_hit_1"]; SKTexture *hit2 = [hitAtlas textureNamed:@"Ninja_hit_2"]; SKTexture *hit3 = [hitAtlas textureNamed:@"Ninja_hit_3"]; SKTexture *hit4 = [hitAtlas textureNamed:@"Ninja_hit_4"]; SKAction *hitAnimation = [SKAction animateWithTextures:@[hit1, hit2, hit3, hit4] timePerFrame:0.1]; SKAction *wait = [SKAction waitForDuration:0.3]; SKAction *goBack = [SKAction animateWithTextures:@[hit1, [SKTexture textureWithImageNamed:@"Ninja"]] timePerFrame:0.1]; […]

在iOS上的SpriteKit的坐标,框架和子节点的混淆?

我还在iOS上学习SpriteKit,并一直在做大量的阅读和大量的实验。 我很困惑的东西我已经find*关于坐标,框架和子节点。 考虑这段代码,其中我正在试图围绕我的飞船精灵绘制一个绿色的框来进行debugging: func addSpaceship() { let spaceship = SKSpriteNode.init(imageNamed: "rocketship.png") spaceship.name = "spaceship" // VERSION 1 spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)) let debugFrame = SKShapeNode(rect: spaceship.frame) debugFrame.strokeColor = SKColor.greenColor() spaceship.addChild(debugFrame) // VERSION 2 // spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)) spaceship.setScale(0.50) self.addChild(spaceship) } 如果我把上面标有“VERSION 1”的行添加到飞船精灵上,我会得到: 这显然是错误的。 但是,如果我注释掉上面标记为“版本1”的行,而是使用标记为“版本2”的行,就可以得到我想要的: 请注意,标记为版本1和版本2的行的实际代码是相同的:spaceship.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame)) 那么为什么当我设定太空船精灵的位置时呢? 按照我的思路,宇宙飞船精灵的位置与debugging帧的位置无关,因为debugging帧是宇宙飞船精灵的一个子,所以它的坐标应该是相对于宇宙飞船精灵的相对位置的。 感谢WM *这与我昨天所问的一个问题有些相关: 在iOS上的SpriteKit中,缩放纹理的sprite会产生不正确的帧? 但是a)我现在明白了一个,b)这是不同的,它应该有自己的问题。 更新: […]

将UIButton的属性赋给SpriteKit中的SKSpriteNode

我想知道是否有一种方法来给一个UIButton的属性,例如一旦它被按下就变暗button,…给SKSpiteNode因为SKSpiteNode有更多的自定义,因为我使用的是SpriteKit 。 我见过其他1个这样的问题,但没有任何答案。 这里是我必须创buildSKSpriteNode和检测触摸它的代码: import SpriteKit class StartScene: SKScene { var startButton = SKSpriteNode() override func didMoveToView(view: SKView) { startButton = SKSpriteNode(imageNamed: "playButton") startButton.size = CGSize(width: 100, height: 100) startButton.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 – 50) self.addChild(startButton) } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { /* Called when a […]