Corona libraries - mobile

I want to add buttons in which untill i stop pressing them it will continuously do its functionality.
For example, in the Mario game once we start pressing forward button it will continue move Mario until we leave that button, we don't have to press again and again to move.

The above would run at all times because of the enterFrame listener what you are looking for would be more like this....
local function moveLeft(event)
if event.phase=="began" then
character.x=character.x+1
elseif event.phase="ended" then
--do Nothing it wont move anymore anyways
end
end
local leftbutton=display.newImage("bla bla bla.png")
leftButton:addEventListener("touch",moveLeft)
When you use the touch event whatever you tell it to do doesn't stop until you release and it's different from tap because in tap you have to release at a pretty fast rate, and the event only registers when you release.

I assume your question is "how do I make a button that acts continuously until release?" First add a listener for the "touch" event.
Touch events have several phases, for the beginning and end of the touch. So in the listener function use an if/else to respond to the different phases.
if event.phase=="began" then
Runtime.addEventListener("enterFrame", doSomething)
elseif event.phase=="ended" then
Runtime.removeEventListener("enterFrame", doSomething)
Now Move Mario in the doSomething function.

Related

Closing a JFrame without it still running

Titling this was awkward, but basically I'm trying to close a JFrame that allows the user to change their password. I know to use the dispose() to close it, but I have a delay timer so the user can see feedback that their password was changed before closing the JFrame. In my initial testing, I forgot to end the delay timer, so when it opened the programs main frame, it continued to open more and more of the program's main frame until I close the program. I then obviously went in and stopped the timer.
This has told me something that kinda worries me, that when disposing the frame, it's still running code. This worries me, it makes me worry that there's a chance that the user could break something or the program could begin to lag with long use if code is still running in the background unnecessarily. Is there a way to actually end the code of the JFrame when its disposed, or is stopping the timer itself the only answer here?
In this case I'm assuming that stopping the timer will stop any background code from running, but if the frame is still active, it makes me worried about the possibilities with other frames that have more interactivity that are being closed and reopened.
EDIT:
I should have explained better. For the actual frame's closing option, I have it as this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); so that the user can't close it without saving their new password or cancelling the password change.
In the save button function, I have:
delayTimer.start();
And for the actual timer:
if (e.getSource() == delayTimer)
{
new MainFrame();
this.dispose();
delayTimer.stop();
}

Timed functions in CVI GUI

I'm working on an application in Windows CVI that needs to run some code for a series of time intervals set by the user via text entry boxes. The boxes include three for how long to run each process, one to show the total time the processes will take, and one to show the time remaining.
My implementation currently is to have a function with static variables to track which process is running and how long is left in the current process, then move on when that time has elapsed. This function triggers on pushing a start button. Unfortunately, the code stops on the click of the start button as it seems to be waiting for the code to finish executing before it allows any further inputs.
Is there a "right" way to do this? Maybe something with multithreading or a pre-built timer application?
Got an answer here: "You are on the right way speaking about timers: place a timer control on your panel, set it to disabled, put your code in the timer callback and run the program. When the user inputs the required time, set the ATTR_INTERVAL attribute of the timer to this value, next enable the timer with ATRT_ENABLED attribute and you'll have your timed function up and running!"

Simulate a mouse click with IOKit

Backstory:
I want to write a C program to automate clicks in a program running in OSx (in a desktop setting).
I first tried Using Quartz Event Services to simulate input events. But then I had this problem: Simulating mouse clicks on Mac OS X does not work for some applications, and the answers didn't help in my case.
CGEventRef click1_down = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, CGPointMake(posx, posy), kCGMouseButtonLeft);
CGEventSetIntegerValueField(click1_down, kCGMouseEventClickState, 0);
// This down click works about 5% of the time.
CGEventPost(kCGHIDEventTap, click1_down);
usleep(30000);
CGEventRef click1_up = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseUp, CGPointMake(posx, posy), kCGMouseButtonLeft);
CGEventSetIntegerValueField(click1_up, kCGMouseEventClickState, 1);
CGEventPost(kCGHIDEventTap, click1_up);
// I've tried every combination of CGEventSetIntegerValueField, usleep and CFRelease, nothing seems to help
// The only thing helping is repeating the line: "CGEventPost(kCGHIDEventTap, click1_down);" 100s of times,
// then the down click works about 80% of the time, still not acceptable
I'm now turning to solution #3 suggested here: How can Mac OS X games receive low-level keyboard input events?
(this might also help How can I simulate the touch events by IOHIDEvent?)
I tried with Karabiner by sending a mouse click on key press:
<item>
<name>Right Mousebutton</name>
<identifier>rightMouseButton</identifier>
<autogen>__KeyToKey__ KeyCode::H, PointingButton::LEFT</autogen>
</item>
And this sends the click 100% of the time, but I want to send the click with by writing C code (to have greater control). Tough I'm not sure, Karabiner seems to use IOKit to send events, so I think this should work in my case, if I'm able to send mouse events with IOKit.
So my question is basically: how do I write a C program to simulate a mouse left click with IOKit ? The documentation is very sparse and I didn't manage to do it.
I tried getting inspiration from some projects:
https://github.com/tekezo/Karabiner
https://github.com/NoobsArePeople2/manymouse

NStimer multiplies each time I reload view

I have a one viewController which serves for 2 screens (main and settings)
in the view did load I have a timer that repeats unlimited times (checking condition)!
when I load setting screen and go back timer starts again but old timer works as well so after few load-unloads I have a lot of timers that checks same conditions (this makes my app to slow down).
I tried to use "invalidate" but it seams that my timer does not see it.(I put invalidate before I invoke timer and nullified it as well but without result)...
As you mentioned you are using the same view controller for main and setting screen,both the times either main or settings
Solution is you initialise this timer in appdelegate and on switching the tabs main and settings first you invalidate it and after viewdidappear you start it again.
call the invalidate timer in viewWillDisappear function
then it will remove the timer instance before it start the new reference.

Update loop in windows forms?

I've been working with some game engines lately. They all have an Update() function that gets called every frame (not a loop in the strict sense of the word, but you get the idea). Any code you want to execute needs to be placed in here.
This made me wonder, how does this work in windows forms, as the only thing I use there is events?
(If the title doesn't explain it good enough, feel free to change it)
It basically works the same, it is only called the message loop, event loop or message pump
http://msdn.microsoft.com/en-us/library/aa383738.aspx

Resources