Using a button and a timer, have an annoying bug - macos

I'm using a button to activate a timer. In that button's code, it turns on the timer. I'll attach the code, though I doubt it'll make sense out of context.
- (IBAction)btnStartWasClicked:(NSButton *)sender {
if ([btnStartTitle.title isEqualToString:#"Start"])
{
NSLog(#"Debug: Start button was clicked."); //Debug
//Changes the Start button to say Stop
[btnStartTitle setTitle:#"Stop"];
started = TRUE;
[self tick:nil];
NSLog(#"Debug: Timer has started."); //Debug
[btnReset setEnabled:FALSE];
[btnLap setEnabled:TRUE];
if (lapTimerIsPaused == TRUE)
{
lapTimerIsRunning = TRUE;
[self tickAfterLap:nil];
}
}
else
{
NSLog(#"Debug: Stop button was clicked."); //Debug
//Changes the Stop button to say Start
[btnStartTitle setTitle:#"Start"];
started = FALSE;
NSLog(#"Timer has been stopped."); //Debug
[btnReset setEnabled:TRUE];
lapTimerIsRunning = FALSE;
lapTimerIsPaused = TRUE;
[btnLap setEnabled:NO];
}
I currently have a nasty bug I'm not quite sure how to get rid of. If the user double-clicks the start button really quickly, it will either:
A) Do nothing, just change the start button to say stop.
B) Make the timer count twice as fast (if the double-click is REALLY quick.) THIS ONLY HAPPENS WHEN THE START BUTTON IS SET TO SAY STOP, NOT START.
My bug is B. I'm not quite sure how to squash it.
Edit: The lap timer is a different timer than the regular timer, and I don't think the bug I'm having affects it, so ignore the lap timer code.
Any ideas?

Related

My animation is lagging, why?

I have an animation in unity, and basically it shows Donald Trump running:
I also have this one frame animation of Trump jumping:
Basically, when he jumps, the jump animation plays, and when he lands, the walk animation plays again.
This all works, and this code runs it:
function Update() {
trump.velocity = Vector2(speed, trump.velocity.y);
if (jump > 0) {
jumpBool = true;
}
else {
jumpBool = false;
}
animator.SetBool("Jump", jumpBool);
That's in the physics script. Then from the animator:
This all works, and the animations change when they are supposed to. The issue is, it lags before it finishes. I think that when Trump jumps, the walk animation finishes before it switches to the jump animation. My question is, how do I automatically switch animations immediately, so it won't look so laggy?
You can immediately call the Jump animation to be played at the point you make the JumpBool = true. Doing so you don't need to wait for the walk animation finish, it will simply stop the Walk and and move to Jump.
function Update() {
trump.velocity = Vector2(speed, trump.velocity.y);
if (jump > 0) {
animator.Play("Trump Jump");
//jumpBool = true;
}
else {
//jumpBool = false;
}
//animator.SetBool("Jump", jumpBool);
You don't even need to Set the bool, after the Jump animation has finished it will move back to the Walk animation.

How to make After Effects UI button to respond immediately while the script is processing

When I run this code in ExtendScript Toolkit (Target app - After Effects CC 2015) the cancel button is not responding until the progress bar has finished.
I would like to make it work so that the cancel button would respond immediately.
I know that pressing the "Esc" key works immediately, so it should be possible to make the cancel button work as well.
testWindow = new Window ("palette", "Processing:", undefined);
pb = testWindow.add("progressBar",undefined, 0);
cancelButton = testWindow.add("button", undefined, "Cancel");
testWindow.show();
cancelButton.onClick = function(){
alert("Cancel");
}
i = 1;
while (i <= 1000000) {
pb.value = Math.round(100*i/1000000);
testWindow.update();
i++
}
I think it has always been so: while a script is running, the user interface is on hold and non responding. Adding a Cancel button somewhere cannot work.
The only way i know for a user to cancel the execution of script is the ESC key.

Double tap recognition takes too long? (Hammer.js 2.0)

I am programming a highly responsive web application and came across the issue that most time is used to recognize double taps.
I am using this code from the website:
var singleTap = new Hammer.Tap({ event: 'singletap' });
var doubleTap = new Hammer.Tap({event: 'doubletap', taps: 2 });
hammer.add([doubleTap, singleTap]);
doubleTap.recognizeWith(singleTap);
singleTap.requireFailure(doubleTap);
This basically works quite fine. However, due to the timeouts/intervals the recognition of a double tap takes quite "long". I guess its about 2 times the interval - one for each tap.
The waiting for the last interval (waiting for a third tap) is senseless in my scenario.
Is there any "ok tapCount == 2, we fire now and don't wait any longer"-TapRecognizer option?
Update, I have done some logging:
First column: passed ms since first event
0 input: mousedown
74ms input: mouseup
145ms input: mousedown
218ms input: mouseup
520ms double tap
-
0 input: mousedown
64ms input: mouseup
366ms single tap
This confirms my theory that double tap is waiting for a third click but I don't think there's an option to disable this.
I share my solution to the problem:
I copied the TapRecognizer and named it DblTapRecognizer. The interesting source code lines are:
if (tapCount === 0) {
// no failing requirements, immediately trigger the tap event
// or wait as long as the multitap interval to trigger
if (!this.hasRequireFailures()) {
return STATE_RECOGNIZED;
} else {
this._timer = setTimeoutContext(function() {
this.state = STATE_RECOGNIZED;
this.tryEmit();
}, options.interval, this);
return STATE_BEGAN;
}
}
"if (!this.hasRequireFailures())" seems to misbehave in my situation, since the comment hints at immediate firing... So just "return STATE_RECOGNIZED;" and delete the rest for the DblTapRecognizer.
We ran into similar slowness issues. Apparently there is an inherent lag on tap action on touch devices.
We ended up using FastClick
All you need to do is FastClick.attach(document.body);
This improved the "tap performance" for us.

XEvent Handling in animation

I'm having some understanding problem of event-handling with Xlib functions.
My question would be - how do i press a key during an animation, without disturbing the animation.
My setup so far, is that i have some animation in a while loop and want to achieve a KeyPress event which modifies a parameter.
It looks something like this
while(1){
XNextEvent(dis, &report);
switch (report.type) {
case KeyPress:
if (XLookupKeysym(&report.xkey, 0) == XK_space){
//...modify parameter a..//}}
//...Some animation where parameter a is used to modify animation...//}
Now, the problem is that i have to press the key consistently to get the animation on my screen, otherwise nothing appears. I've tried some multiple code-modifications, with KeyRelease etc. but i don't have clou, really.
Trivially said - i need to hook a key during animation without the XNextEvent process, waiting for any event. But without the XNextEvent statement in my code, conditional statements for KeyPress event checking aren't working.
I guess formally this would mean:
while(1){
if(report.type==KeyPress) {
if (XLookupKeysym(&report.xkey, 0) == XK_space){
//...modify parameter a..//}}
//...Some animation where parameter a is used to modify animation...//}
Use XPending() to check for XEvents before getting them with XNextEvent().
XPending() returns then number of events in the event queue so modify your loop:
while(1){
if (XPending(dis) > 0) {
XNextEvent(dis, &report);
switch (report.type) {
case KeyPress:
if (XLookupKeysym(&report.xkey, 0) == XK_space){
//...modify parameter a..//
}
}
}
//...Some animation where parameter a is used to modify animation...//
}

Lap timer in XNA 4.0?

Right, I've got a slight problem here, in which I've attempted to implement a lap timer.
In my protect override void update I've got this;
if ((IntersectPixels(destinationRedRect, car2redTextureData, startingLineRectangle, startingLineTextureData)))
{
{
redHit = true;
_timer1 += gameTime.ElapsedGameTime.TotalMilliseconds;
}
}
What I'm saying here^ is, if car2red is colliding with the starting line, the timer begins, but if it's not, timer does not add seconds (it just stops_ . How can I make it so, if car2red hits the startingLine and moves forward a few pixels (without touching starting line) the timer still continues?
Thank you.
You should have a separate if statement like this:
if (redHit) {
_timer1 += gameTime.ElapsedGameTime.TotalMilliseconds;
}
if ((IntersectPixels(destinationRedRect, car2redTextureData, startingLineRectangle, startingLineTextureData)))
{
redHit = true;
//Only use this line if you want to reset the timer to 0 when the player crosses that line again.
_timer1 = 0;// I'm assuming that _timer1 is a double
}

Resources