简体   繁体   English

碰撞检测和AnimateWithDuration

[英]Collision detection & animateWithDuration

I am trying to get 'luke' to jump over an object to ensure that the game does not end, which he does but even if he avoids the object the game still ends, as if luke had not left his original location during the animation. 我试图让'luke'跳过一个对象,以确保游戏不会结束,但他确实这样做,但是即使他避开了该对象,游戏仍会结束,就像luke在动画过程中没有离开他的原始位置一样。

I used the UIView animateWithDuration in -(void)touchesBegin to try to achieve this. 我在-(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);
          }];
     }

I am also using CGRectIntersectsRect to tell me when the two objects collide 我还使用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];
}   

finally can i use an animation set up as a NSArray to show movement when jumping. 最后,我是否可以使用设置为NSArray的动画来显示跳跃时的运动。

Many thanks 非常感谢

JP J.P

When using UIView animation methods to animate various properties, the animation is actually performed on something called the presentation layer. 使用UIView动画方法设置各种属性的动画时,实际上是在称为表示层的东西上执行动画。 But when you use the view's frame accessor, it accesses the model layer (rather than the presentation layer), which holds the latest values (so it holds the designated frame after animation). 但是,当您使用视图的frame访问器时,它会访问模型层(而不是表示层),该模型层保存最新的值(因此,它在动画后保存指定的帧)。

You should check for "luke"'s location using luke.layer.presentationLayer.frame . 您应该使用luke.layer.presentationLayer.frame检查“ luke”的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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