Touch is moved" on image ,score goes up and when "Touch is moved" out of image,score

Discussion in 'iOS Programming' started by pasha12345, Aug 27, 2013.

  1. pasha12345, Aug 27, 2013
    Last edited by a moderator: Aug 27, 2013

    pasha12345 macrumors newbie

    Joined:
    Aug 27, 2011
    #1
    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;
    }
     
  2. Duncan C, Aug 27, 2013
    Last edited by a moderator: Aug 27, 2013

    Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #2


    "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.
     
  3. xArtx macrumors 6502a

    Joined:
    Mar 30, 2012
    #3
    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 :)

     
  4. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #4
    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!
     
  5. xArtx macrumors 6502a

    Joined:
    Mar 30, 2012
    #5
    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
    	}}
    
    }
    
    
     
  6. dantastic macrumors 6502a

    dantastic

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

    Joined:
    Mar 30, 2012
    #7
    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.
     

Share This Page