01-13-2010, 02:07 AM
#1 (permalink )
Registered Member
Join Date: Dec 2009
Posts: 471
Game, touch and hold of button
hey! in a quick game that ive been testing with im trying to implement a left button and a right button that move an imageview, left or right. my problem is, i want to make it so if you hold down on one of the buttons it will continue to call the action, so that you don't have to click repeatedly.
in another post, i found this hold was found using
Code:
-(void)movePlayer:(id)sender {
backgroundImage.frame = CGRectMake(backgroundImage.frame.origin.x-1, guyImage.frame.origin.y, backgroundImage.frame.size.width, backgroundImage.frame.size.height);
}
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
timer = [NSTimer schduleTimerWithTimeInterval:0.3 target:self selector:@selector(movePlayer:) repeats:YES userInfo:nil];
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
if (timer != nil)
[timer invalidate];
timer = nil;
}
-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
if (timer != nil) {
[timer invaidate];
timer = nil;
}
}
which all make sense to me, but how do i use that for both the left and the right buttons, to do two different things?
as far as i see in this code, it doesnt even check where the click was, how to i make it so it happens on the click of a button, and changes depends on what button was clicked.
Thanks! Cheers, and i hope that makes sense!
__________________
01010111 01101111 01110111 00101100 00100000 01001010 01110101 01110011 01110100 01101001 01101100 01101100 00110100 00110101 00101100 00100000 01111001 01101111 01110101 01110010 00100000 01110011 01101111 01101111 01101111 00100000 01100011 01101111 01101111 01101111 01101100
01-13-2010, 03:22 AM
#2 (permalink )
Nuisance Developer
Join Date: Jul 2009
Location: Italy
Posts: 4,691
if the click is on the view, just check the x value,
if x>resolution/2 = right else = left
or you can insert 2 uimages subclassing it from uiimage and implement touchesBegan touchesMoved
01-13-2010, 09:17 AM
#3 (permalink )
Registered Member
Join Date: Dec 2009
Posts: 471
O.o why didn't I think of that. How do I check the clicks x axis? And how do I get the resoultion? Also if I'm not letting the person rotate at all, couldn't I just check the if the x coordinate of the touch is > 240 (or half the screen in landscape?
__________________
01010111 01101111 01110111 00101100 00100000 01001010 01110101 01110011 01110100 01101001 01101100 01101100 00110100 00110101 00101100 00100000 01111001 01101111 01110101 01110010 00100000 01110011 01101111 01101111 01101111 00100000 01100011 01101111 01101111 01101111 01101100
01-13-2010, 09:00 PM
#4 (permalink )
Registered Member
Join Date: Dec 2009
Posts: 471
Is there a way to register a hold like this using uibuttons, in my game it is quite neccisary to use to a uibuttons.
Instead of doing:
Code:
-(void)movePlayer:(id)sender {
backgroundImage.frame = CGRectMake(backgroundImage.frame.origin.x-1, guyImage.frame.origin.y, backgroundImage.frame.size.width, backgroundImage.frame.size.height);
}
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
timer = [NSTimer schduleTimerWithTimeInterval:0.3 target:self selector:@selector(movePlayer:) repeats:YES userInfo:nil];
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
if (timer != nil)
[timer invalidate];
timer = nil;
}
-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
if (timer != nil) {
[timer invaidate];
timer = nil;
}
}
thanks
__________________
01010111 01101111 01110111 00101100 00100000 01001010 01110101 01110011 01110100 01101001 01101100 01101100 00110100 00110101 00101100 00100000 01111001 01101111 01110101 01110010 00100000 01110011 01101111 01101111 01101111 00100000 01100011 01101111 01101111 01101111 01101100
01-14-2010, 12:55 AM
#5 (permalink )
Registered Member
Join Date: Dec 2009
Posts: 471
YES!
So i figured it out:
Code:
-(void)movePlayerRight:(id)sender {
backgroundImage.frame = CGRectMake(backgroundImage.frame.origin.x-1, 0, 2000, 320);
[self checkFallOnLeft];
}
-(void)movePlayerLeft:(id)sender {
backgroundImage.frame = CGRectMake(backgroundImage.frame.origin.x+1, 0, 2000, 320);
[self checkFallOnLeft];
}
-(IBAction)RightDown {
guyImage.image = [UIImage imageNamed:@"guyright.png"];
backgroundImage.frame = CGRectMake(backgroundImage.frame.origin.x-1, 0, 2000, 320);
timerRight = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(movePlayerRight:) userInfo:nil repeats:YES];
}
-(IBAction)LeftDown {
guyImage.image = [UIImage imageNamed:@"guyleft.png"];
backgroundImage.frame = CGRectMake(backgroundImage.frame.origin.x+1, 0, 2000, 320);
timerLeft = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(movePlayerLeft:) userInfo:nil repeats:YES];
}
-(IBAction)RightUp {
backgroundImage.frame = CGRectMake(backgroundImage.frame.origin.x, 0, 2000, 320);
if (timerRight != nil) {
[timerRight invalidate];
timerRight = nil;
}
}
-(IBAction)LeftUp {
backgroundImage.frame = CGRectMake(backgroundImage.frame.origin.x, 0, 2000, 320);
if (timerLeft != nil) {
[timerLeft invalidate];
timerLeft = nil;
}
}
-(void)checkFallOnLeft {
if (backgroundImage.frame.origin.x > 0) {
backgroundImage.frame = CGRectMake(0, 0, 2000, 320);
}
}
the only issue i have now is the guy moves so slowly, is there any way i can speed it up?
fixed that issue by changing the -1 to -5 thanks guys!
__________________
01010111 01101111 01110111 00101100 00100000 01001010 01110101 01110011 01110100 01101001 01101100 01101100 00110100 00110101 00101100 00100000 01111001 01101111 01110101 01110010 00100000 01110011 01101111 01101111 01101111 00100000 01100011 01101111 01101111 01101111 01101100
Last edited by WillSDev; 01-14-2010 at 12:59 AM .
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 332
12 members and 320 guests
2Apps1Day , akacaj , Domele , GraffitiCircus , michelle , NetGuru , NSString , Paul Slocum , SLIC , Sloshmonster , soohyun , v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,114
Posts: 402,886
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun