在本文中,解释了问题:移动平台地狱
http://www.learn-cocos2d.com/2013/08/physics-engine-platformer-terrible-idea/
在评论中有Box2D的解决方案:运动体
但是SpriteKit怎么样?
更新
我正在使用平台
let moveHPart1 = SKAction.moveByX(origW,y: 0,duration: moveDuration);let moveHPart2 = SKAction.moveByX(-origW,duration: moveDuration);platform(SKAction.repeatActionForever(SKAction.sequence([moveHPart1,moveHPart2])));解决方法 我个人反对使用物理学来移动平台,因为移动平台物理主体必须是动态的.
静态平台
对于静态平台,将物理体动态属性设置为false是完美的解决方案.这就是它的意义.静态物体不受力的影响,但仍然会给你一个碰撞响应.所以,问题就解决了.
但是你无法通过使用力来改变静态物理体的位置.您可以使用 *** 作或手动设置其位置来执行此 *** 作.但是,那么你正在从物理模拟中移除一个平台.
为了完成所有物理学,你必须保持平台的动态.但这可能导致其他问题.例如,当玩家登陆平台时,他会将平台向下推,因为玩家有一个质量.
即使平台质量很大,它也会随着时间的流逝而下降.请记住,我们不能手动更新平台x位置,因为这可能会使物理模拟变得混乱.
“移动平台地狱”中所说的那个很好的article的LearnCocos2d可能是使用物理学完成这项任务时可能发生的最佳描述:-)
移动平台示例
为了向您展示一些可能性,我做了一个简单的例子,说明如何通过对其施加力来移动平台,并使角色留在它上面.为了使其工作,我做了很少的事情:
>改变了大量的平台.当玩家从下面碰到平台时,这将阻止平台移动.
>制作一个基于边缘的物理机构,以防止玩家落地时平台掉落.
>使用诸如允许旋转和摩擦等属性来获得所需效果.
这是代码:
import SpriteKitclass GameScene: SKScene,SKPhysicsContactDelegate{ let Bodycategory : UInt32 = 0x1 << 1 let Platformcategory : UInt32 = 0x1 << 2 let Wallcategory : UInt32 = 0x1 << 3 let Edgecategory : UInt32 = 0x1 << 4 // This will prevent a platforom from falling down let Playercategory : UInt32 = 0x1 << 5 let platformSpeed: CGfloat = 40.0 let body = SKShapeNode(circleOfRadius: 20.0) let player = SKShapeNode(circleOfRadius: 20.0) let platform = SKSpriteNode(color: SKcolor.greencolor(),size: CGSize(wIDth:100,height:20)) let notDynamicPlatform = SKSpriteNode(color: SKcolor.greencolor(),height:20)) overrIDe func dIDMovetoVIEw(vIEw: SKVIEw) { //Setup contact delegate so we can use dIDBeginContact and dIDEndContact methods physicsWorld.contactDelegate = self //Setup borders self.physicsBody = SKPhysicsBody(edgeLoopFromrect: self.frame) self.physicsBody?.categoryBitMask = Wallcategory self.physicsBody?.collisionBitMask = Bodycategory | Playercategory //Setup some physics body object body.position = CGPoint(x: CGRectGetMIDX(self.frame),y: CGRectGetMIDY(self.frame)) body.fillcolor = SKcolor.greencolor() body.physicsBody = SKPhysicsBody(circleOfRadius: 20) body.physicsBody?.categoryBitMask = Bodycategory body.physicsBody?.contactTestBitMask = Platformcategory body.physicsBody?.collisionBitMask = Platformcategory | Wallcategory body.physicsBody?.allowsRotation = false body.physicsBody?.dynamic = true self.addChild(body) //Setup player player.position = CGPoint(x: CGRectGetMIDX(self.frame),y:30) player.fillcolor = SKcolor.greencolor() player.physicsBody = SKPhysicsBody(circleOfRadius: 20) player.physicsBody?.categoryBitMask = Playercategory player.physicsBody?.contactTestBitMask = Platformcategory player.physicsBody?.collisionBitMask = Platformcategory | Wallcategory | Bodycategory player.physicsBody?.allowsRotation = false player.physicsBody?.friction = 1 player.physicsBody?.dynamic = true self.addChild(player) //Setup platform platform.position = CGPoint(x: CGRectGetMIDX(self.frame),y: CGRectGetMIDY(self.frame) - 100) platform.physicsBody = SKPhysicsBody(rectangleOfSize: platform.size) platform.physicsBody?.categoryBitMask = Platformcategory platform.physicsBody?.contactTestBitMask = Bodycategory platform.physicsBody?.collisionBitMask = Bodycategory | Edgecategory | Playercategory platform.physicsBody?.allowsRotation = false platform.physicsBody?.affectedByG@R_502_6610@ty = false platform.physicsBody?.dynamic = true platform.physicsBody?.friction = 1.0 platform.physicsBody?.restitution = 0.0 platform.physicsBody?.mass = 20 //Setup edge let edge = SKNode() edge.physicsBody = SKPhysicsBody(edgeFromPoint: CGPoint(x: 0,y:-platform.size.height/2),topoint: CGPoint(x: self.frame.size.wIDth,y:-platform.size.height/2)) edge.position = CGPoint(x:0,y: CGRectGetMIDY(self.frame) - 100) edge.physicsBody?.categoryBitMask = Edgecategory edge.physicsBody?.collisionBitMask = Platformcategory self.addChild(edge) self.addChild(platform) } overrIDe func update(currentTime: NSTimeInterval) { if(platform.position.x <= platform.size.wIDth/2.0 + 20.0 && platform.physicsBody?.veLocity.dx < 0.0 ){ platform.physicsBody?.veLocity = CGVectorMake(platformSpeed,0.0) }else if((platform.position.x >= self.frame.size.wIDth - platform.size.wIDth/2.0 - 20.0) && platform.physicsBody?.veLocity.dx >= 0.0){ platform.physicsBody?.veLocity = CGVectorMake(-platformSpeed,0.0) }else if(platform.physicsBody?.veLocity.dx > 0.0){ platform.physicsBody?.veLocity = CGVectorMake(platformSpeed,0.0) }else{ platform.physicsBody?.veLocity = CGVectorMake(-platformSpeed,0.0) } } overrIDe func touchesBegan(touches: NSSet,withEvent event: UIEvent) { let touch: AnyObject? = touches.anyObject() let location = touch?.locationInNode(self) if(location?.x > 187.5){ player.physicsBody?.applyImpulse(CGVector(dx: 3,dy: 50)) }else{ player.physicsBody?.applyImpulse(CGVector(dx: -3,dy: 50)) } }}
结果如下:
总结以上是内存溢出为你收集整理的swift – 在移动平台上移动节点全部内容,希望文章能够帮你解决swift – 在移动平台上移动节点所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)