ios – 雪碧套装套装和最大为跳

前端之家收集整理的这篇文章主要介绍了ios – 雪碧套装套装和最大为跳前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在Y轴上移动一个SKSpriteNode.名为Player的SKSpriteNode没有Velocity.如果Platform与之联系,则播放器只能跳转.

每当屏幕被触动时,我想给玩家一个冲动,最小的冲动或最大的冲动

如果屏幕被轻拍,则最小脉冲应该是例如. y = 50.
如果屏幕保持,则表示手指长度在屏幕上,最大值应为例如. y = 100.

但是玩家也应该能够在最小和最大高度之间跳跃,屏幕不长,但也不短短,玩家只能得到y = 70的冲动.

如果屏幕保持,播放器应该跳到最大高度,掉下来,如果它再次与平台接触,则应该跳转,因为您仍然按住屏幕.

我已经尝试了这个线程中的建议答案:StackOverFlow
但这并不是最小的跳跃,也没有按下跳.

为了清楚起见:脉冲不应该在水龙头完成之后,而是在轻敲.你持有的时间越长,跳跃越长.

  1. import SpriteKit
  2. import GameKit
  3.  
  4. struct Constants {
  5.  
  6. static let minimumJumpForce:CGFloat = 40.0
  7. static let maximumJumpForce:CGFloat = 60.0
  8. static let characterSideSpeed:CGFloat = 18.0
  9. }
  10.  
  11. class GameScene: SKScene,SKPhysicsContactDelegate {
  12.  
  13. var Player: SKSpriteNode!
  14.  
  15. var Platform0: SKSpriteNode!
  16.  
  17. var World: SKNode!
  18. var Camera: SKNode!
  19.  
  20. var force: CGFloat = 40.0
  21.  
  22. var pressed = false
  23.  
  24. var isCharacterOnGround = false
  25.  
  26. .....
  27.  
  28. func SpawnPlatforms() {
  29.  
  30. Platform0 = SKSpriteNode (color: SKColor.greenColor(),size: CGSize(width: self.frame.size.width,height: 25))
  31. Platform0.position = CGPoint(x: self.frame.size.width / 2,y: -36)
  32. Platform0.zPosition = 1
  33.  
  34. Platform0.physicsBody = SKPhysicsBody(rectangleOfSize:Platform0.size)
  35. Platform0.physicsBody?.dynamic = false
  36. Platform0.physicsBody?.allowsRotation = false
  37. Platform0.physicsBody?.restitution = 0
  38. Platform0.physicsBody?.usesPreciseCollisionDetection = true
  39.  
  40. Platform0.physicsBody?.categoryBitMask = Platform0Category
  41. Platform0.physicsBody?.collisionBitMask = PlayerCategory
  42. Platform0.physicsBody?.contactTestBitMask = PlayerCategory
  43.  
  44. World.addChild(Platform0)
  45.  
  46. }
  47.  
  48. func SpawnPlayer(){
  49.  
  50. Player = SKSpriteNode (imageNamed: "Image.png")
  51. Player.size = CGSize(width: 64,height: 64)
  52. Player.position = CGPoint(x: self.frame.size.width / 2,y: 0)
  53. Player.zPosition = 2
  54.  
  55. Player.physicsBody = SKPhysicsBody(rectangleOfSize:CGSize(width: 35,height: 50))
  56. Player.physicsBody?.dynamic = true
  57. Player.physicsBody?.allowsRotation = false
  58. Player.physicsBody?.restitution = 0.1
  59. Player.physicsBody?.usesPreciseCollisionDetection = true
  60.  
  61. Player.physicsBody?.categoryBitMask = PlayerCategory
  62. Player.physicsBody?.collisionBitMask = Platform0Category
  63. Player.physicsBody?.contactTestBitMask = Platform0Category | Platform1Category | Platform2Category | Platform3Category | Platform4Category | Platform5Category
  64.  
  65. World.addChild(Player)
  66.  
  67. }
  68.  
  69. func jump(force : CGFloat){
  70.  
  71.  
  72. if(self.isCharacterOnGround){
  73.  
  74. self.Player.physicsBody?.applyImpulse(CGVectorMake(0,force))
  75. self.isCharacterOnGround = false
  76. }
  77.  
  78. }
  79.  
  80. override func touchesBegan(touches: Set<NSObject>,withEvent event: UIEvent) {
  81. /* Called when a touch begins */
  82.  
  83. for touch in (touches as! Set<UITouch>) {
  84. let location = touch.locationInNode(self)
  85.  
  86. self.pressed = true
  87.  
  88. let timerAction = SKAction.waitForDuration(0.0)
  89.  
  90. let update = SKAction.runBlock({
  91. if(self.force < Constants.maximumJumpForce){
  92. self.force += 2.0
  93. }else{
  94. self.jump(Constants.maximumJumpForce)
  95. self.force = Constants.maximumJumpForce
  96. }
  97. })
  98. let sequence = SKAction.sequence([timerAction,update])
  99. let repeat = SKAction.repeatActionForever(sequence)
  100. self.runAction(repeat,withKey:"repeatAction")
  101. }
  102. }
  103.  
  104. override func touchesEnded(touches: Set<NSObject>,withEvent event: UIEvent) {
  105. for touch in (touches as! Set<UITouch>) {
  106. let location = touch.locationInNode(self)
  107.  
  108. self.removeActionForKey("repeatAction")
  109.  
  110. self.jump(self.force)
  111.  
  112. self.force = Constants.minimumJumpForce
  113.  
  114. self.pressed = false
  115.  
  116. }
  117. }
  118.  
  119. func didBeginContact(contact: SKPhysicsContact) {
  120.  
  121. //this gets called automatically when two objects begin contact with each other
  122.  
  123. let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
  124.  
  125. switch(contactMask) {
  126.  
  127. case PlayerCategory | Platform0Category:
  128. //either the contactMask was the bro type or the ground type
  129. println("Contact Made0")
  130. Green = true
  131. self.isCharacterOnGround = true
  132.  
  133. default:
  134. return
  135.  
  136. }
  137.  
  138. }

解决方法

这是一个关于如何制作如下的工作示例:

>按压时间长按压跳跃
>短(一击)
>限制人物在空中跳跃
>手指在屏幕上保持字符跳跃

  1. import SpriteKit
  2.  
  3. struct Constants {
  4.  
  5. static let minimumJumpForce:CGFloat = 15.0
  6. static let maximumJumpForce:CGFloat = 30.0
  7. static let characterSideSpeed:CGFloat = 18.0
  8. }
  9.  
  10. class GameScene: SKScene,SKPhysicsContactDelegate
  11. {
  12. let CharacterCategory : UInt32 = 0x1 << 1
  13. let PlatformCategory : UInt32 = 0x1 << 2
  14. let WallCategory : UInt32 = 0x1 << 3
  15.  
  16. var force: CGFloat = 16.0 //Initial force
  17.  
  18. var pressed = false
  19.  
  20. var isCharacterOnGround = false // Use this to prevent jumping while in the air
  21.  
  22. let character = SKSpriteNode(color: SKColor.greenColor(),size: CGSize(width: 30,height:30))
  23.  
  24.  
  25. let debugLabel = SKLabelNode(fontNamed: "Geneva")
  26.  
  27. override func didMoveToView(view: SKView)
  28. {
  29. //Setup contact delegate so we can use didBeginContact and didEndContact methods
  30. physicsWorld.contactDelegate = self
  31. physicsWorld.speed = 0.5
  32. //Setup borders so character can't escape from us :-)
  33. self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
  34. self.physicsBody?.categoryBitMask = WallCategory
  35. self.physicsBody?.collisionBitMask = CharacterCategory
  36.  
  37.  
  38. //Setup character
  39. character.position = CGPoint(x: 150,y: 150)
  40. character.physicsBody = SKPhysicsBody(rectangleOfSize: character.size)
  41. character.physicsBody?.categoryBitMask = CharacterCategory
  42. character.physicsBody?.contactTestBitMask = PlatformCategory
  43. character.physicsBody?.collisionBitMask = PlatformCategory | WallCategory
  44. character.physicsBody?.allowsRotation = false
  45. character.physicsBody?.dynamic = true
  46. character.physicsBody?.restitution = 0.1
  47.  
  48. self.addChild(character)
  49.  
  50. generatePlatforms()
  51.  
  52. debugLabel.text = " DEBUG: "
  53. debugLabel.fontColor = SKColor.whiteColor()
  54. debugLabel.fontSize = 12.0
  55. debugLabel.position = CGPoint(x: CGRectGetMidX(frame),y: CGRectGetMidY(frame)+100)
  56. self.addChild(debugLabel)
  57.  
  58.  
  59. }
  60.  
  61. func generatePlatforms(){
  62.  
  63.  
  64. for i in 1...4
  65. {
  66.  
  67.  
  68.  
  69. let position = CGPoint(x: CGRectGetMidX(frame),y: CGFloat(i)*140.0 - 100)
  70.  
  71. let platform = createPlatformAtPosition(position)
  72.  
  73. self.addChild(platform)
  74.  
  75. }
  76.  
  77.  
  78. }
  79.  
  80.  
  81. func createPlatformAtPosition(position : CGPoint)->SKSpriteNode{
  82.  
  83. let platform = SKSpriteNode(color: SKColor.greenColor(),size: CGSize(width:frame.size.width,height:20))
  84.  
  85. platform.position = position
  86.  
  87. platform.physicsBody = SKPhysicsBody(
  88. edgeFromPoint: CGPoint(x: -platform.size.width/2.0,y:platform.size.height/2.0),toPoint:CGPoint(x: platform.size.width/2.0,y: platform.size.height/2.0))
  89.  
  90. platform.physicsBody?.categoryBitMask = PlatformCategory
  91. platform.physicsBody?.contactTestBitMask = CharacterCategory
  92. platform.physicsBody?.collisionBitMask = CharacterCategory
  93. platform.physicsBody?.allowsRotation = false
  94. platform.name = "platform"
  95. platform.physicsBody?.dynamic = false
  96. platform.physicsBody?.restitution = 0.0
  97.  
  98. return platform
  99. }
  100.  
  101. func jump(force : CGFloat){
  102.  
  103.  
  104. if(self.isCharacterOnGround){
  105.  
  106. self.character.physicsBody?.applyImpulse(CGVectorMake(0,force))
  107. self.character.physicsBody?.collisionBitMask = WallCategory
  108. self.isCharacterOnGround = false
  109. }
  110.  
  111. }
  112.  
  113. override func touchesBegan(touches: NSSet,withEvent event: UIEvent) {
  114.  
  115. self.pressed = true
  116.  
  117. let timerAction = SKAction.waitForDuration(0.05)
  118.  
  119. let update = SKAction.runBlock({
  120. if(self.force < Constants.maximumJumpForce){
  121. self.force += 2.0
  122. }else{
  123.  
  124.  
  125.  
  126. self.jump(Constants.maximumJumpForce)
  127.  
  128. self.force = Constants.maximumJumpForce
  129.  
  130.  
  131. }
  132. })
  133. let sequence = SKAction.sequence([timerAction,update])
  134. let repeat = SKAction.repeatActionForever(sequence)
  135. self.runAction(repeat,withKey:"repeatAction")
  136.  
  137. }
  138.  
  139.  
  140. override func touchesEnded(touches: NSSet,withEvent event: UIEvent) {
  141.  
  142. self.removeActionForKey("repeatAction")
  143.  
  144. self.jump(self.force)
  145.  
  146. self.force = Constants.minimumJumpForce
  147.  
  148. self.pressed = false
  149. }
  150.  
  151.  
  152. override func update(currentTime: NSTimeInterval) {
  153.  
  154.  
  155. debugLabel.text = "DEBUG: onTheGround : \(isCharacterOnGround),force \(force)"
  156.  
  157.  
  158.  
  159. if(character.position.x <= character.size.width/2.0 + 5.0 && character.physicsBody?.velocity.dx < 0.0 ){
  160.  
  161. character.physicsBody?.applyForce(CGVectorMake(Constants.characterSideSpeed,0.0))
  162.  
  163.  
  164. }else if((character.position.x >= self.frame.size.width - character.size.width/2.0 - 5.0) && character.physicsBody?.velocity.dx >= 0.0){
  165.  
  166. character.physicsBody?.applyForce(CGVectorMake(-Constants.characterSideSpeed,0.0))
  167.  
  168. }else if(character.physicsBody?.velocity.dx > 0.0){
  169.  
  170. character.physicsBody?.applyForce(CGVectorMake(Constants.characterSideSpeed,0.0))
  171.  
  172. }else{
  173.  
  174. character.physicsBody?.applyForce(CGVectorMake(-Constants.characterSideSpeed,0.0))
  175.  
  176. }
  177.  
  178.  
  179. }
  180.  
  181.  
  182. func didBeginContact(contact: SKPhysicsContact) {
  183.  
  184. var firstBody,secondBody: SKPhysicsBody
  185.  
  186. if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
  187. firstBody = contact.bodyA
  188. secondBody = contact.bodyB
  189. } else {
  190. firstBody = contact.bodyB
  191. secondBody = contact.bodyA
  192. }
  193.  
  194.  
  195. if ((firstBody.categoryBitMask & CharacterCategory) != 0 &&
  196. (secondBody.categoryBitMask & PlatformCategory != 0)) {
  197.  
  198. let platform = secondBody.node as SKSpriteNode
  199. // platform.color = UIColor.redColor()
  200. let platformSurfaceYPos = platform.position.y + platform.size.height/2.0
  201.  
  202. let player = contact.bodyB.node as SKSpriteNode
  203. let playerLegsYPos = player.position.y - player.size.height/2.0
  204.  
  205.  
  206.  
  207. if((platformSurfaceYPos <= playerLegsYPos) ){
  208.  
  209. character.physicsBody?.collisionBitMask = PlatformCategory | WallCategory
  210.  
  211.  
  212.  
  213. self.isCharacterOnGround = true
  214.  
  215. if(self.pressed){
  216.  
  217.  
  218. var characterDx = character.physicsBody?.velocity.dx
  219.  
  220. character.physicsBody?.velocity = CGVector(dx: characterDx!,dy: 0.0)
  221.  
  222. self.jump(Constants.maximumJumpForce)
  223. }
  224.  
  225. }
  226.  
  227.  
  228. }
  229.  
  230.  
  231.  
  232. }
  233.  
  234. func didEndContact(contact: SKPhysicsContact) {
  235.  
  236. var firstBody,secondBody: SKPhysicsBody
  237.  
  238. if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
  239. firstBody = contact.bodyA
  240. secondBody = contact.bodyB
  241. } else {
  242. firstBody = contact.bodyB
  243. secondBody = contact.bodyA
  244. }
  245.  
  246. if ((firstBody.categoryBitMask & CharacterCategory) != 0 &&
  247. (secondBody.categoryBitMask & PlatformCategory != 0)) {
  248.  
  249.  
  250. let platform = secondBody.node as SKSpriteNode
  251. let platformSurfaceYPos = platform.position.y + platform.size.height/2.0
  252.  
  253. let player = contact.bodyB.node as SKSpriteNode
  254. let playerLegsYPos = player.position.y - player.size.height/2.0
  255.  
  256.  
  257.  
  258. if((platformSurfaceYPos <= playerLegsYPos) && (character.physicsBody?.velocity.dy > 0.0)){
  259.  
  260. character.physicsBody?.collisionBitMask = WallCategory
  261.  
  262. self.isCharacterOnGround = false
  263.  
  264. }
  265.  
  266. }
  267.  
  268. }
  269.  
  270. }

请注意,这是一个简单的例子,在实际应用中,您可能需要以不同的方式处理像isOnTheGround这样的状态.现在,要确定字符是否在地面上,您只需设置isOnTheGround = true,当字符与平台进行联系时,在didEndContact中设置为false …但是有些情况下,当与空气(如侧面接触)…

编辑:

我改变了代码,让玩家在按下时跳跃.结果如下:

重要:

实际的平台实施和联系人处理取决于你,这并没有被测试.此示例的唯一目的是向您展示如何在按下时跳转.目前,physicsWorld.speed设置为0.5,使动画更慢,因为它更容易调试,但可以将其更改为默认值(1.0).

所以,从图像可以看出,当玩家在第一个平台上时,会出现一些小跳跃(通过简单的敲击或短按).然后(玩家还在第一个平台上)长按了,玩家已经跳上了第二个平台.之后,又做了一个长时间的新闻,但这次没有释放,玩家开始从一个平台跳到另一个平台,最大的力量.

这需要大量的调整和适当的平台&联系检测,但它可以给你一个想法如何实现跳跃你问.

猜你在找的iOS相关文章