I am attempting to "pulse" a sprite in SK/SWIFT. My attempts were crude using For-loops and .setScale
, but they were not working (no errors - just no animation). I feel that using SKActions would perhaps be more elegant.
After using some help from below here is my current implementation. However, it is coming up with compile errors;
Expected member name or constructor call after type name & Consecutive statement on a line must be separated by a ;
Here is the code I am using:
SKAction *pulseUp = [SKAction.scaleTo(3.0, duration: 3.0)]
SKAction *pulseDown = [SKAction.scaleTo(1.0, duration: 3.0)]
SKAction *pulse = [SKAction.sequence(pulseUp, pulseDown)]
SKAction *repeat = [SKAction repeatActionForever:pulse]]
[self.playButton runAction: repeat]
Any help would be greatly appreciated.
In xCode Version 8.3.3 with Swift,
let pulseUp = SKAction.scale(to: 3.0, duration: 1.0)
let pulseDown = SKAction.scale(to: 0.5, duration: 1.0)
let pulse = SKAction.sequence([pulseUp, pulseDown])
let repeatPulse = SKAction.repeatForever(pulse)
self.playButton.run(repeatPulse)
It will be similar to something like this:
SKAction *scaleUpAction = [SKAction ...]; // you need to find the CCScaleTo counterpart
SKAction *scaleDownAction = [SKAction ...];
SKAction *pulse = [SKAction sequence:@[scaleUpAction, scaleDownAction]];
[sprite runAction:[SKAction repeatActionForever:pulse]];
As extension :
import SpriteKit
extension SKSpriteNode {
private static let fillColor = UIColor(red: 0, green: 0.455, blue: 0.756, alpha: 0.45)
func addPulseEffect(circleOfRadius: CGFloat, backgroundColor: UIColor = fillColor) {
let circle = SKShapeNode(circleOfRadius: circleOfRadius)
circle.fillColor = backgroundColor
circle.lineWidth = 0.0
circle.position = CGPoint(x: 0, y: 0)
self.addChild(circle)
let scale = SKAction.scale(to: 3.0, duration: 1.0)
let fadeOut = SKAction.fadeOut(withDuration: 1.0)
let pulseGroup = SKAction.sequence([scale, fadeOut])
let repeatSequence = SKAction.repeatForever(pulseGroup)
circle.run(repeatSequence)
}
func repeatPulseEffectForEver(circleOfRadius: CGFloat) {
let _ = Timer.scheduledTimer(withTimeInterval: 0.8, repeats: true) { (timer) in
self.addPulseEffect(circleOfRadius: circleOfRadius)
}
}
}
If you want a real pulse effect (ie. once scaled up, instantly get to original scale and repeat for ever, do as follows:
func addPulseAction(scale: CGFloat = 50, duration: TimeInterval = 0.5) {
let pulse = = SKAction.sequence([
SKAction.group([
SKAction.scale(to: scale, duration: duration),
SKAction.fadeOut(withDuration: duration)
]),
SKAction.group([
SKAction.scale(to: 1, duration: 0),
SKAction.fadeIn(withDuration: 0)
])
])
let shockwave = SKShapeNode(circleOfRadius: 1)
node.addChild(shockwave)
shockwave.run(SKAction.repeatForever(pulse))
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.