简体   繁体   中英

iPhone Game Development, collision detection that creates a wall effect

Alright i tried this last time and it was too vague so I'll try again. I am making an top-down perspective iPhone game, there is a mainSprite (a UIImageView) which will collide with another UIImageView. When it does, I want the mainSprite to stop moving. Basically creating a wall. Does that make sense? I am not using any frameworks like cocos2d. I know the CGRectIntersectWithRect method, my question is what do i need to put in that method. I don't have any other variables such as speed or velocity, just buttons that will move the sprite around the screen. Here is my code.

This is the .m code used to move the sprite around the screen.

-(void)goDown{
mainSprite.center = CGPointMake(mainSprite.center.x, mainSprite.center.y+5);
}

-(IBAction)down{
    [self downAnimation];
    goDown = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(goDown) userInfo:nil repeats:YES];
    if (goDown == nil) {
        goUp = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(goDown) userInfo:nil repeats:YES];

    }
}

Here is the method I have set up for the collision:

-(void)collision {
    if (CGRectIntersectsRect(mainSprite.frame, collisionImage.frame)) {

    }
}

Help would be greatly appreciated.

What you could do is add a bool on whether or not the object has collided (I would suggest as an instance variable)

With this you could set it to YES when the rects have intersected

in your goDown method you would then check the boolean, if the boolean is YES then do not change the center, otherwise continue changing it

Everytime -goDown is called add the check for the collision

This way, if it has collided you could actually deactivate that timer so it no longer goes down, or you could do it the boolean way I mentioned above.

I suggest making the method that checks for a collision with one argument of the current center so that you can check if adding 5 to the y of the center will cause it to collide so you can deactivate the timer/stop adding 5 right as it collides rather than right after

- (void)goDown {
   float delta = [self checkCollision];
   mainSprite.center = CGPointMake(mainSprite.center.x, mainSprite.center.y + delta);
}

- (float)checkCollision {
   CGRect testFrame = CGRectMake(mainSprite.frame.origin.x, mainSprite.frame.origin.y + 5.0f, mainSprite.frame.size.width, mainSprite.frame.size.height);
   if (CGRectIntersectsRect(testFrame, collisionImage.frame)) {
       return 0.0f;
       // stop the sprite from moving anymore so it doesn't actually intersect, just stops "on top"
    }
    return 5.0f;
    // if they have not collided then just continue on your way

the code sample actually shows neither of my methods above

what i do here is check for the collision, and if theres going to be a collision then stop adding any to the center, otherwise it'll continue adding to the center coordinate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM