Alb*_*ert 13 camera ios sprite-kit swift
我做了一个平台游戏并将相机指向主节点播放器.
我已经浏览了Apple文档,可以在这里找到:Apple
大约有6个平台,self.frame.size.height / 4.5它们之间有空格,每当它们dy = -15再次位于屏幕顶部时应重新生成,以使其成为无限循环.
我有一个播放器节点,每次触摸屏幕时都会产生一个冲动(播放器上下跳动).整场比赛只会继续前进y-Axis.
我创建的相机专注于播放器,每当玩家跳跃平台向下移动并且玩家向上移动时.然而,这只是观点,而不是现实世界的立场.这意味着,给定的位置SpawnPlatforms总是相同的,因为它们不移动,只有坚持播放器的视图上下移动.
我想在我的播放器节点上安装摄像头,它在y轴上移动,当它进一步向上时,平台也应该向下移动,此时只有视图改变,我无法使用更新:重建平台的方法.我怎么能改变呢?
编辑:我让我的代码使用了更新Update:Method,但这仍然没有回答我的初始问题,如何使用相机移动整个世界,而不仅仅是相机.
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
}
Run Code Online (Sandbox Code Playgroud)
创建一个SKNode,我们称之为地图。将地图添加为该场景的子级。您将所有其他游戏节点添加为该地图的子节点(平台、玩家等)。
然后,您不移动 node.parent,而是移动地图。这将导致即使您移动相机,平台也始终处于相同的位置。
另外,您可以将游戏菜单添加到场景中,它们不会移动,但地图会移动。
例如,下一个代码将以update:线速度移动方法中的地图:
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);
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您想根据玩家内部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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2003 次 |
| 最近记录: |