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));
Related
I have a function that I want to run x percent of the time. example: when I tap a button, I want to run a function x percent of the time(the user enters 0.01, the function will run 1% of the time the button is tapped). Does anyone know how to do this in swift?
You can use a random function to determine whether a particular action should be undertaken:
func possiblyDoSomething(){
if Int.random(1...100) == 1 {
actuallyDoSomething() // will execute with a 0.01 chance
}
}
Int.random(1...100) will choose a random number in the range from 1 to 100, so any one number has a chance of 0.01 to appear, so it doesn't matter what number you equate it to (I chose to check that it equals to 1 arbitrarily)
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!
Question: The update(currentTime:) function in SKScene is called every frame with a currentTime argument. I'm wondering how to get the time from the same source update(currentTime:) uses without using the update function. I only found CFAbsoluteTimeGetCurrent() which is different.
Reason: To calculate timeSinceLastUpdate you need an instance variable called timeOfLastUpdate. To implement timeOfLastUpdate you need to set it to an arbitrary value before the first update which makes the first calculation of timeSinceLastUpdate incorrect. You could have a simple if statement to detect this or use optionals but that is an unneeded branch which could be avoided if I just set timeOfLastUpdate in didMoveToView(view:). And that is what I'm trying to do.
The first timeSinceLastUpdate will always be different from the rest. Typically one stores the absolute time from the previous update call and subtracts it from the current call's time to get a timeSinceLastUpdate. If the previous time doesn't exist because you're on the first pass through the render loop — it's set to zero or infinity or negative something or whatever sentinel value you initialized it to, just set your timeSinceLastUpdate to some nominal value for getting started. Something like your frame interval (16.67 ms if you're going for 60 fps) would make sense — you don't want game code that depends on that time interval to do something wild because you passed it zero or some wildly huge value.
In fact, it's not a bad idea to have logic for normalizing your timeSinceLastUpdate to something sane in the event that it gets foo large — say, because your user paused and resumed the game. If you have game entities (such as GameplayKit agents) whose movement follows some sort of position += velocity * timeSinceLastUpdate model, you want them to move by one frame's worth of time when you resume from a pause, not take a five-minute pause times velocity and jump to hyperspace.
If you initialize your previous time to zero, subtract, and normalize to your expected frame interval, you'll cover both the starting and unpausing cases with the same code.
You could always use coalesce:
let deltaTime = currentTime - (previousTime ?? currentTime).
On first loop, your deltaTime is 0 (Should be this) after that, it is the change
Of course previousTime must be an optional for this to work
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.
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 :)