Increasing Velocity - objective-c

Hey everyone I am making an app where you're a blue ball and have to move without letting the red ball touch you and I was wondering what could I do so that every second the velocity increases I have something like this:
- (void)viewDidLoad {
[super viewDidLoad];
//(X Speed, Y Speed)
pos = CGPointMake(13.0,8.0);
pos2 = CGPointMake(10.0,9.0);
}
- (IBAction)restart:(id)sender {
[restartbutton setHidden:YES];
[menubutton setHidden:YES];
randomMain = [NSTimer scheduledTimerWithTimeInterval:(0.03) target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
}
- (IBAction)start:(id)sender {
[startbutton setHidden:YES];
[restartbutton setHidden:YES];
[menubutton setHidden:YES];
randomMain = [NSTimer scheduledTimerWithTimeInterval:(0.03) target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
}
- (IBAction)menu:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)onTimer {
[self checkCollision];
enemy.center = CGPointMake(enemy.center.x+pos.x, enemy.center.y+pos.y);
enemy2.center = CGPointMake(enemy2.center.x+pos2.x, enemy2.center.y+pos2.y);
if (enemy.center.x > 320 || enemy.center.x < 0)
pos.x = -pos.x;
if (enemy.center.y > 480 || enemy.center.y < 0)
pos.y = -pos.y;
if (enemy2.center.x > 320 || enemy2.center.x < 0)
pos2.x = -pos2.x;
if (enemy2.center.y > 480 || enemy2.center.y < 0)
pos2.y = -pos2.y;
}
- (void)checkCollision {
if (CGRectIntersectsRect(player.frame, enemy.frame)) {
[randomMain invalidate];
[startbutton setHidden:YES];
[restartbutton setHidden:NO];
[menubutton setHidden:NO];
CGRect frame = [player frame];
frame.origin.x = 135.0f;
frame.origin.y = 205.0f;
[player setFrame:frame];
CGRect frame2 = [enemy frame];
frame2.origin.x = 135.0f;
frame2.origin.y = 20.0f;
[enemy setFrame:frame2];
CGRect frame3 = [enemy2 frame];
frame3.origin.x = 135.0f;
frame3.origin.y = 390.0f;
[enemy2 setFrame:frame3];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You Lost!"
message:[NSString stringWithFormat:#"Better Luck Next Time"]
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[alert show];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [[event allTouches] anyObject];
player.center = [myTouch locationInView:self.view];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
#end

It seems that the only method you've given us that is being constantly called is onTimer. Why don't you set an acceleration constant of something really small like 0.01. Then, create a method called accelerate and put in code to the effect of:
pos = CGPointMake(pos.x + ACC_CONSTANT, pos.y + ACC_CONSTANT);
pos2 = CGPointMake(pos2.x + ACC_CONSTANT, pos.y + ACC_CONSTANT);
Call accelerate after [self checkCollision];
Then play around with the Acceleration constant until you get the results you want.
If you want it to be a step function where it only increases every second, you will need to use an NSTimer iVar, which would also be fairly easy. Just instantiate it with a one second time interval using this init function
scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
Then call #selector(accelerate) as the target selector.

Related

Detecting position tapped with UITapGestureRecognizer

Hi so I was wondering if there was any way possible to get the position that was touched using UITapGestureRecognizer to recognize taps on the background.
-(void)handleTapGesture:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateRecognized)
{
if ( bomb == nil)
{
bomb = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bomb.png"]];
bomb.center = CGPointMake(10, 1);
bomb.frame = CGRectMake(113,123,67,67);
[self.view addSubview:bomb];
//[self performSelector:#selector(Update) withObject:nil afterDelay:1];
NSLog(#"Spawned bomb");
timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:#selector(dropBomb) userInfo:nil repeats:YES];
}
}
}
Sure there is! Use locationInView: to get the CGPoint location of the touch on your image view.
CGPoint touchLocation = [sender locationInView:sender.view];
Or, if you are allowing multiple touches, you can use the following to specify which touch you are interested in.
CGPoint otherTouchLocation = [sender locationOfTouch:0 inView:sender.view];

IOS - how do I reset a number label to zero when a button is pressed?

How would I reset a label back to zero that counts up every time the user taps an image?
The current method that I have for it does not work.
Here is the code that I believe you will need:
int number = 0;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if ([self Intersecting:location :Ball]) {
number++;
[labelCurrentNumber setText:[NSString stringWithFormat:#"%d", number]];
}
}
-(IBAction)startMove {
buttonStart.hidden = YES;
Ball.hidden = NO;
labelEasy.hidden = YES;
buttonBack.hidden = YES;
Destination = CGPointMake(arc4random() % 320, arc4random() % 480);
xamt = ((Destination.x - Ball.center.x) / speed);
yamt = ((Destination.y - Ball.center.y) / speed);
mainTimer = [NSTimer scheduledTimerWithTimeInterval:(0.02) target:self selector:#selector(moveBall) userInfo:nil repeats: YES];
mainInt = 20.00;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(countDown) userInfo:nil repeats:YES];
}
Please help me out, and thank you in advance.
Just set an integer for the amount you want to change to zero and make the label display that integer. Then all you have to do is code "When the button is pressed, set integer name here to 0.
Instead of listening for touch events and calculating intersection, you might want to consider a UITapGestureRecognizer. Just add your set to zero method as an target and add the gesture recognizer to your image.

Moving an image after rotating does not behave correctly

I'm getting problem when I tried to move the after rotating the same image. My code is working fine when I tried to move the image before rotating it. After rotating the image, when I tried to move the rotated image, the image is not moving in the direction which I dragged. Can any one help me.
Thanks in advance. I'm using the code
#import "myDraggableImage.h"
#import <CoreGraphics/CoreGraphics.h>
#import <QuartzCore/CoreAnimation.h>
#implementation myDraggableImage
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
[[self superview] bringSubviewToFront:self];
CGPoint pt = [[touches anyObject] locationInView:self];
startLocation = pt;
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[self addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[self addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[self addGestureRecognizer:panRecognizer];
[panRecognizer release];
CGRect frame = [self frame];
printf("\n the x co ordinte of the frame is :%f",frame.origin.x);
printf("\n the y co ordinte of the frame is :%f",frame.origin.y);
printf("\n the widht co ordinte of the frame is :%f",frame.size.width);
printf("\n the height co ordinte of the frame is :%f",frame.size.height);
}
-(void)rotate:(id)sender
{
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
CGRect frame = [self frame];
}
-(void)scale:(id)sender
{
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
-(void)move:(id)sender
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
{
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{
CGFloat finalX = translatedPoint.x ;//+ (.15*[(UIPanGestureRecognizer*)sender velocityInView:self].x);
CGFloat finalY = translatedPoint.y ;//+ (.15*[(UIPanGestureRecognizer*)sender velocityInView:self].y);
if(finalX < 0)
{
finalX = 0;
}
else if(finalX > 460)
{
finalX = 440;
}
if(finalY < 40)
{
finalY = 40;
}
else if(finalY > 300)
{
finalY = 300;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
-(void)dealloc
{
[super dealloc];
}
#end
touchesBegan:withEvent: is a wrong place to set up your gestures as it will add a new gestures every time the user. As for the translation function, it's hard to put a finger on what's wrong. Some of the process here seems overdone. While I haven't implemented the animation part (to bring it in, right?), take a look at this sample project. It could help you get started in the right direction.

Objective C: SetNeedsDisplay

I've been reading this book on game programming, but the examples are not that great. I'm working on one that calls [self setNeedsDisplay] but it cause the app the crash.
This is all the code I have, copied verbatim from the book:
-(void)awakeFromNib {
//start timer that will fire every second
[car setAlpha:0];
[road setAlpha:0];
currentImage = [UIImage imageNamed:#"road.png"];
//start timer that fires every second
[NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
//start timer that fires every hundreth of a second
[NSTimer scheduledTimerWithTimeInterval:(0.01) target:self selector:#selector(onTimerRoad) userInfo:nil repeats:YES];
}
-(void)onTimerRoad
{
int tileIndex;
tileIndex += 1;
[self setNeedsDisplay];
}
-(void)onTimer {
[self update];
[self draw];
}
-(void)update {
[self updateRoad];
}
-(void)updateRoad {
[self randomRoadUpdate];
}
-(void)randomRoadUpdate {
int distance = (random() % 11) -5;
CGPoint oldPosition = road.center;
if (oldPosition.x + distance < 96 || oldPosition.x + distance > 224)
return;
road.center = CGPointMake(oldPosition.x + distance, oldPosition.y);
}
-(void)draw {
}
- (id) initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
//init code
}
return self;
}
-(void)drawRect:(CGRect)rect {
CGImageRef image = CGImageRetain(currentImage.CGImage);
CGRect imageRect;
imageRect.origin = CGPointMake(160, 240);
imageRect.size = CGSizeMake(320.0, 480.0);
CGContextRef uiContext = UIGraphicsGetCurrentContext();
CGContextClipToRect(uiContext, CGRectMake(0.0, 0.0, rect.size.width, rect.size.height));
CGContextDrawTiledImage(uiContext, imageRect, image);
}
Turns out instead of [self setNeedsDisplay] I need to do [self.view setNeedsDisplay]

Cocoa Touch - Looping Help

I need help with some programming logic... I need to loop this method to display the math problem in my labels then sleep(5) and then loop again. Anything I've tried ends of freezing the program. PLEASE help! I've been tearing my hair out trying everything I know!
EDIT: I edited the code to this, After 3 seconds it fades away the label displaying the problem but then it crashed and the debugger displays 2010-08-06 10:43:27.776 Reactor [13444:207] modifying layer that is being finalized - 0x5c13500
//quickfire problems
-(void)simpleMath{ //"Tap when the answer is X"
isTimeToTap = NO;
int problemSelector = arc4random() % 4;
NSArray *mathProblems = [[NSArray alloc] initWithObjects:#"6 - 1",#"2 + 3",#"3 x 2",#"3 x 1",#"2 x 4",nil]; //correct index 2
NSArray *mathAnswers = [[NSArray alloc] initWithObjects:#"5",#"5",#"6",#"3",#"8",nil]; //correct index 2
if ([mathAnswers objectAtIndex:problemSelector] == #"6") {
isTimeToTap = YES;
}
if (ranBefore == NO) { //create labels
//tell when to tap
tapTellerTop.text = #"Tap when the answer is 6!";
tapTellerBottom.text = #"Tap when the answer is 6!";
//make bottom label
mathDisplayBottom = [[UILabel alloc] initWithFrame:CGRectMake(15, 250, 242, 92)];
mathDisplayBottom.font = [UIFont fontWithName:#"Helvetica" size: 96.0];
mathDisplayBottom.textColor = [UIColor whiteColor];
mathDisplayBottom.backgroundColor = [UIColor clearColor];
[self.view addSubview: mathDisplayBottom];
//make top label
mathDisplayTop = [[UILabel alloc] initWithFrame:CGRectMake(55, 120, 242, 92)];
mathDisplayTop.font = [UIFont fontWithName:#"Helvetica" size: 96.0];
mathDisplayTop.textColor = [UIColor whiteColor];
mathDisplayTop.backgroundColor = [UIColor clearColor];
[self.view addSubview: mathDisplayTop];
//rotate top label
mathDisplayTop.transform = CGAffineTransformMakeRotation(180.0 /180.0 * M_PI);
}
//if ran before just update the text
mathDisplayBottom.text = [mathProblems objectAtIndex:problemSelector];
mathDisplayTop.text = [mathProblems objectAtIndex:problemSelector];
ranBefore = YES; //if its here. its been ran.
//run timer wait for (3) then loop again until userTaps = YES
[self performSelector:#selector(simpleMath) withObject:nil afterDelay:3.0f];
[mathProblems release];
[mathAnswers release];
[mathDisplayBottom release];
[mathDisplayTop release];
}
Sleep will stop your application from running or responding at all. Here is a quick partial example of a view controller you could use. This assumes you wire up the tap button and only uses one of the labels, etc. but you can play around with it. Also, this won't deal with memory issues or anything, so you'd add support for that.
But, once this view controller is created and the view installed, the math problem will update every 5 seconds. If the user presses the button and the answer is valid, we log a success message, otherwise we log fail message.
#interface myMathController : UIViewController {
NSArray* mathProblems;
NSIndexSet* validIndexes;
NSUInteger currentIndex;
NSTimer* timer;
}
#property(nonatomic,assign) IBOutlet UILabel* mathDisplayLabel;
- (void)updateProblem:(NSTimer*)timer;
- (IBAction)userTap;
#end
#implementation myMathController
- (void)viewDidLoad
{
[super viewDidLoad];
mathProblems = [[NSArray alloc] initWithObjects:#"6 - 1",#"2 + 3",#"3 x 2",#"3 x 1",#"2 x 4",nil];
// Add all valid answers
NSMutableIndexSet* tempIndexes = [[NSMutableIndexSet alloc] init];
[tempIndexes addIndex:2];
validIndexs = [tempIndexes copy];
[tempIndexes release];
timer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:5.0 target:self selector:#selector(updateProblem:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
- (void)updateProblem:(NSTimer*)timer
{
currentIndex = arc4random() % [mathProblems count];
[mathDisplayLabel setText:[mathProblems objectAtIndex:currentIndex]];
}
- (void)userTap
{
if( [validIndexes containsIndex:currentIndex] ) {
NSLog(#"This user is SMART!");
} else {
NSLog(#"This user needs more work!");
}
}
- (void)dealloc
{
[timer invalidate];
[timer release];
[mathProblems release];
[validIndexes release];
[super dealloc];
}
#end
You shouldn't sleep(), ever. To invoke a function repeatedly, call:
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(doStuff:)
userInfo:nil repeats:YES];
then define the function:
-(void)doStuff:(NSTimer*)timer
{
// stuff
if ( iAmDone ) [timer invalidate];
}
OR if you want to fire another call, after 5 seconds, you could call
if ( !iAmDone ) [self performSelector:#selector(simpleMath) afterDelay:5];
at the end of simpleMath.