0 Replies - 765 Views - Last Post: 22 June 2014 - 05:26 AM Rate Topic: -----

#1 rodka99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-June 14

Collision with screen border in Xcode

Posted 22 June 2014 - 05:26 AM

I am fairly new to objective-c and iOS programming in Xcode, and while making my first app I ran into the problem of creating barriers around the edge of the screen which the object cannot pass through. I tried placing objects at the boundary and using CGRectIntersectsRect, but the collision between the boundary and the main object is not registered. Does anyone know how I would go about in restricting an object from leaving the screen? The code below is used for object movement and my failed attempt at creating a barrier.

- (IBAction)right {
   
    goRight=[NSTimer scheduledTimerWithTimeInterval:rate
                                            target:self
                                          selector:@selector(goRight)
                                          userInfo:nil
                                           repeats:YES];
    if (goRight == nil){
        goRight=[NSTimer scheduledTimerWithTimeInterval:rate target:self selector:@selector(goRight) userInfo:nil repeats:YES];
    }
    


    
}
    
    

- (IBAction)left{
    
    
    goLeft=[NSTimer scheduledTimerWithTimeInterval:rate
                                           target:self
                                         selector:@selector(goLeft)
                                         userInfo:nil
                                          repeats:YES];
    if (goLeft == nil){
        goLeft=[NSTimer scheduledTimerWithTimeInterval:rate target:self selector:@selector(goLeft) userInfo:nil repeats:YES];
    }
    
            
}

-(IBAction)stopLeft{
    [goLeft invalidate];
    goLeft=nil;
}


-(IBAction)stopRight{
    [goRight invalidate];
    goRight=nil;
}

-(void)goLeft{
    _userToken.center =CGPointMake(_userToken.center.x -20, _userToken.center.y);
}

-(void)goRight{
    _userToken.center =CGPointMake(_userToken.center.x +20, _userToken.center.y);
    
}

-(void)checkForCollision{
        if (CGRectIntersectsRect(_userToken.frame, _friendly.frame)){
            NSLog(@"Left Intersect");

        }
    }





Note: userToken is a UIImageView

Is This A Good Question/Topic? 0
  • +

Page 1 of 1