ios – Sprite Kit相机在节点上

ios – Sprite Kit相机在节点上,第1张

概述我做了一个平台游戏,并将相机指向主节点播放器. 我已经通过了可以在这里找到的苹果文档:Apple 大约有6个平台,它们之间有self.frame.size.height / 4.5空格,每次在屏幕顶部的dy = -15时都应该重新生成一个平台,使它成为一个无限循环. 我得到了一个Player节点,每当屏幕被触摸时,它都会得到一个冲动(玩家跳起来).整个游戏只能在y轴上移动. 我创建的摄像机专注于播 我做了一个平台游戏,并将相机指向主节点播放器.

我已经通过了可以在这里找到的苹果文档:Apple

大约有6个平台,它们之间有self.frame.size.height / 4.5空格,每次在屏幕顶部的dy = -15时都应该重新生成一个平台,使它成为一个无限循环.

我得到了一个Player节点,每当屏幕被触摸时,它都会得到一个冲动(玩家跳起来).整个游戏只能在y轴上移动.

我创建的摄像机专注于播放器,每当播放器跳转平台向下移动并且播放器向上移动时.但是,这只是视野,而不是现实世界的立场.这意味着,SpawnPlatforms中给定的位置总是相同的,因为它们不移动,只有坚持使用Player的VIEw上下移动.

我想让我的播放器节点上的相机,它在y轴上移动,而在它进一步向上,平台也应该向下移动,只有视图改变,我不能使用更新:重建平台的方法.我该怎么改呢?

编辑:我的代码使用更新:方法中的更新,但这仍然不能回答我的初始问题,如何移动整个世界与相机,不仅相机.

overrIDe func dIDMovetoVIEw(vIEw: SKVIEw) {    /* Setup your scene here */    self.physicsWorld.contactDelegate = self    self.anchorPoint = CGPoint(x: 0,y: 0.30)    backgroundcolor = SKcolor(red: 81.0/255.0,green: 192.0/255.0,blue: 201.0/255.0,Alpha: 1.0)    self.World = SKNode()    self.World.name = "World"    addChild(World)    self.Camera = SKNode()    self.Camera.name = "Camera"    self.World.addChild(Camera)    SpawnPlatforms()    SpawnPlayer()}func SpawnPlatforms() {    Platform0 = SKSpriteNode (color: SKcolor.greencolor(),size: CGSize(wIDth: self.frame.size.wIDth,height: 25))    Platform0.position = CGPoint(x: self.frame.size.wIDth / 2,y: -36)    Platform0.zposition = 1    Platform0.physicsBody = SKPhysicsBody(rectangleOfSize:Platform0.size)    Platform0.physicsBody?.dynamic = false    Platform0.physicsBody?.allowsRotation = false    Platform0.physicsBody?.restitution = 0    Platform0.physicsBody?.usesPreciseCollisionDetection = true    Platform0.physicsBody?.categoryBitMask = Platform0category    Platform0.physicsBody?.collisionBitMask = Playercategory    Platform0.physicsBody?.contactTestBitMask = Playercategory    World.addChild(Platform0)    ....//Same code for the other Platforms1-5    }    func SpawnPlayer(){    Player = SKSpriteNode (imagenamed: "Image.png")    Player.size = CGSize(wIDth: 64,height: 64)    Player.position = CGPoint(x: self.frame.size.wIDth / 2,y: 0)    Player.zposition = 2    Player.physicsBody = SKPhysicsBody(rectangleOfSize:CGSize(wIDth: 35,height: 50))    Player.physicsBody?.dynamic = true    Player.physicsBody?.allowsRotation = false    Player.physicsBody?.restitution = 0    Player.physicsBody?.usesPreciseCollisionDetection = true    Player.physicsBody?.categoryBitMask = Playercategory    Player.physicsBody?.collisionBitMask = Platform0category    Player.physicsBody?.contactTestBitMask = Platform0category | Platform1category | Platform2category | Platform3category | Platform4category | Platform5category    World.addChild(Player)}overrIDe func dIDSimulatePhysics() {    Camera.position = CGPoint(x: Player.position.x,y: Player.position.y)    self.centerOnNode(Camera!)}func centerOnNode(node: SKNode) {    let camerapositionInScene: CGPoint = Camera.scene!.convertPoint(Camera.position,fromNode: World)    World.runAction(SKAction.moveto(CGPoint(x:World.position.x,y:World.position.y - camerapositionInScene.y),duration: 1.0))}overrIDe func update(currentTime: CFTimeInterval) {    /* Called before each frame is rendered */    if(Player.position.y > (Platform3.position.y + 30) && Player.position.y < Platform4.position.y){        Platform1.position = CGPointMake(self.frame.size.wIDth / 2,Platform6.position.y + self.frame.size.height / 4.5)        Platform1.color = SKcolor.redcolor()    }    ...//Same code for the other Platforms1-5 }
解决方法 创建一个SKNode,让我们称之为map.添加地图作为这个场景的孩子.您添加每个其他游戏节点作为此地图的一个孩子(平台,播放器等).

然后,移动node.parent,而不是移动地图.这将导致平台始终保持相同的位置,即使您移动相机.

此外,您可以添加游戏菜单到场景,他们不会移动,但地图将.

例如,下一个代码将以线性速度移动update:method中的map:

kScrollScenarioFactor可能就像200.f

-(voID)update:(CFTimeInterval)currentTime {  //Calculating time for reaListic scroll speed  float scrollTime = currentTime - self.lastTime;  self.lastTime = currentTime;  self.map.position = CGPointMake(map.position.x - (scrollTime * self.kScrollScenarioFactor),map.position.y);}

或者如果要根据播放器在update:方法中移动地图,可以查看此方法:

-(voID)update:(CFTimeInterval)currentTime {  //Calculating playerposition difference  float diffX = self.lastPlayerposition.x - self.player.position.x;  float diffY = self.lastPlayerposition.y - self.player.position.y;  self.lastPlayerposition = self.player.position;  //Updating map position  self.map.position = CGPointMake(map.position.x + diffX,map.position.y + diffY);}
总结

以上是内存溢出为你收集整理的ios – Sprite Kit相机在节点上全部内容,希望文章能够帮你解决ios – Sprite Kit相机在节点上所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1112321.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存