Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pasha12345

macrumors newbie
Original poster
Aug 27, 2011
5
0
Hey!
I'm a new xcoder and I have a question.
I have image and score(label)
So i want when "Touch is moved" on image ,score goes up and when "Touch is moved" out of image,score dont go up.
I cant do that.
Please help!


CODE:

Code:
@interface image : UIImageView{
    NSInteger score;
    IBOutlet UILabel*label;}
    
     
@end
-------------------------------------------------------------------------------


Code:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
    score = score + 1;
    NSString *text = [NSString stringWithFormat:@"%d",score];
        self->label.text = text;
}
 
Last edited by a moderator:

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
Hey!
I'm a new xcoder and I have a question.
I have image and score(label)
So i want when "Touch is moved" on image ,score goes up and when "Touch is moved" out of image,score dont go up.
I cant do that.
Please help!


CODE:

Code:
@interface image : UIImageView{
    NSInteger score;
    IBOutlet UILabel*label;}
    
     
@end
-------------------------------------------------------------------------------


Code:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
    score = score + 1;
    NSString *text = [NSString stringWithFormat:@"%d",score];
        self->label.text = text;
}



"touch is moved?" Oh, you mean the touchesMoved method.

You would be better off not using that method. Create a gesture recognizer (probably at UIPanGestureRecognizer, if you're trying to recognize drag gestures) and attach it to your image. Then have the drag gesture on the image invoke a method that increases your score.

P.S. There are 2 sample applications linked in the docs for UIPanGestureRecognizer in the section "related sample code". Take a look at those for a working example that uses a UIPanGestureRecognizer.
 
Last edited by a moderator:

xArtx

macrumors 6502a
Mar 30, 2012
764
1
"Touch is moved" on image ,score goes up and when "Touch is moved" out of image,score dont go up.
You don't have to tell the forum or computer what not to do.
It already doesn't do everything in the Universe all by itself :)

"Touch is moved" on image ,score goes up.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
You don't have to tell the forum or computer what not to do.
It already doesn't do everything in the Universe all by itself :)

You mean all those tens of thousands of dollars we've paid contractors to write "do not explode" code for our apps was unnecessary? Oh snap!
 

xArtx

macrumors 6502a
Mar 30, 2012
764
1
You mean all those tens of thousands of dollars we've paid contractors to write "do not explode" code for our apps was unnecessary? Oh snap!

Yes, and the code not to kill the last remaining Elephants was also bogus.
"Touch is moved" on image ,score goes up and when "Touch is moved" out of image,score dont go up.

if (Touch is moved) {if (on image) {score goes up} else {score don't go up}}

if (Touch is moved) {if (on image) {score goes up}}

Code:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { // if touch is moved
    [self coordsForTouch:[touches anyObject]];
}

- (void)coordsForTouch:(UITouch *)touch { // then get the current touch point
    CGPoint tp = [touch locationInView:self.view];

	// if touch is on image…
	if (tp.x < imageXposition + imgwidth && tp.x > imageXposition) { // check x coords
	if (tp.y < imageYposition + imgheight && tp.y > imageYposition) { // check y coords
	score++; // score goes up
	}}

}
 

dantastic

macrumors 6502a
Jan 21, 2011
572
678
I have method in my toolkit to keep tigers away though. Works brilliantly!
Would not dream of releasing an app without it!:rolleyes:
 

xArtx

macrumors 6502a
Mar 30, 2012
764
1
I've never done the sort of thing to test if touches are sampled fast
enough for collision detection with moving sprites this way.

If it is that fast, you'd also have to check the touch moved outside the sprites
to prevent the score incrementing multiple times for one swipe across the
same sprite because touches moved could be called multiple times while swiping over the same sprite...

Is it that fast?

If not, and the sprite is small, or moving fast,
I suspect the movement might not be caught even the first time.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.