Modify a repeating task sequence while running - ios

I am trying to make a repeating task where I can change the delay in which it repeats. Here is the current code I am using:
var actionwait = SKAction.waitForDuration(self.wait)
var actionrun = SKAction.runBlock({
self.count+=1
if (self.count % 2 == 0 && self.wait > 0.2){
self.wait -= 0.1
actionwait.duration = self.wait
}
for node in self.children{
if (node.isMemberOfClass(Dot)){
node.removeFromParent()
}
}
var radius = CGFloat(arc4random_uniform(100) + 30)
var newNode = Dot(circleOfRadius: radius)
var color = self.getRandomColor()
newNode.fillColor = color
newNode.strokeColor = color
newNode.yScale = 1.0
newNode.xScale = 2.0
newNode.userInteractionEnabled = true
newNode.setScene(self)
newNode.position = newNode.randomPos(self.view!)
self.addChild(newNode)
})
self.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))
However, it appears that because the sequence is already repeating, changing the duration of the delay does not effect anything.

when you create actionwait it's going to save the values inside that closure and keep using them in your repeatActionForever
When you're tracking changes it's best to do that sort of thing in the update method. Using actions in this case might not be the best approach.
I'm not sure about when you (for your game) need to be checking for changes. For most things using the update method is adequate
heres one way i implement a timer in update
// MY TIMER PROPERTIES IN MY CLASS
var missleTimer:NSTimeInterval = NSTimeInterval(2)
var missleInterval:NSTimeInterval = NSTimeInterval(2)
// IN MY UPDATE METHOD
self.missleTimer -= self.delta
if self.missleTimer <= 0 { // TIMER HIT ZERO, DO SOMETHING!
self.launchMissle() // LAUNCH MY MISSLE
self.missleTimer = self.missleInterval // OKAY LETS RESET THE TIMER
}
delta is the difference of time between this frame and the last frame. It's used create fluid motion, and track time. Things like that. This is pretty standard among spritekit projects. you usually need to use delta projectwide
declare these two properties:
// time values
var delta:NSTimeInterval = NSTimeInterval(0)
var last_update_time:NSTimeInterval = NSTimeInterval(0)
This should be how the top of your update method looks
func update(currentTime: NSTimeInterval) {
if self.last_update_time == 0.0
self.delta = 0
} else {
self.delta = currentTime - self.last_update_time
}
self.last_update_time = currentTime

Related

Simple combo multiplier in sprite-kit

I am making a reaction game, where you can destroy enemys and earn points. Now I would like to have combo points if you destroy them fast and if there is a specific time gap the combo multiplier should go to zero again.
I would like to multiple the points like this: 2 * 2 = 4 * 2 = 8 * 2 = 16 * 2...
(you get 2 points if you destroy an enemy).
I add the points here:
if (CGRectIntersectsRect(enemy.frame, player.frame)) {
points = points + 1;
[enemy removeFromParent];
}
I could always multiply the current points with 2, but I want to reset the combo multiplier if there is specific amount of time without getting points.
I hope someone can help me.
(code in objective c please)
It seems no more complicated than recording the time the last enemy was destroyed and then in the update: method deciding if the combo has elapsed as no more enemies were hit in whatever timeout period you allow.
I am not familiar with Sprite kit, but the update appears to pass the current time; excellent. You will need to record the following:
timeout (time): The current timeout. This will reduce as the game progresses, making it harder.
lastEnemyKillTime (time): the time the last enemy was killed.
comboPoints (integer): How many points the user gets per hit. This will increase as the combo extends.
points (integer): The current score.
So, something like this:
#interface MyClass ()
{
NSTimeInterval _timeout;
NSTimeInterval _lastEnemyKillTime;
BOOL _comboFactor;
NSUInteger _points;
}
#end
I guess Sprite Kit uses an init: method; use it to initialize the variables:
- (id)init
{
self = [super init];
if (self != nil) {
_timeout = 1.0;
_lastEnemyKillTime = 0.0;
_points = 0;
_comboPoints = 1;
}
}
The update: method would be something like:
- (void)update:(NSTimeInterval)currentTime
{
BOOL withinTimeout = currentTime - _lastEnemyKillTime <= _timeout;
if (CGRectIntersectsRect(enemy.frame, player.frame)) {
_inCombo = withinTimeout;
if (_inCombo)
_comboPoints *= 2;
_points += _comboPoint;
_lastEnemyKillTime = currentTime;
[enemy removeFromParent];
} else if (_comboPoints > 1 && !withinTimeout) {
_lastEnemyKillTime = 0.0;
_comboPoints = 1;
}
}
You need to keep track on the last enemy casual timestamp and the factor. When the next kill is processed, you check the timestamp, if it is below threshold, you raise the factor. The time of the current kill replaces the timestamp.
You could create a FightRecorder class as singleton, if you don't have a better place yet (services or sth).
NSDate *newKillTime = new NSDate;
FightRecorder recorder = [FightRecorder instance];
if([newKillTime timeIntervalSinceDate:recorder.lastKillTime] < SCORE_BOUNDS_IN_SEC) {
recorder.factor++; // could also be a method
points = points + [recorder calculateScore]; // do your score math here
}
else {
[recorder reset]; // set the inner state of the fight recorder to no-bonus
}
recorder.lastKillTime = newKillTime; // record the date for the next kill

Updating parameter of an SKAction that is repeating forever swift

I have a function that animates a ball in a game that I am designing right now. However, I want the animation speed to change with the balls actual velocity, which I have achieved but only after each iteration of the animation. That makes it come out a little choppy, I am looking for a more elegant solution that could update the timePerFrame argument mid-action. I originally had it set to repeatforever but realized that timePerFrame wouldn't update once the action had started. Is there a way to make this animation speed change more smoothly?
func animBall() {
//This is the general runAction method to make our ball change color
var animSpeedNow = self.updateAnimSpeed()
println(animSpeedNow)
var animBallAction = SKAction.animateWithTextures(ballColorFrames, timePerFrame: animSpeedNow,resize: false, restore: true)
self.runAction(animBallAction, completion: {() -> Void in
self.animBall()
})
}
func updateAnimSpeed() -> NSTimeInterval{
// Update the animation speed based on the velocity to syncrhonize animation with ball velocity
var velocX = self.physicsBody!.velocity.dx
var velocY = self.physicsBody!.velocity.dy
if abs(velocX) > 0 || abs(velocY) > 0 {
var veloc = sqrt(velocX*velocX + velocY*velocY)
var animSpeedNow: NSTimeInterval = NSTimeInterval(35/veloc)
let minAS = NSTimeInterval(0.017)
let maxAS = NSTimeInterval(0.190)
if animSpeedNow < minAS {
return maxAS
}
else if animSpeedNow > maxAS {
return minAS
}
else {
return animSpeedNow
}
}
else {
return NSTimeInterval(0.15)
}
}
If it isn't possible to directly manipulate a parameter of an SKAction that is running forever, I supposed I
In your case, a simpler way would be to remove the old animation action and replace it with a new one.

iOS Swift SpriteKit - how to detect time elapsed between user touches on sprites

I have set up a match-3 game, and I want to track the number of seconds a user takes to touch valid points (game piece sprites) on the game board:
(1) when a game level is loaded, if the user does not touch a sprite within 5 seconds, track this;
(2) while playing the game, if the time elapsed between the user touching a sprite is greater than 5 seconds, track this.
(I will use these results to provide a hint to the user).
I would like to use a NSTimer/NSTimeInterval for this, but not sure how to implement it.
I wouldn't suggest adding an integer as others suggested.
All you need is delta time. I'm assuming you have that part worked out, but I'll post it just in case
scene properties
// time values
var delta:NSTimeInterval = NSTimeInterval(0)
var last_update_time:NSTimeInterval = NSTimeInterval(0)
your scene's update method (also create an update method for your sprites, and pass delta into it here)
func update(currentTime: NSTimeInterval) {
if self.last_update_time == 0.0 {
self.delta = 0
} else {
self.delta = currentTime - self.last_update_time
}
self.yourSprite.update(self.delta)
your sprite's time properties
var timeSinceTouched = NSTimeInterval(0)
let timeLimit = NSTimeInterval(5.0)
your sprites update / touched method
func touched(){
self.timeSinceTouched = 0.0
}
func update(delta: CFTimeInterval) {
if self.timeSinceTouched < self.timeLimit {
self.timeSinceTouched += delta
} else {
// five seconds has elapsed
}

Action not working correctly in SpriteKit

I'm new to iOS programing and I'm experimenting to learn trying to create a game in swift using Sprite Kit.
What I'm trying to achieve is having a constant flow of blocks being created and moving rightwards on the screen.
I start by creating a set which contains all the initial blocks, then an action "constant movement" is added to each one, which makes them move slowly to the right. What I'm having trouble is adding new blocks to the screen.
The last column of blocks has an "isLast" boolean set to true, when it passes a certain threshold it is supposed to switch to false and add a new column of blocks to the set which now have "isLast" set to true.
Each block in the set has the "constantMovement" action added which makes them move slowly to the right, the new blocks have it added as well, but they don't work as the original ones.
Not all of the move, even tho if I print "hasActions()" it says they do, and the ones that do move stop doing so when they get to the middle of the screen. I have no idea why this happens, can somebody experienced give me a hint please?
This is the update function:
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
let constantMovement = SKAction.moveByX(-1, y: 0, duration: 10);
background.runAction(SKAction.repeatActionForever(constantMovement));
let removeBlock = SKAction.removeFromParent();
let frame = self.frame;
var currentBlockSprite:SKSpriteNode;
var newBlock: Block;
for block in blocks {
currentBlockSprite = block.sprite!;
currentBlockSprite.runAction(constantMovement);
if(block.column == NumColumns - 1) {
block.isLast = true;
}
if(block.isNew) {
println("position \(currentBlockSprite.position.x) has actions \(currentBlockSprite.hasActions())");
}
if(block.isLast && currentBlockSprite.position.x < frame.maxX - 50) {
println("the block that hits is " + block.description);
println("HITS AT \(currentBlockSprite.position.x)");
block.isLast = false;
for row in 0..<NumRows {
newBlock = Block(column: NumColumns - 1, row: row, blockType: BlockType.random(), isLast: true, isNew: true);
blocks.addElement(newBlock);
addBlockSprite(newBlock);
println("new block: " + newBlock.description + "position \(newBlock.sprite?.position.x)");
}
}
if(currentBlockSprite.position.x < frame.minX) {
currentBlockSprite.runAction(removeBlock);
blocks.removeElement(block);
}
}
}
My whole project is in here: https://github.com/thanniaB/JumpingGame/tree/master/Experimenting
but keep in mind that since I'm new to this it might be full of cringeworthy bad practices.
I would remove any SKAction code from the update function as that's kind of a bad idea. Instead I would just apply the SKAction when you add your block sprite to the scene, like this.
func addBlockSprite(block: Block) {
let blockSprite = SKSpriteNode(imageNamed: "block");
blockSprite.position = pointForColumn(block.column, row:block.row);
if(block.blockType != BlockType.Empty) {
addChild(blockSprite);
let constantMovement = SKAction.moveByX(-10, y: 0, duration: 1)
var currentBlockSprite:SKSpriteNode
let checkPosition = SKAction.runBlock({ () -> Void in
if(blockSprite.position.x < -512){
blockSprite.removeAllActions()
blockSprite.removeFromParent()
}
})
let movementSequence = SKAction.sequence([constantMovement, checkPosition])
let constantlyCheckPosition = SKAction.repeatActionForever(movementSequence)
blockSprite.runAction(constantlyCheckPosition)
}
block.sprite = blockSprite;
}
That would then allow you to simply add a new block whenever you see fit and it will have the appropriate action when it's added.
I've used 512 as thats the size of the iPhone 5 screen but you could swap this out for another screen size or what would be better would be a variable that dynamically reflects the screen size.

Updating the position of a node constantly?

I am experiencing some problems with my game, I am currently using the update method to create the illusion of my Hero character being "dragged" along with a moving platform. The update method works by using a bool that switches on and off whenever my Hero makes contact with the platform and then updates the position to seemingly show that the Hero is being moved. This worked nicely, but as I realize when the framerate drops, so does the character's positioning and it breaks the illusion.
Is there any sort of way to constantly update the positioning of a node without using the update method?
didBeginContact Method
-(void)didBeginContact:(SKPhysicsContact *)contact {
if (((firstBody.categoryBitMask & fPlayerCategory) != 0) && ((secondBody.categoryBitMask & fPlatformCategory) != 0))
{
[_Hero removeActionForKey:#"idleAnimation"];
[_Hero runAction:repeatWalkAnimation withKey:#"walkAnimation"];
syncMove = YES;
}
}
Update Method
-(void)update:(CFTimeInterval)currentTime {
if (gameStart == YES & gameOver == NO) {
if (syncMove == YES) {
_Hero.position = CGPointMake(_Hero.position.x - .83, _Hero.position.y);
}
}
}
Any sort of help would be appreciated.
You can use the currentTime argument in your update function to calculate how much _Hero should have moved. This way _Hero will always move the appropriate amount based on the time since the last update.
The steps to do this are:
Store your last update time
On each update, calculate the difference between the last update time and the current time
Calculate how far _Hero should have moved in that time
Update _Hero's position
Something like:
CFTimeInterval lastUpdateTime = 0;
CGFloat frameRate = 1.0 / 60.0; // Normal SpriteKit frame rate
CGFloat xDistanceForNormalFrameRate = 0.83;
-(void)update:(CFTimeInterval)currentTime {
if (lastUpdateTime == 0) lastUpdateTime = currentTime - frameRate; // Init lastUpdateTime to one frame back
if (gameStart && !gameOver) { // Note: the original question had & here, but it looks like it was a typo
if (syncMove) {
CFTimeInterval timeDifference = currentTime - lastUpdateTime;
CGFloat xDistance = xDistanceForNormalFrameRate * (timeDifference / frameRate);
_Hero.position = CGPointMake(_Hero.position.x - xDistance, _Hero.position.y);
}
}
lastUpdateTime = currentTime;
}
Note: If your frame rate is dropping below ~30FPS, you should probably focus more on why your frame rate is dropping than how to work around it.

Resources