每当屏幕被触动时,我想给玩家一个冲动,最小的冲动或最大的冲动
如果屏幕被轻拍,则最小脉冲应该是例如. y = 50.
如果屏幕保持,则表示手指长度在屏幕上,最大值应为例如. y = 100.
但是玩家也应该能够在最小和最大高度之间跳跃,屏幕不长,但也不短短,玩家只能得到y = 70的冲动.
如果屏幕保持,播放器应该跳到最大高度,掉下来,如果它再次与平台接触,则应该跳转,因为您仍然按住屏幕.
我已经尝试了这个线程中的建议答案:StackOverFlow
但这并不是最小的跳跃,也没有按下跳.
为了清楚起见:脉冲不应该在水龙头完成之后,而是在轻敲.你持有的时间越长,跳跃越长.
import SpriteKitimport GameKitstruct Constants {static let minimumJumpForce:CGfloat = 40.0static let maximumJumpForce:CGfloat = 60.0static let characterSIDeSpeed:CGfloat = 18.0}class GameScene: SKScene,SKPhysicsContactDelegate {var Player: SKSpriteNode!var Platform0: SKSpriteNode!var World: SKNode!var Camera: SKNode!var force: CGfloat = 40.0var pressed = falsevar isCharacterOnGround = false.....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 = 1Platform0.physicsBody = SKPhysicsBody(rectangleOfSize:Platform0.size)Platform0.physicsBody?.dynamic = falsePlatform0.physicsBody?.allowsRotation = falsePlatform0.physicsBody?.restitution = 0Platform0.physicsBody?.usesPreciseCollisionDetection = truePlatform0.physicsBody?.categoryBitMask = Platform0categoryPlatform0.physicsBody?.collisionBitMask = PlayercategoryPlatform0.physicsBody?.contactTestBitMask = PlayercategoryWorld.addChild(Platform0)}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 = 2Player.physicsBody = SKPhysicsBody(rectangleOfSize:CGSize(wIDth: 35,height: 50))Player.physicsBody?.dynamic = truePlayer.physicsBody?.allowsRotation = falsePlayer.physicsBody?.restitution = 0.1Player.physicsBody?.usesPreciseCollisionDetection = truePlayer.physicsBody?.categoryBitMask = PlayercategoryPlayer.physicsBody?.collisionBitMask = Platform0categoryPlayer.physicsBody?.contactTestBitMask = Platform0category | Platform1category | Platform2category | Platform3category | Platform4category | Platform5categoryWorld.addChild(Player)}func jump(force : CGfloat){ if(self.isCharacterOnGround){ self.Player.physicsBody?.applyImpulse(CGVectorMake(0,force)) self.isCharacterOnGround = false }}overrIDe func touchesBegan(touches: Set<NSObject>,withEvent event: UIEvent) { /* Called when a touch begins */ for touch in (touches as! Set<UItouch>) { let location = touch.locationInNode(self) self.pressed = true let timerAction = SKAction.waitForDuration(0.0) let update = SKAction.runBlock({ if(self.force < Constants.maximumJumpForce){ self.force += 2.0 }else{ self.jump(Constants.maximumJumpForce) self.force = Constants.maximumJumpForce } }) let sequence = SKAction.sequence([timerAction,update]) let repeat = SKAction.repeatActionForever(sequence) self.runAction(repeat,withKey:"repeatAction") }}overrIDe func touchesEnded(touches: Set<NSObject>,withEvent event: UIEvent) { for touch in (touches as! Set<UItouch>) { let location = touch.locationInNode(self) self.removeActionForKey("repeatAction") self.jump(self.force) self.force = Constants.minimumJumpForce self.pressed = false}}func dIDBeginContact(contact: SKPhysicsContact) { //this gets called automatically when two objects begin contact with each other let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask switch(contactMask) { case Playercategory | Platform0category: //either the contactMask was the bro type or the ground type println("Contact Made0") Green = true self.isCharacterOnGround = true default: return }}解决方法 这是一个关于如何制作如下的工作示例:
>按压时间长按压跳跃
>短(一击)
>限制人物在空中跳跃
>手指在屏幕上保持字符跳跃
import SpriteKit struct Constants { static let minimumJumpForce:CGfloat = 15.0 static let maximumJumpForce:CGfloat = 30.0 static let characterSIDeSpeed:CGfloat = 18.0 } class GameScene: SKScene,SKPhysicsContactDelegate { let Charactercategory : UInt32 = 0x1 << 1 let Platformcategory : UInt32 = 0x1 << 2 let Wallcategory : UInt32 = 0x1 << 3 var force: CGfloat = 16.0 //Initial force var pressed = false var isCharacterOnGround = false // Use this to prevent jumPing while in the air let character = SKSpriteNode(color: SKcolor.greencolor(),size: CGSize(wIDth: 30,height:30)) let deBUGLabel = SKLabelNode(Fontnamed: "Geneva") overrIDe func dIDMovetoVIEw(vIEw: SKVIEw) { //Setup contact delegate so we can use dIDBeginContact and dIDEndContact methods physicsWorld.contactDelegate = self physicsWorld.speed = 0.5 //Setup borders so character can't escape from us :-) self.physicsBody = SKPhysicsBody(edgeLoopFromrect: self.frame) self.physicsBody?.categoryBitMask = Wallcategory self.physicsBody?.collisionBitMask = Charactercategory //Setup character character.position = CGPoint(x: 150,y: 150) character.physicsBody = SKPhysicsBody(rectangleOfSize: character.size) character.physicsBody?.categoryBitMask = Charactercategory character.physicsBody?.contactTestBitMask = Platformcategory character.physicsBody?.collisionBitMask = Platformcategory | Wallcategory character.physicsBody?.allowsRotation = false character.physicsBody?.dynamic = true character.physicsBody?.restitution = 0.1 self.addChild(character) generatePlatforms() deBUGLabel.text = " DEBUG: " deBUGLabel.Fontcolor = SKcolor.whitecolor() deBUGLabel.FontSize = 12.0 deBUGLabel.position = CGPoint(x: CGRectGetMIDX(frame),y: CGRectGetMIDY(frame)+100) self.addChild(deBUGLabel) } func generatePlatforms(){ for i in 1...4 { let position = CGPoint(x: CGRectGetMIDX(frame),y: CGfloat(i)*140.0 - 100) let platform = createPlatformAtposition(position) self.addChild(platform) } } func createPlatformAtposition(position : CGPoint)->SKSpriteNode{ let platform = SKSpriteNode(color: SKcolor.greencolor(),size: CGSize(wIDth:frame.size.wIDth,height:20)) platform.position = position platform.physicsBody = SKPhysicsBody( 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)) platform.physicsBody?.categoryBitMask = Platformcategory platform.physicsBody?.contactTestBitMask = Charactercategory platform.physicsBody?.collisionBitMask = Charactercategory platform.physicsBody?.allowsRotation = false platform.name = "platform" platform.physicsBody?.dynamic = false platform.physicsBody?.restitution = 0.0 return platform } func jump(force : CGfloat){ if(self.isCharacterOnGround){ self.character.physicsBody?.applyImpulse(CGVectorMake(0,force)) self.character.physicsBody?.collisionBitMask = Wallcategory self.isCharacterOnGround = false } } overrIDe func touchesBegan(touches: NSSet,withEvent event: UIEvent) { self.pressed = true let timerAction = SKAction.waitForDuration(0.05) let update = SKAction.runBlock({ if(self.force < Constants.maximumJumpForce){ self.force += 2.0 }else{ self.jump(Constants.maximumJumpForce) self.force = Constants.maximumJumpForce } }) let sequence = SKAction.sequence([timerAction,update]) let repeat = SKAction.repeatActionForever(sequence) self.runAction(repeat,withKey:"repeatAction") } overrIDe func touchesEnded(touches: NSSet,withEvent event: UIEvent) { self.removeActionForKey("repeatAction") self.jump(self.force) self.force = Constants.minimumJumpForce self.pressed = false } overrIDe func update(currentTime: NSTimeInterval) { deBUGLabel.text = "DEBUG: onTheGround : \(isCharacterOnGround),force \(force)" if(character.position.x <= character.size.wIDth/2.0 + 5.0 && character.physicsBody?.veLocity.dx < 0.0 ){ character.physicsBody?.applyForce(CGVectorMake(Constants.characterSIDeSpeed,0.0)) }else if((character.position.x >= self.frame.size.wIDth - character.size.wIDth/2.0 - 5.0) && character.physicsBody?.veLocity.dx >= 0.0){ character.physicsBody?.applyForce(CGVectorMake(-Constants.characterSIDeSpeed,0.0)) }else if(character.physicsBody?.veLocity.dx > 0.0){ character.physicsBody?.applyForce(CGVectorMake(Constants.characterSIDeSpeed,0.0)) }else{ character.physicsBody?.applyForce(CGVectorMake(-Constants.characterSIDeSpeed,0.0)) } } func dIDBeginContact(contact: SKPhysicsContact) { var firstbody,secondBody: SKPhysicsBody if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask { firstbody = contact.bodyA secondBody = contact.bodyB } else { firstbody = contact.bodyB secondBody = contact.bodyA } if ((firstbody.categoryBitMask & Charactercategory) != 0 && (secondBody.categoryBitMask & Platformcategory != 0)) { let platform = secondBody.node as SKSpriteNode // platform.color = UIcolor.redcolor() let platformSurfaceYPos = platform.position.y + platform.size.height/2.0 let player = contact.bodyB.node as SKSpriteNode let playerLegsYPos = player.position.y - player.size.height/2.0 if((platformSurfaceYPos <= playerLegsYPos) ){ character.physicsBody?.collisionBitMask = Platformcategory | Wallcategory self.isCharacterOnGround = true if(self.pressed){ var characterDx = character.physicsBody?.veLocity.dx character.physicsBody?.veLocity = CGVector(dx: characterDx!,dy: 0.0) self.jump(Constants.maximumJumpForce) } } } } func dIDEndContact(contact: SKPhysicsContact) { var firstbody,secondBody: SKPhysicsBody if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask { firstbody = contact.bodyA secondBody = contact.bodyB } else { firstbody = contact.bodyB secondBody = contact.bodyA } if ((firstbody.categoryBitMask & Charactercategory) != 0 && (secondBody.categoryBitMask & Platformcategory != 0)) { let platform = secondBody.node as SKSpriteNode let platformSurfaceYPos = platform.position.y + platform.size.height/2.0 let player = contact.bodyB.node as SKSpriteNode let playerLegsYPos = player.position.y - player.size.height/2.0 if((platformSurfaceYPos <= playerLegsYPos) && (character.physicsBody?.veLocity.dy > 0.0)){ character.physicsBody?.collisionBitMask = Wallcategory self.isCharacterOnGround = false } } }}
请注意,这是一个简单的例子,在实际应用中,您可能需要以不同的方式处理像isOnTheGround这样的状态.现在,要确定字符是否在地面上,您只需设置isOnTheGround = true,当字符与平台进行联系时,在dIDEndContact中设置为false …但是有些情况下,当与空气(如侧面接触)…
编辑:
我改变了代码,让玩家在按下时跳跃.结果如下:
重要:
实际的平台实施和联系人处理取决于你,这并没有被测试.此示例的唯一目的是向您展示如何在按下时跳转.目前,physicsWorld.speed设置为0.5,使动画更慢,因为它更容易调试,但可以将其更改为默认值(1.0).
所以,从图像可以看出,当玩家在第一个平台上时,会出现一些小跳跃(通过简单的敲击或短按).然后(玩家还在第一个平台上)长按了,玩家已经跳上了第二个平台.之后,又做了一个长时间的新闻,但这次没有释放,玩家开始从一个平台跳到另一个平台,最大的力量.
这需要大量的调整和适当的平台&联系检测,但它可以给你一个想法如何实现跳跃你问.
总结以上是内存溢出为你收集整理的ios – 雪碧套装套装和最大为跳全部内容,希望文章能够帮你解决ios – 雪碧套装套装和最大为跳所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)