I want to make a simple platformer game in Xcode using Swift so that when I hold the left side of the screen, it continuously moves left until let go and vice versa. I have already added a jump feature but if you have any other suggestions I am open to take advice.
我想在Xcode中使用Swift制作一个简单的平台游戏,这样当我按住屏幕的左侧时,它会一直向左移动直到松开,反之亦然。我已经添加了跳转功能,但如果您有任何其他建议,我愿意接受建议。
Here is my GameController.swift code: import SpriteKit
这是我的GameController.swift代码:import 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 */
}
}
}
}
1 个解决方案
#1
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")
}
}
}
I edited the touchesBegan
method and I added a touchesEnded
method. You seem to have tried to embed touchesEnded
(and update
) into the touchesBegan
, which you definitely cannot do.
我编辑了touchesBegan方法,并添加了touchesEnded方法。你似乎试图将touchesEnded(和更新)嵌入到touchesBegan中,你绝对不能这样做。
Okay, so, the code is pretty self explanatory, but anyway:
好的,所以,代码是非常自我解释的,但无论如何:
Instead of what you had with the vectors, I added an SKAction that runs forever. Changing the duration will change how fast the object moves. A lower duration results in a faster object, and vice versa (because lower duration means less time to get somewhere, hence a faster speed). When touchesEnded
is called, the action, whose key is "moveAction"
is removed from character
, thereby stopping it.
我没有使用向量,而是添加了一个永远运行的SKAction。更改持续时间将改变对象移动的速度。较低的持续时间导致更快的对象,反之亦然(因为较低的持续时间意味着较少的时间到达某处,因此速度更快)。当调用touchesEnded时,将从字符中删除其键为“moveAction”的操作,从而停止它。
That's pretty much it. If you have any questions, please, tell me!
这就是它。如果您有任何疑问,请告诉我!
By the way, I'm not sure if you intended for this, but when you run the action for the object to move up, character
ultimately ends up bouncing up and down because of the gravity.
顺便说一句,我不确定你是否打算这样做,但当你为对象向上移动时,角色最终会因为引力而上下跳动。
#1
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")
}
}
}
I edited the touchesBegan
method and I added a touchesEnded
method. You seem to have tried to embed touchesEnded
(and update
) into the touchesBegan
, which you definitely cannot do.
我编辑了touchesBegan方法,并添加了touchesEnded方法。你似乎试图将touchesEnded(和更新)嵌入到touchesBegan中,你绝对不能这样做。
Okay, so, the code is pretty self explanatory, but anyway:
好的,所以,代码是非常自我解释的,但无论如何:
Instead of what you had with the vectors, I added an SKAction that runs forever. Changing the duration will change how fast the object moves. A lower duration results in a faster object, and vice versa (because lower duration means less time to get somewhere, hence a faster speed). When touchesEnded
is called, the action, whose key is "moveAction"
is removed from character
, thereby stopping it.
我没有使用向量,而是添加了一个永远运行的SKAction。更改持续时间将改变对象移动的速度。较低的持续时间导致更快的对象,反之亦然(因为较低的持续时间意味着较少的时间到达某处,因此速度更快)。当调用touchesEnded时,将从字符中删除其键为“moveAction”的操作,从而停止它。
That's pretty much it. If you have any questions, please, tell me!
这就是它。如果您有任何疑问,请告诉我!
By the way, I'm not sure if you intended for this, but when you run the action for the object to move up, character
ultimately ends up bouncing up and down because of the gravity.
顺便说一句,我不确定你是否打算这样做,但当你为对象向上移动时,角色最终会因为引力而上下跳动。