gpt4 book ai didi

swift - 如何让玩家在 Swift 中只跳一次?

转载 作者:行者123 更新时间:2023-11-30 13:28:24 26 4
gpt4 key购买 nike

我想知道如何让玩家只跳跃一次直到落地,就像大多数游戏一样。我试过这个: swift - Jump only when landed ,但它对我来说并没有真正起作用,因为无论出于什么原因,玩家只有在我大量点击屏幕时才会跳跃,而他只会每点击 30 次就会跳跃一次。其外观如下:

它位于:类 GameScene: SkScene {

var ableToJump = true
var Player = SKSpriteNode(imageNamed: "Spieler_rechts")

在:didMoveToView(view: SKView)

Player.position = CGPoint(x: self.frame.size.width*0.1, y: Ground.frame.size.height + Player.frame.size.height)
Player.zPosition = 3
Player.physicsBody = SKPhysicsBody(rectangleOfSize: Player.size)
Player.physicsBody?.allowsRotation = false
Player.physicsBody?.affectedByGravity = true
Player.physicsBody?.restitution = 0
self.addChild(Player)

中:override func update(currentTime: NSTimeInterval)

if Player.physicsBody?.velocity.dy == 0 {
ableToJump = true
}
else {
ableToJump = false
}

中:override func TouchesBegan(touches: Set, withEvent event: UIEvent?)

if ableToJump == true {
Player.physicsBody?.applyImpulse(CGVectorMake(0, 20))
}

最佳答案

我是这样做的:

编辑:

    class MainScene: SKScene, SKPhysicsContactDelegate {

let colorChanger = ColorChanger()
let ground = SKSpriteNode(imageNamed: "Ground")
let player = SKSpriteNode(imageNamed: "Player")
let block1 = SKSpriteNode(imageNamed: "Block2")
let block2 = SKSpriteNode(imageNamed: "Block2")
let scoreLabel = SKLabelNode(fontNamed: "Chalkboard SE")
let highScoreLabel = SKLabelNode(fontNamed: "Chalkboard SE")
let life = SKSpriteNode(imageNamed: "Player")
let life2 = SKSpriteNode(imageNamed: "Player")
let life3 = SKSpriteNode(imageNamed: "Player")
var firstTime = Int()

var blockMaxX = CGFloat(0)
var origBlockPositionX = CGFloat(0)
var score = Int()
var highScore = Int()
var lives = Int()

var groundPositionX = CGFloat(0)
var maxBarX = CGFloat(0)
var groundSpeed = 5
var playerBaseLine = CGFloat(0)
var more = CGFloat(0)

var onGround = true
var velocityY = CGFloat(0)
let gravity = CGFloat(1.3)

enum Collision:UInt32{
case Player = 1
case Block = 2
}



override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
let color1 = colorChanger.randomColor()
runAction(SKAction.colorizeWithColor(color1, colorBlendFactor: 1, duration: 0.2))
self.ground.anchorPoint = CGPointMake(0, 1)
self.ground.position = CGPointMake(CGRectGetMinX(self.frame),CGRectGetMidY(self.frame)-23)
self.ground.size = CGSize(width: 1100, height: 4000)

self.groundPositionX = self.ground.position.x
self.maxBarX = self.ground.size.width - self.frame.size.width - 100
self.maxBarX *= -1

self.player.size = CGSize(width: 50, height: 50)
self.playerBaseLine = self.frame.height / 2
self.player.position = CGPointMake(CGRectGetMinX(self.frame) + self.player.size.width / 1.5 , self.playerBaseLine)
self.player.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(self.player.size.width / 2))
self.player.physicsBody!.affectedByGravity = false
self.player.physicsBody!.categoryBitMask = Collision.Player.rawValue
self.player.physicsBody!.contactTestBitMask = Collision.Block.rawValue
self.player.physicsBody!.collisionBitMask = Collision.Block.rawValue

override func update(currentTime: NSTimeInterval) {

self.velocityY += self.gravity
self.player.position.y -= velocityY

if self.player.position.y < self.playerBaseLine {
self.player.position.y = self.playerBaseLine
velocityY = 0.0
onGround = true
}
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if self.onGround{
self.velocityY = -18.0
self.onGround = false
}
}

你需要利用重力;)

关于swift - 如何让玩家在 Swift 中只跳一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36862739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com