Question

Im trying to make it so when two images touch an action occurs. The best way of doing this from what I've seen is CGRecIntersectRect and it works very well expect when I have it working with a an image inside an animation loop. If anyone can help me figure it out so that when the two images touch an action occurs that would amazing! `

-(void)startanimation {

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:2];
[UIView setAnimationDidStopSelector:@selector(animationdidstop) ];
_enemy1.center = CGPointMake(_enemy1.center.x +390, _enemy1.center.y);
[UIView commitAnimations]; 

}

-(void)animationdidstop
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:2];
    [UIView setAnimationDidStopSelector:@selector(startanimation) ];
    _enemy1.center = CGPointMake(_enemy1.center.x -390, _enemy1.center.y);
    [UIView commitAnimations];
}


-(void)collision
{
    if (CGRectIntersectsRect(_node.frame, _bounds1.frame)) {
        NSLog(@"got to collision");
        [_node setHidden:YES];
    }
}


`

This part comes right after and is where the issue is. _enemy1.frame doesn't seem to recognize that its colliding or I'm not sure exactly what the issue is but this is where I think the problem is.

 -(void)collision
    {
        if (CGRectIntersectsRect(_node.frame, _enemy1.frame)) {
            NSLog(@"got to collision");
            [_node setHidden:YES];
        }
    }
    }
Was it helpful?

Solution

It will be very hard to achieve this using just CoreAnimation stuff. I recommend you took a look on SpriteKit or Cocos2d

Sprite Kit - is a framework designed to make powerful 2D graphics applications with ease.

Cocos2d - is a framework for building 2D games, demos, and other graphical/interactive applications.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top