0

I am creating an iOS app with Xcode 6.1 using Objective C

I am trying to create code to stop a moving object (Character) when hits another object. I have tried to do this using two methods but the object seem to be stopping at a different point to what I have set it to. Below are the two methods I have tried. I have also put links to the full version of the code on dropbox (links below).

Method 1: When the object reaches a particular co-ordinate.

//Right Wall
if(Character.center.x>295){
    [CharacterMovingTimer invalidate];
}
//Left Wall
if(Character.center.x<30){
    [CharacterMovingTimer invalidate];
}
//Top Wall
if(Character.center.y<30){
    [CharacterMovingTimer invalidate];
}
//Bottom Wall
if(Character.center.y>587){
    [CharacterMovingTimer invalidate];
}

Method 2: When the object intersect with the second object

if(CGRectIntersectsRect(Character.frame, RightWall.frame)){
 [CharacterMovingTimer invalidate];
 }
 if(CGRectIntersectsRect(Character.frame, LeftWall.frame)){
 [CharacterMovingTimer invalidate];
 }
 if(CGRectIntersectsRect(Character.frame, TopWall.frame)){
 [CharacterMovingTimer invalidate];
 }
 if(CGRectIntersectsRect(Character.frame, BottomWall.frame)){
 [CharacterMovingTimer invalidate];
 }

ViewController.h https://www.dropbox.com/s/56j7rokp4il5eul/ViewController_h.rtf?dl=0

ViewController.m https://www.dropbox.com/s/faqqbsnqb8o4se7/ViewController_m.rtf?dl=0

  • What speed is the character moving? If you want the character to stop at 295 you would need to set their position after stopping the timer. If the character is moving at 40 pixels per timer tick the stop position could range between 295 and 335. – Gary Riches Feb 06 '15 at 15:16
  • The character is moving at 30 pixels every 0.3 seconds. – SBannerman Feb 06 '15 at 15:50
  • And how far is the character off from where you expect him to be when you have a collision? – Gary Riches Feb 06 '15 at 15:52
  • Sorry the issue is that when the character gets close to the wall it stops at a point that is not that actual co-ordinate i have set it to.. please take a look at this image, you can see the point at which the character stops is after it has gone through the object. https://www.dropbox.com/s/0vcebj725y6qpw5/Main_storyboard2.png?dl=0 it was set to stop once they touch, when i try to change the co-ordinates slightly it never allows the character to stop at that exact point. – SBannerman Feb 06 '15 at 15:58
  • Ok, so back to my original comment, I will now use that as an answer. – Gary Riches Feb 06 '15 at 16:04

1 Answers1

0

If you want the character to stop at 295 you would need to set their position after stopping the timer. If the character is moving at 40 pixels per timer tick the stop position could range between 295 and 335.

Using your first code as an example:

if(Character.center.x>295){
    Character.center = CGPointMake(295, Character.center.y);
    [CharacterMovingTimer invalidate];
}
//Left Wall
if(Character.center.x<30){
    Character.center = CGPointMake(30, Character.center.y);
    [CharacterMovingTimer invalidate];
}
//Top Wall
if(Character.center.y<30){
    Character.center = CGPointMake(Character.center.x, 30);
    [CharacterMovingTimer invalidate];
}
//Bottom Wall
if(Character.center.y>587){
    Character.center = CGPointMake(Character.center.x, 587);
    [CharacterMovingTimer invalidate];
}
Gary Riches
  • 2,847
  • 1
  • 22
  • 19
  • Thank you for you answer, but this is still not working the same issue is occurring, the collision is identified either before or after the objects actually collide. Could it be something related to my settings rather than my code? – SBannerman Feb 06 '15 at 21:30
  • Could you post a new screen shot of what's happening now please? – Gary Riches Feb 07 '15 at 07:26
  • And, given the iPhone 6 screen width of 375, what makes you think that the square is not at 295? – Gary Riches Feb 07 '15 at 20:53
  • When I increase the co-ordinate number to 300, it stops at the same point. Even when the number is 305, it still stops at the exact same point. – SBannerman Feb 07 '15 at 22:55
  • Log out the x when it stops. I am guessing your statement will be incorrect and that you are just not noticing the 5 or 10 pixel change. – Gary Riches Feb 08 '15 at 08:25
  • Slight movement with objects usually noticeable. But in this case I can see the object is in the same place until I increase the figure by 50 pixels, then it makes a dramatic jump. So there is no in between. – SBannerman Feb 11 '15 at 12:56
  • I would imagine that the number is 30 pixels as that's what you're moving by. You do change BOTH the check AND the assignment yes? I would instead move by 10 pixels every 0.1 if you're going to do it that way, it allows better control – Gary Riches Feb 11 '15 at 13:23
  • I have tried that but no change. I have uploaded the code to dropbox, give it a go if you have a chance you'll understand. – SBannerman Feb 11 '15 at 13:38
  • Is there a link the to the project? The old links just link to old code with none of the changes mentioned here. – Gary Riches Feb 11 '15 at 14:39
  • I have not saved any of these changes as they have not made a difference to the problem. I would like others to be able to alter the initial code to find a solution. – SBannerman Feb 11 '15 at 15:51
  • How about building the screen, the map, the walls. I'm prepared to look at your issue, not rebuild the project off of 1 view controller – Gary Riches Feb 11 '15 at 15:53
  • I didn't realise the project link was not available https://www.dropbox.com/s/w5fmht5dinz5wfj/ObjectMovement.zip?dl=0 – SBannerman Feb 13 '15 at 00:00
  • I have downloaded and looked and everything is working as it should be. I changed the value from 295 to 300 and the block stopped 5 pixels further than it did previously. Be sure to change both values: `if(Character.center.x>305){ Character.center = CGPointMake(305, Character.center.y);` Log out the positions and you will see – Gary Riches Feb 13 '15 at 10:43