为简单的迷宫游戏创造墙壁

时间:2014-03-24 10:14:04

标签: ios iphone

我正在做一个单一的视图游戏演示,一个像墙壁游戏的迷宫。玩家将使用UIButton控制角色。

基本上我已经设计出我需要检查如果在下一步中,角色将与墙相交,则角色移动将返回false。但是,我似乎无法将它们组合在一起。

我目前有这个布尔函数来检查角色是否会与墙相交

-(Boolean) checkCollision : (CGRect) newFrame{
CGRect frame = self.mainchar.frame;
frame.origin.x = self.currentPoint.x;
frame.origin.y = self.currentPoint.y;

for (UIImageView *image in self.hardwalls) {

    if (CGRectIntersectsRect(frame, image.frame)) {
        return true;
    }
}
return false;

}

我的UIButton for movement是

-(IBAction)CharMovingLeft:(id)sender; {
CGPointMake(mainchar.center.x -charmovement, mainchar.center.y);

我应该在按钮方法中添加什么内容,以便在交叉点发生时停止此移动?

提前致谢

1 个答案:

答案 0 :(得分:0)

-(IBAction)CharMovingLeft:(id)sender {

    CGRect newFrame = YOUR_CHARACTERS_FRAME_IF_IT_MOVED;

    if (![self checkCollision:newFrame];
        //MOVE CHARACTER:

    }

    else 
        //DONT MOVE CHARACTER
}

这会......?

相关问题