gpt4 book ai didi

ios - 如何使用触摸快速制作角色 Controller ?

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

我想使用 Swift 在 Xcode 中制作一个简单的平台游戏,这样当我按住屏幕左侧时,它会不断向左移动直到放开,反之亦然。我已经添加了跳转功能,但如果您有任何其他建议,我愿意接受建议。

这是我的 GameController.swift 代码: 导入SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {
let character = SKSpriteNode(imageNamed:"square.png")
let block = SKSpriteNode(imageNamed: "square.png")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
//Physics
self.physicsWorld.gravity = CGVectorMake(0.0, -5.0)
self.physicsWorld.contactDelegate = self
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
//Character
character.position = CGPoint(x:CGRectGetMidX(self.frame),
y:CGRectGetMidY(self.frame))
character.setScale(0.25)
character.physicsBody = SKPhysicsBody(rectangleOfSize: character.size)
self.addChild(character)

//block

block.position = CGPoint(x: CGRectGetMidX(self.frame), y:
CGRectGetMidY(self.frame)*0.3)
block.setScale(0.2)
block.physicsBody = SKPhysicsBody(rectangleOfSize: block.size)
block.physicsBody?.dynamic = false
self.addChild(block)


}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch:AnyObject in touches {
let location = touch.locationInNode(self)

if location.x < CGRectGetMidX(self.frame) && location.y <
CGRectGetMidY(self.frame)*0.7{
character.physicsBody?.applyImpulse(CGVector(dx: -50, dy: 0))
} else if location.x > CGRectGetMidX(self.frame) && location.y <
CGRectGetMidY(self.frame)*0.7{
character.physicsBody?.applyImpulse(CGVector(dx: 50, dy: 0))
} else if location.y > character.position.y + 15 {
character.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 50))
}





func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

}
func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
}
}

最佳答案

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
character.removeActionForKey("moveAction")
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch:AnyObject in touches {
let location = touch.locationInNode(self)

if location.x < CGRectGetMidX(self.frame) && location.y < CGRectGetMidY(self.frame)*0.7{
moveAction = SKAction.repeatActionForever(SKAction.moveByX(-20, y: 0, duration: 0.1))
let character.runAction(moveAction, withKey: "moveAction")
} else if location.x > CGRectGetMidX(self.frame) && location.y < CGRectGetMidY(self.frame)*0.7{
let moveAction = SKAction.repeatActionForever(SKAction.moveByX(20, y: 0, duration: 0.1))
character.runAction(moveAction, withKey: "moveAction")
} else if location.y > character.position.y + 15 {
let moveAction = SKAction.repeatActionForever(SKAction.moveByX(0, y: 20, duration: 0.1))
character.runAction(moveAction, withKey: "moveAction")
}
}
}

我编辑了 touchesBegan 方法,并添加了 touchesEnded 方法。您似乎尝试将 嵌入 touchesEnded (和 update)到 touchesBegan 中,但您绝对不能这样做。

好吧,代码非常不言自明,但无论如何:

我添加了一个永远运行的 SKAction,而不是向量。更改持续时间将改变对象移动的速度。持续时间越短,物体速度越快,反之亦然(因为持续时间越短意味着到达某处的时间越短,因此速度越快)。当调用 touchesEnded 时,键为 "moveAction" 的操作将从 character 中删除,从而停止它。

差不多就这些了。如果您有任何疑问,请告诉我!

顺便说一句,我不确定您是否有意这样做,但是当您运行使对象向上移动的 Action 时,角色最终会由于重力而上下弹跳.

关于ios - 如何使用触摸快速制作角色 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34427546/

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