每次当屏幕被点击时,我想增加一个CGFloat.
浮点数具有设定的最大值和最小值.
我试图通过这个建议: StackOverflow
浮点数具有设定的最大值和最小值.
我试图通过这个建议: StackOverflow
然而,这只会在触摸完成后增加CGFloat,或者达到最大值.我想在触摸过程中增加CGFloat,这意味着您触摸跳转/ CGFloat的时间越长.
这个问题可能在冲动之中,在应用之后你不能改变它.这意味着,在“玩家”获得20的冲动之后,屏幕更长时间的触动,浮动可能会增加,但冲动不会.
如果你看看我当前的代码,那么当屏幕被触摸时,脉冲被设置为最大值,但如果释放该动作应该被删除.但是,它不行,冲动不会停止.
我知道您可以在按下按钮之后将身体的速度设置为一个值,如果按下速度已经将速度返回到0,那么它会停止它的“跳跃”,但是看起来不太顺利有冲动.
有人有解决方案吗?
struct Constants { static let minimumJumpForce:CGFloat = 20.0 static let maximumJumpForce:CGFloat = 60.0 } class GameScene: SKScene,SKPhysicsContactDelegate { var force: CGFloat = 20.0 func longPressed(longPress: UIGestureRecognizer) { if (longPress.state == UIGestureRecognizerState.Began) { println("Began") self.pressed = true let HigherJump = SKAction.runBlock({Player.physicsBody?.applyImpulse(CGVectorMake(0,Constants.maximumJumpForce))}) self.runAction(HigherJump,withKey:"HighJump") }else if (longPress.state == UIGestureRecognizerState.Ended) { println("Ended") self.pressed = false self.removeActionForKey("HighJump") } } 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) } } override func touchesEnded(touches: Set<NSObject>,withEvent event: UIEvent) { for touch in (touches as! Set<UITouch>) { let location = touch.locationInNode(self) } } override func update(currentTime: CFTimeInterval) { /* Called before each frame is rendered */ }
解决方法
1.从基于SpriteKit的Xcode模板创建“游戏”
将列出的代码复制到GameScene类中
将列出的代码复制到GameScene类中
import SpriteKit class GameScene: SKScene,SKPhysicsContactDelegate { var location = CGPoint() var floorSize = CGSize() var floorColor = UIColor() var player = SKSpriteNode() override func didMoveToView(view: SKView) { view.showsFPS = true; view.showsNodeCount = true; view.showsDrawCount = true; self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame) self.physicsBody?.categoryBitMask = 1 self.physicsBody?.contactTestBitMask = 1 self.physicsWorld.gravity = CGVectorMake(0,0) self.physicsWorld.contactDelegate = self; location = CGPoint(x:CGRectGetMidX(self.frame),y:CGRectGetMidY(self.frame)) player = SKSpriteNode(imageNamed:"Spaceship") player.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 320,height: 320)) player.physicsBody?.categoryBitMask = 1 player.physicsBody?.collisionBitMask = 1 player.physicsBody?.contactTestBitMask = 1 player.physicsBody?.linearDamping = 0; player.xScale = 1 player.yScale = 1 player.position = location self.addChild(player) } override func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?) { self.physicsWorld.gravity = CGVectorMake(0,0) let direction = Float(1.5708)//Float(player.zRotation) + Float(M_PI_2) player.physicsBody?.applyForce(CGVector(dx: 150000*CGFloat(cosf(direction)),dy: 150000*CGFloat(sinf(direction)))) } override func touchesEnded(touches: Set<UITouch>,-7.9) } }
3.运行应用程序
这应该给你’Jump’游戏的起点:)