Jerk or throw motion detection with iphone - iphone

I am making a stone throw app that shows a rock skipping over water. What I want do to is detect a motion when the hand moves quickly from side to the front (like as if you were skipping rocks). I tried the following code and this is the results I get in my log. Is there a gesture detection that I can use?
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (event.subtype == UIEventSubtypeEndSeekingForward)
{
NSLog(#" Forward event ...");
}
else
{
NSLog(#" Something moved but I dont know what event ...");
}
if ([super respondsToSelector:#selector(motionEnded:withEvent:)])
[super motionEnded:motion withEvent:event];
}
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(void) viewWillAppear:(BOOL)animated
{
[self becomeFirstResponder];
[super viewWillAppear:animated];
}
- (void) viewWillDisappear:(BOOL)animated
{
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
This is what I get when I quickly move my iPhone back and forth (happens both ways)
Something moved but I dont know what event ...
Something moved but I dont know what event ...
Something moved but I dont know what event ...
Also, I had to put a lot of force to make the detection work. So two questions
Is there some built in threshold number that I can override?
And how can I tell it to do this action only when phone is moved forward?
Thanks

If you see the class reference for UIEvent, you'll realize that the only motion related event subtype is UIEventSubtypeMotionShake, which means you can only know, from that event, that the device was shaken.
To achieve what you want to achieve, you would have to use accelerometer values directly.
This means that, first, you would have to do some tests on what values the accelerometer returns when you are doing that specific movement, and on different steps of the movement. You also have to get the range of values in which the movement still makes sense.
Then, you will need to integrate those values into your app, basically, what you'd have to do, is, in your accelerometer delegate, to check that, in every step of the movement, the values the accelerometer is giving you are inside the ranges that you tested make sense.
It's not gonna be a walk in the park, be advised.

Related

iPhone app increase shake gesture sensitivity

In my iPhone App I have used shake gesture it is working but it requires lots of efforts to shake.
Is there any way to make it more sensitive?
Here is the code
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake)
{
[self performSelector:#selector(startPressed:)];
}
}
The isnt a way to do it directly as far as I'm aware.
One way to get around this would be to use the accelerometer's readings, and then decide for yourself if it was a shake, you would be able to fine tune it yourself if so.

Why shake doesn't work when the screen is off on iPhone?

I am playing an audio file in the app, so it doesn't turn off when the phone is locked and the screen is off.
However when I want detect shake it does not work.
It works fine when the app is open and when the screen is locked (not off). The app is definitely running because the logs are working fine.
Any idea?
I use the following code:
-(BOOL)canBecomeFirstResponder {
//make it respond to shake events
return YES;
}
- (void)viewDidAppear:(BOOL)animated {
//make it respond to shake events
[self becomeFirstResponder];
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventSubtypeMotionShake) {
NSLog(#"Shake detected");
}
}
Monitoring the accelerometer causes extra power consumption by the phone, so it is disabled while the phone is locked. I don't believe there is a way around this.
This would help you.
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
This wont let the screen turn off.
Hope this helps.
EDIT:
But Even I feel the same as Akshay does. That it is not possible to capture UIEvent when screen is off.
I don't think you can capture UIEvents when the screen is off.

Creating a "developer screen" for game development in cocos2d

I'm currently developing a game on iPhone using the Cocos2D API. It's going well. One issue I'm having though is that I have to recompile each time I want to change my variables. This is very tedious, especially now that I am tweaking gameplay.
Is there any implementation for a sort of developer console screen? What I mean is: I want to have a type of game screen that I load, that contains a list of variables that I register to the game screen(with scroller). And I want to be able to modify these variables on the spot.
I remember there being a presentation on a WWDC event in which they showed such a screen on an ipad. The developer would just press a button and the gamescreen would change to a developer console like screen. I know this presentation had nothing to do with Cocos2D, but still, if this already exists in some shape or form, I would love to re-use this code instead of writing it on my own.
Though if I had to write it on my own, I wouldn't really know where to start. So any help there would be appreciated as well.
Thx!
It was (I believe) Graeme Devine at Apple's WWDC last year who had some suggestions on how to implement such a developer console (check the video on iTunes University). An example called Game Console is included with the example code of WWDC 2010 (232 MB). I've also added a link (57 kb) to GameConsole.zip from DropBox, for convenience.
This is a seriously backdated reply but we implemented a developer console for Mega Run to test out various stages and modify player properties at run time. Implementation was to tap in the top left corner of the screen at any point in the game to bring up the console. From there you could modify to your needs. The skeleton of the implementation was to override EAGLView and handle the touchesBegan touch callback yourself. Here is the implementation...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
const CGFloat DEV_DASHBOARD_ENABLE_TOUCH_AREA = 20.0f;
for (UITouch* t in touches)
{
CGPoint pt = [t locationInView:self];
if (pt.x < DEV_DASHBOARD_ENABLE_TOUCH_AREA && pt.y < DEV_DASHBOARD_ENABLE_TOUCH_AREA)
{
ToolSelectorContainer* editorViewController = [[ToolSelectorContainer alloc] initWithNibName:#"ToolSelectorContainer" bundle:nil];
if (editorViewController != nil)
{
CCScene* g = [CCDirector sharedDirector].runningScene;
// Pause the game if we're in it playing
//
if ([g isKindOfClass:[Game class]])
[((Game *)g) menuPause];
[[GSGCocos2d sharedInstance].navigationController pushViewController:editorViewController animated:YES];
[editorViewController release];
break;
}
}
}
#endif
[super touchesBegan:touches withEvent:event];
}
ifdef is used to not compile this code for production builds.

Is there a way to fake a real touch into iPhone OS?

I need to fake a real touch event programmatically, so that any subsequent touch (from the users finger, for example) will immediately result into a -touchesMoved.... message rather than -touchesBegan....
Any solution how to do that? I know Three20, but I need something legal.
I'm not sure if I got you right, but if you want touchesBegan act like touchesMoved why don't do like:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if(specialModeOn)//I don't know if you need this
{
[self touchesMoved:touches withEvent:event];
}
else
{
//whatever touchesBegan should do normally
}
}
Is it not viable to simply set a flag to interpret the next touchesBegan event as a touchesMoved event instead, and have your own code handle figuring out what the latter event would look like?

Why is detecting touches getting slower and slower?

In my game if I play a particular game for several times, my touches need more time to be detected.
It stores all touches and then applies those touches all at the same time.
Can anybody tell me what's the problem?
In touchesBegan I wrote:
if (CGRectContainsPoint([tapView frame], [touch locationInView:self])
&& tapView.alpha == 1) {
[self callTapCode];
}
This is the code of touchesEnded. If I tapped and release the tapped it shows one tapping event.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (checkTap == TRUE && tapView.alpha == 1 )
tap_effect_view.alpha = 0;
}
- (void)callTapCode {
// Move player code by 6 pixels
// not possible to write all code
}
In tapView I continuously tap. callTapCode moves the player by six pixels. But after some time my touches detected very slowly, so that the player looks like he's jumping around. I played the game continuously 15 to 16 times.
You might work through this tutorial to learn how to use the Leaks Instrument. This is part of the Instruments suite that comes with Xcode, which will, among other things, help you track down memory leaks and general performance issues with your application.
I found the solution to my problem. In my game I had enabled the tapView.multipleTouchEnabled = TRUE
tapView is the view where I was continuously tapping.
When I make it FALSE it works.
i.e.
tapView.multipleTouchEnabled = FALSE;
I exactly dont know how. But it works.
Thanks for the replies.
Try to look for any memory leaks. Maybe the iPhone has to use virtual memory a lot.