ios - 碰撞检测和 animateWithDuration

标签 ios ios4

我试图让“luke”跳过一个物体以确保游戏不会结束,他确实这样做了,但即使他避开了物体,游戏仍然会结束,就好像 luke 在游戏过程中没有离开他原来的位置一样动画。

我在 -(void)touchesBegin 中使用了 UIView animateWithDuration 来尝试实现这一点。

[UIView animateWithDuration:3
                 animations:^
 {
     luke.center = CGPointMake(luke.center.x +0, luke.center.y -60);
 }
                 completion:^(BOOL completed)
 {
     if (completed)
     {
         [UIView animateWithDuration:3
                     animations:^     
          {
              luke.center = CGPointMake(luke.center.x +0, luke.center.y +60);
          }];
     }

我还使用 CGRectIntersectsRect 来告诉我两个对象何时发生碰撞

-(void)collision {

if (CGRectIntersectsRect(luke.frame, smallboulder.frame)) {
    [self endgame];
}

if (CGRectIntersectsRect(luke.frame, largeboulder.frame)) {
    [self endgame];

}

if (CGRectIntersectsRect(luke.frame, cactus.frame)) {
    [self endgame];
}   

我终于可以使用设置为 NSArray 的动画来显示跳跃时的运动了。

非常感谢

日本

最佳答案

当使用 UIView 动画方法为各种属性设置动画时,动画实际上是在称为表示层的东西上执行的。但是当您使用 View 的 frame 访问器时,它会访问模型层(而不是表示层),模型层保存最新的值(因此它保存动画后的指定帧)。

您应该使用 luke.layer.presentationLayer.frame 检查“luke”的位置。

关于ios - 碰撞检测和 animateWithDuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22122160/

相关文章:

iphone - 从窗口中移除时 UIScrollview contentOffset 发生变化

ios4 - 你如何使用 MPMediaItem Property Persistent IS 在 iPhone Music Player Framework 中播放音乐?

iphone - UILabel + touchDown

iphone - 自定义键盘建议

iphone - UIImagePNGRepresentation() 和比例(iPhone 4 屏幕)

javascript - 连接失败 (1006) 消息 : TB. 套接字错误,连接超时 (1008)

ios - 如何在 SwiftUI 中单击按钮时添加 UI 组件?

ios - 如何在 iOS 中创建类似书架的 View ?

ios - Swift - 如何绘制一条线,其中每个点都可以有单独的颜色

ios - 如何从 Alamofire 返回值