Reputation: 523
Ive been working on this game, and have a ground for my sprite to walk on, but whenever I tap for the sprite to jump, the ground speeds up gradually with every tap to a ridiculous pace and I don't want it to do this. How do I fix it?
Here is my code for the ground:
class MCTGround: SKSpriteNode {
let numberOfSegments = 21
let colorOne = UIColor(red: 88.0/255.0, green: 148.0/255.0, blue:87.0/255.0, alpha: 1.0)
let colorTwo = UIColor(red: 120.0/255.0, green: 195.0/255.0, blue: 118.0/255.0, alpha: 1.0)
init(size: CGSize) {
super.init(texture: nil, color: UIColor.brownColor(), size: CGSizeMake(size.width * 2, size.height))
anchorPoint = CGPointMake(0, 0.5)
for var i = 0; i < numberOfSegments; i++ {
var segmentColor: UIColor!
if i % 2 == 0 {
segmentColor = colorOne
} else {
segmentColor = colorTwo
}
let segment = SKSpriteNode(color: segmentColor, size: CGSizeMake(self.size.width / CGFloat(numberOfSegments), self.size.height))
segment.anchorPoint = CGPointMake(0.0, 0.5)
segment.position = CGPointMake(CGFloat(i) * segment.size.width, 0)
addChild(segment)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func start() {
let moveLeft = SKAction.moveByX(-frame.size.width / 2, y: 0, duration: 1.0)
let resetPosition = SKAction.moveToX(0, duration: 0)
let moveSequence = SKAction.sequence([moveLeft, resetPosition])
runAction(SKAction.repeatActionForever(moveSequence))
}
}
Edit: My touchesBegan func:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
jumpPlayer()
movingGround.start()
fruitGenerator.startGeneratingFruitEvery(3)
let frames = [
SKTexture(imageNamed: "koala_idle"),
SKTexture(imageNamed: "koala_walk01"),
SKTexture(imageNamed: "koala_walk02"),
]
let duration = 1.5 + drand48() * 1.0
let move = SKAction.animateWithTextures(frames, timePerFrame:0.10)
let wait = SKAction.waitForDuration(duration)
let rest = SKAction.setTexture(frames[0])
let sequence = SKAction.sequence([move, rest])
player.runAction(SKAction.repeatActionForever(sequence))
}
Upvotes: 0
Views: 55
Reputation: 14824
It's because you add an additional action to the background every time. Just do:
var hasBegun = false
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
jumpPlayer()
if !hasBegun {
hasBegun = true
movingGround.start()
fruitGenerator.startGeneratingFruitEvery(3)
}
...
or any other method of only running those "start"-type methods once.
Upvotes: 0