- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我对 spritekit 坐标系很困惑。我需要澄清两个案例。
案例 1:
当 Sprite 节点作为子节点添加到场景时:
SKSpriteNode * blueBall = [SKSpriteNode spriteNodeWithImageNamed:@"hRedBall.png"];
blueBall.name=@"blueball";
blueBall.size=CGSizeMake(75, 75);
blueBall.position=point; //I am varying this point
[self addChild:blueBall];
我每次都在不同的点添加节点。这些是我添加节点的点:(100x100)(200x200)(300x300)(400x400)(900,600)
在我的 iphone 5 设备中,它们看起来像这样:
如果你查看 100x100,当我在设备上的点 (100,100) 添加 Sprite 时,当我记录节点位置时,它显示 (99.9,138.17) 与点 (100,100) 相比有很大差异。
尽管其他点也有变化,但我忽略了它们,因为对于其他点来说差异太小了。
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
[self bounceTheBlueBallAtPoint:CGPointMake(100, 100)];
}
-(void)bounceTheBlueBallAtPoint:(CGPoint)point{
SKSpriteNode * blueBall = [SKSpriteNode spriteNodeWithImageNamed:@"hRedBall.png"];
blueBall.name=@"blueball";
blueBall.size=CGSizeMake(75, 75);
blueBall.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:blueBall.frame.size.width/2];
blueBall.physicsBody.categoryBitMask=blueBallHitCategory;
blueBall.physicsBody.collisionBitMask=blueBallHitCategory|pinkBallHitCategory|wallHitCategory;
blueBall.physicsBody.contactTestBitMask=blueBallHitCategory|pinkBallHitCategory|wallHitCategory;
blueBall.physicsBody.dynamic=YES;
blueBall.physicsBody.affectedByGravity=NO;
blueBall.physicsBody.restitution=1.0;
blueBall.physicsBody.friction=0.0;
blueBall.physicsBody.linearDamping=0.0;
blueBall.physicsBody.angularDamping=0.0;
blueBall.position=point;
[self addChild:blueBall];
//applying impulse to bounce off
// CGVector impulse = CGVectorMake(30.0,60.0);
// [blueBall.physicsBody applyImpulse:impulse];
//
}
我正在用这种方法检查球的位置:
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
[self checkSpriteMovement];
}
-(void)checkSpriteMovement{
[self enumerateChildNodesWithName:@"blueball" usingBlock:^(SKNode * _Nonnull node, BOOL * _Nonnull stop) {
SKSpriteNode *ball=(SKSpriteNode *)node;
if (ball.physicsBody.resting) {
NSLog(@"The ball position is %@",NSStringFromCGPoint(ball.position));
}
}];
}
案例 2:
假设我有一个背景 Sprite 节点,我将球节点添加到这个背景节点,而不是将球节点添加到场景。
_background = [SKSpriteNode spriteNodeWithImageNamed:@"parkImage.jpg"];
_background.size = self.frame.size; //frame size is (1024x768)
_background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:_background];
然后我将球节点添加到这个背景中:
[_background addChild:blueBall];
这是添加的后台节点:
现在我尝试将球添加到添加的背景中:
在点:(100,100):
点:(-280,-150)
我还想讨论一下第三种情况:
案例三:
我想要另一个节点来调用 ring 的 sprite backgorund。后来我想在背景中添加球。所以会有2个子节点。
_ringNode=[SKSpriteNode spriteNodeWithImageNamed:@"hue_ring.png"];
_ringNode.size=CGSizeMake(80, 80);
_ringNode.position=CGPointMake(0, 0); //center point of background in this case
_ringNode.anchorPoint=CGPointMake(0.5,0.5);
_ringNode.name=kRingNodeName;
[_background addChild:_ringNode];
所以戒指会在这里:
现在我想将球节点添加到背景节点:
我使用相同的代码在 (-280,-150) 点添加球:
为什么场景和 Sprite 节点的坐标系会发生变化?
我面临的另一个问题是:
节点未正确添加到场景中。即使场景已启动,用户也看不到节点,但我可以看到节点数,表明屏幕上有节点。我需要运行代码再次查看正确添加到场景中的节点。这种情况每 4 次发生一次。我只需要停止代码并再次运行它就可以看到屏幕上的球,否则我只会看到没有任何球的背景,尽管我正在将它们添加到背景中。
为什么会这样?
我在 didMoveToView
中添加所有节点
最佳答案
好的,所以很难回答这个问题,因为你没有一个问题,但我会尽力解释为什么事情会这样发生。
首先要考虑的是。你的场景是 1024 x 768(猜测是因为你的评论之一和你的问题)现在这会导致一些问题。首先是你的手机不是那个纵横比,其次你的手机不是那个尺寸。如果您查看 ViewController,您可能会看到设置了一些 AspectFill 或 AspectFit。这会缩小你的场景。当涉及到您在代码中看到的内容和您所做的事情时,定位不再是 1:1。
让我们走得更远。场景的原点位于左下角,根据您的场景大小和缩放比例,它可能会在屏幕外。这是 0,0 所在的位置。因此,您添加的每个 child 都将从那里开始,并根据位置正确向上工作。 SKSpriteNode 的原点在中心。例如,您的背景原点位于中心。请注意如何将位置设置为框架宽度和高度的一半。它的原点在中间,你把它移到右边,从左下角向上移动到正确的距离。现在,当您向该节点添加子节点时,它会从其父节点的原点开始。在这个例子中的中心。所以这就是为什么当你向背景添加一个球时你必须给它负值才能下降到左下角。
至于节点没有出现。如果您查看 ViewController,您可能会看到 ignoresSibling order 或类似性质的东西。这意味着您必须设置节点的 zPosition。较低的 zPositions 首先被渲染,因此背景通常为 0,所有显示在背景之上的节点的 zPosition 为 1 或更高。
希望对您有所帮助。尽管您的问题有详细记录,但很难在一个答案中回答所有问题。将来我会建议您在第一个问题得到解答后将您的问题分解并提出一个新问题,因为您甚至可能在有机会提问之前就知道了答案 =)
关于ios - SpriteKit 中的坐标系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34076632/
我正在尝试构建一个 SpriteKit 游戏,但它因错误而失败:“无法将模块“SpriteKit”加载为“Spritekit”” 我使用的是 Xcode 版本 6.3.2 (6D2105)。 最佳答案
如何在 spritekit swift 中更改碰撞时的 spritekit。 else if collidedBird.birdType == 6 { if U
我刚刚创建了一个新的 SpriteKit 项目,就遇到了砰!问题。我运行该项目时没有触及任何内容,并且应该如此,默认的 hello world 项目出现了。这方面一切都很好。现在,我创建了一个新的 C
当我使用 SpriteKit 创建视频对象时 let background = SKVideoNode(videoFileNamed: "Video.mov") 我只使用了一次命令: backgrou
出于某种原因,我无法将节点放置在屏幕的顶部或底部。我曾经能够使用 self.frame.height/2 很好地做到这一点,但现在我在景观中工作,这行代码不起作用。注意:我的 anchor 是 0.5
在场景中“缩小”的正确方法是什么。 我有一个物体,我施加一个冲动将它发射到屏幕上。例如,它会发射大约 100 像素,这按预期工作 - 增加它飞行的力量,增加它飞行的密度更少等等。 我遇到的问题是缩放,
是否可以更改粒子发射的频率。例如,如果我发射一个粒子,我可以让它每 5 秒或 10 秒发射一次吗? 我搜索了文档,但找不到任何内容。有解决方法吗?我想做这样的事情: emitter.partic
有谁知道一些开箱即用的库,它们可能会为 SKNode 提供诸如相机抖动之类的效果。 ? 如果没有,是否有一种简单的方法可以使用 Action 来实现相机抖动? 谢谢 最佳答案 我找到了一种使用 SKA
我目前一直在使用委托(delegate)和 SKView 自定义类来监视我的 OS X SpriteKit 游戏中的按键。我需要为我的键盘监控类雇用多个代表,我知道这是不可能的,但可以通过使用观察者来
我从事 SpriteKit 游戏已经有一段时间了,但我有一个无法摆脱的恼人问题。我不想过多介绍这款游戏,但它非常简单。我每秒生成一些对象(SKSpriteNodes),这些对象使用 SpriteKit
我正在制作基于SpriteKit的多级游戏。 一切正常,除了一件事:当用户玩很长时间、改变很多关卡等时......然后 SpriteKit 开始丢失纹理。 我的意思是,没有像图像加载失败时那样的大红叉
我正在尝试将 SKSprite Particle Emitter 与 Swift 一起使用。 但我想在我的发射器中使用许多不同的纹理。 是否有可能:拥有许多图像,并且让发射器随机使用图像,而不是仅使用
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
我正在用SpriteKit构建我的第一个项目,但我无法弄清楚这部分。 我的项目包括飞入屏幕的敌方直升机。每个敌人播放大约14秒长的声音文件。这不是循环。它包括敌人进入屏幕之前接近的声音,以及在屏幕上移
我试图在代码中到处搜索:解释文档在进入后台时会做什么,或者甚至有时会暂停,但无济于事-有人可以指导我在进入后台时建议做什么到启用 Sprite 套件的游戏中的背景吗? 我应该调用scene.pause
我是 SpriteKit 新手。我需要通过 UIImageView 或 SpriteNode 显示图像(它是游戏的背景图像)。但是,我需要用户能够放大背景并平移。我在不使用 SpriteKit 的情况
在我的游戏中,我正在创建一些受重力影响的球。当游戏开始时,他们会从屏幕外掉下来,并在底部找到自己的位置。没有其他力施加在他们身上。 问题是我正在用精确的坐标、精确的物理属性创建这些球,但结果不一样。它
我想让我的宇宙飞船有一条跟随它的粒子轨迹。我该怎么做? 这是我的宇宙飞船的代码: override func didMove(to view: SKView) { spaceS.positio
我正在尝试制作一个动画角色,可以在不移动他的躯干的情况下伸手去拿屏幕上的东西。 如果他够不到,他的手应该朝这个方向移动最大量,然后被他的静态躯干的物理限制所抵消。 所以在我的代码中,绿色矩形( han
我的场景上有player对象,并试图对其施加一些力。您可以看到我正在对update:文件上的MyScene.h方法施加作用力。但是,我的player根本没有动。发生了什么? 下面是Player SKS
我是一名优秀的程序员,十分优秀!