我想使用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 */
}
}
}
}
答案 0 :(得分:0)
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
中删除,从而停止播放。
这就是它。如果您有任何疑问,请告诉我!
顺便说一句,我不确定你是否打算这样做,但是当你为对象向上移动时,character
最终会因为引力而上下跳动。