Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

These are my ccTouch__ methods. Note that I am using the singular form of the functions (ie. ccTouchBegan v. ccTouchesBegan (don't know if that has anything to do with it, but it can't hurt to point it out)).

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
    //NSLog(@"1");

    CGPoint touchLocation = [GameScene locationFromTouch:touch];
    CTouch* newTouch = [[CTouch alloc] initWithStart:touchLocation];
    [touches addObject:newTouch];

    //Right now the UILayer takes all touch events and handles them
    return YES;
}

-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
    //NSLog(@"2");

    //Get the touch's location and previous location
    CGPoint touchLocation = [GameScene locationFromTouch:touch];
    CGPoint lastTouchLocation = [GameScene previousLocationFromTouch:touch];
    //Check which CTouch has been moved and move it
    for (CTouch* ct in touches){

        if ([ct checkSameTouch:lastTouchLocation]) {
            [ct setCurrentPosition:touchLocation];
        }
    }

}

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
    NSLog(@"3");

    //Get the touch's location
    CGPoint touchLocation = [GameScene locationFromTouch:touch];
    CGPoint lastTouchLocation = [GameScene previousLocationFromTouch:touch];
    //The CTouch object to be removed
    //You can't remove an object from the array while its being enumerated
    CTouch* removeTouch;

    //Check and hold which CTouch has ended
    for (CTouch* ct in touches){

        if ([ct checkSameTouch:lastTouchLocation]) {
            removeTouch = ct;
        }
    }

    //Remove the touch
    [touches removeObject:removeTouch];
}

For some reason, the way I'm checking which CTouch object in touches to move in the ccTouchMoved function works fine, but when I try the exact same thing in ccTouchEnded, it doesn't work.

EDIT: I take it back. The "if" statement to update ct in ccTouchMoved only works the first time. I think this might be the real problem. Because the CTouch object isn't being updated, its information isn't the same as the UITouch object's by the time the touch ends.

I have tried the "if" statement in ccTouchEnded with both the lastTouchLocation and touchLocation and neither of them work. The "if" statement is never true and removeTouch is, therefore, always empty.

The lastTouchLocation and touchLocation in ccTouchEnded are always wildly different from the ct.lastPosition. Just to make sure I cover everything, here's a sample set:

ct.lastPosition:     84,125
ct.currentPosition:  85,125

lastTouchLocation:   155,131
touchLocation:       174,132
share|improve this question

1 Answer 1

Alright, I figured it out, and it wasn't in the code I displayed.

I was checking the CTouch object's previous location against the UITouch object's. What I needed to be doing was checking the CTouch object's current location agains UITouch object's previous.

Got it working.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.