Dispatch and Delay between calls - ios

I'm making a clone for that old game Simon (a.k.a. Genius, in Brazil), the one with the for coloured buttons which the player needs to press following a sequence of colors.
For testing, interface has 4 coloured buttons
I created an array for the button outlets, for easy access:
var buttonArray:[UIButton] = [self.greenButton, self.yellowButton, self.redButton, self.blueButton]
Also, created another array to store the sequence of colors
var colors:[Int] = []
When a game starts it calls a function which adds a random number from 0 to 3 (index on buttonArray), and add this number to the colors array
After adding a new color the color sequence, the app needs to show the sequence for the user, so he can repeat it
For that, it calls the playMoves function, which uses a for loop the run through the colors array and change the alpha from the button, simulating a 'blink'
func playMoves(){
let delay = 0.5 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
for i in self.colors{
self.buttonArray[i].alpha = 0.2
dispatch_after(time, dispatch_get_main_queue(), {
self.buttonArray[i].alpha = 1
})
}
}
It changes the alpha from the button to 0.2 and then, after half a second it returns the alpha to 1. I was using dispatch_after, passing the 0.5 seconds and on the code block it returns the alpha, as you guys can see on the code above.
On the first run, it appears to do it correctly, but when the colors array has 2 or more items, when it runs the loop, although it has a 0.5 sec delay, it blinks all the buttons on the same time.
It's probably some dumb mistake I'm making, but I'm clueless on the moment.
I would appreciate very much all the help!
Thanks!

All of these dispatch_after calls are scheduled at nearly the same time, making them appear to blink at the same time. There are a couple of approaches that would solve this:
You could, for example, adjust the when parameter (the dispatch_time_t parameter) for each button to be offset from the original time (such that delay was, effectively i * 0.5 * Double(NSEC_PER_SEC)).
You could also use key-frame animation, but I'd suggest you first try fixing the delay in your dispatch_after approach first.

Related

How to implement countdown function like Yahoo's APP News Digest

I can use CAShapeLayer and UIBezierPath to draw the circle, I can also use this property CAShapeLayer.strokeEnd to control the progress. But the fast scrolling of path and time , I do not know how to implement.
Now I think the approach is to calculate the time difference between the two , and then use the time difference to circulation .
For example, The time difference between the two is 1000 seconds , should I have to set strokeEnd and the middle of the Label 1000 cycles ? Or that implement good results it ?
thanks in advance!
Note : StrokeEnd accepts value between 0 - 1
Lets Say,
See in your case circle represents the remaining time it will take to appear new feed. lets say 3PM.
so 3 PM will be your nextFeedTime = 3PM and,
you got this feed at 12PM so feedTime = 12PM.
So now you will have start and end value of feedTime=12PM - nextFeedTime=3PM
So feedTime is 0 for strokeEnd and nextFeedTime is 1 for StrokeEnd.
When you open app you will got currentTime which is initially initialised with feedTime and later it will be replaced all the time with current time stamp.
Lets assume currentTime is 1 PM.
Now we can calculate ratio to animate strockEnd property
strokeEnd = currentTime / nextFeedTime
and animate strokeEnd accordingly. hope this will helps you!

iOS: Animating Multiple UIBezierPaths (One After the Other)?

I have an array of UIBezierPaths, but I'm having trouble doing so.
I've tried calculating the beginTime by doing the following, but all it does is animate the first line, but not the rest of the lines (they just appear without an animation).
pathAnimation.beginTime = n * lineDuration;
pathAnimation.beginTime = n * lineDuration;
Well, that's your problem. That time is in the deep dark past.
You want a time in the future. Start by getting the CACurrentMediaTime() and now add the delay amount to that.

Cleaner (better) alternative to using update function in Swift

In short:
Is there a better way to run large functions for a long time that's less 'lagy' and memory abusive than putting it all in the update function?
Full question
I'm currently in the final stages of developing my first game app using iOS Swift and SpriteKit. One of the bigger problems I got is lag/delay. The Time Profiler Instrument pointed out that my override func update was using a lot of memory and time. My update function consists of the following:
override func update(currentTime: CFTimeInterval) {
if hasStarted {
// Character
updateCharacterPosition()
updateJumpMotion()
// Blocks
blockRunnerDebug()
// Wave
debugRunningBarPosition()
// Game Engine
gameEngine()
debugGameEngine()
}
// Update Scenery
updateScenery()
}
As you can see it has a lot of functions. Most of them run background animations like water, clouds or the jumping animation of the character.
Example:
// Move y
self.cloud01.position.y = self.cloud01.position.y + (CGFloat(createSinWave(0.5, b: b, angle: angle))) * 0.3
self.cloud02.position.y = self.cloud02.position.y + (CGFloat(createSinWave(0.5, b: b, angle: angle))) * 0.3
My question is: is there a better way to run large functions for a long time that's less 'lagy' and memory abusive than putting it all in the update function?
Thanks
Items that may change direction or position based on user interaction need to be in update. There is no way around that.
For items like background animations you can (and should) set them up once at the beginning and put them into an infinite animation loop instead of manually updating the position every update. That should help a lot.

AS2 - Display game results at end of level

I'm currently making a maze game at the minute, and I want to include a death counter and a count-up timer. When the level is over, I want the game to display the time taken and the number of deaths made. How can I make it save the death count, and display it at the end?
On your first frame, declare these two variables which will hold the number of deaths and count up (paste the code on your Frame actions):
var deaths = 0;
var time = 0;
Now, whenever the player dies, use this code to increase the death count:
deaths++;
When you want to start the timer, paste this code on the frame where the game begins:
function countUp(){
time++;
}
setInterval(countUp, 1000);
This will run the function every 1000th milliseconds, which is the same as every second (1000 milliseconds = 1 second). The function itself increases the time variable by 1, but it does not display it on the screen.
Now, on the results screen, you first have to make two Dynamic Text Fields (create a text field, open the Properties Panel [CTRL+F3], and change it from Static text to Dynamic text). Give these two Dynamic text fields the instance names of, death_txt and timer_txt, respectively. Then, paste this code on the frame actions of the results screen:
death_txt.text = deaths;
timer_txt.text = time;
This will make them show up on the screen.
By the looks of it, it doesn't seem like you're familiar with variables, but they're nothing but containers which contain values. You can control them "behind the scenes", meaning that you can calculate, store and change them in Flash without them appearing on the screen. This is useful since you don't want the death count and count up to actually be shown during gameplay, right?
If you need more clarficiation, please don't hesitate to ask.
Hope this helps :)

Decimal in slider value

I have a button that I press and a loop does:
NSLog(#"on");
sleep(slider.value);
NSLog(#"off");
sleep(slider.value);
However if I want to have the slider somewhere between 1 and 0 it either sleeps 1 second or not at all, how do I make it sleep half a second?
sleep() takes in seconds. You want to do sub seconds, so try using usleep(), which will sleep in milliseconds (.001 of a second). You may need to import unistd.h
So what you'd want to do is something like this:
usleep((int)(slider.value * 1000.0));

Resources