Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使块与屏幕边界碰撞_Ios_Objective C_Uikit Dynamics - Fatal编程技术网

Ios 使块与屏幕边界碰撞

Ios 使块与屏幕边界碰撞,ios,objective-c,uikit-dynamics,Ios,Objective C,Uikit Dynamics,我将跟随斯坦福ios7课程第8章的内容,在那里,老师构建了一个简化的俄罗斯方块游戏,上面有彩色的方块,你必须在上面填充行。在添加重力行为以使块落下后,他添加了碰撞行为(请注意下面的属性),惰性地实例化它,并且在执行此操作时,他像这样设置边界 _collider.translatesReferenceBoundsIntoBoundary = YES; 这使得块体与屏幕底部发生碰撞(而不是掉落),因此它们可以堆叠在彼此的顶部。然后,他将碰撞行为添加到animator属性中,作为最后一步,在dro

我将跟随斯坦福ios7课程第8章的内容,在那里,老师构建了一个简化的俄罗斯方块游戏,上面有彩色的方块,你必须在上面填充行。在添加重力行为以使块落下后,他添加了碰撞行为(请注意下面的属性),惰性地实例化它,并且在执行此操作时,他像这样设置边界

 _collider.translatesReferenceBoundsIntoBoundary = YES;
这使得块体与屏幕底部发生碰撞(而不是掉落),因此它们可以堆叠在彼此的顶部。然后,他将碰撞行为添加到animator属性中,作为最后一步,在
drop
方法中,他将dropView(即块)添加到碰撞行为中。当他跑的时候,石块撞到了底部,互相堆叠在一起。当我运行时,使用下面的代码,块继续通过屏幕底部(在模拟器上)落下。换句话说,没有堆叠

你能理解为什么碰撞行为可能不起作用吗

视图控制器

@property (strong,nonatomic) UIDynamicAnimator *animator;
@property (strong, nonatomic) UIGravityBehavior *gravity;
@property (strong, nonatomic) UICollisionBehavior *collider;

@end

@implementation DropItViewController

static const CGSize DROP_SIZE = { 40, 40 };
- (IBAction)tap:(UITapGestureRecognizer *)sender {

    [self drop];
}

- (UICollisionBehavior *)collider
{
    if (!_collider){
        _collider = [[UICollisionBehavior alloc] init];
        _collider.translatesReferenceBoundsIntoBoundary = YES;
        [self.animator addBehavior:_collider];
    }
    return _collider;
}

- (UIDynamicAnimator *)animator
{
    if (!_animator) {
        _animator = [[UIDynamicAnimator alloc] init];
    }
    return _animator;
}

-(UIGravityBehavior *)gravity
{
    if (!_gravity) {
        _gravity = [[UIGravityBehavior alloc] init];
        [self.animator addBehavior:_gravity];

    }
    return _gravity;
}
drop
方法中将dropView添加到碰撞器中

-(void)drop
{
    CGRect frame;
    frame.origin = CGPointZero;
    frame.size = DROP_SIZE;
    int x = (arc4random() % (int)self.gameView.bounds.size.width) / DROP_SIZE.width;

    frame.origin.x = x * DROP_SIZE.width;
    UIView *dropView = [[UIView alloc] initWithFrame:frame];
    dropView.backgroundColor = self.randomColor;

    [self.gameView addSubview:dropView];

    [self.gravity addItem:dropView];
    [self.collider addItem:dropView];
}

实例化
UIDynamicAnimator
时,请使用
initWithReferenceView
而不是
init
。然后,当您使用
translatesReferenceBoundsIntoBoundary
时,它将知道要使用的引用边界。

当您实例化
UIDynamicMator
时,请使用
initWithReferenceView
而不是
init
。然后,当您使用
translatesReferenceBoundsToBoundary
时,它将知道要使用什么引用边界。

我也在使用相同的过程,并且认为我看到了相同的问题。但是,请尝试在4.0英寸模拟器中运行。你的方块可能只是在屏幕外收集(在3.5英寸屏幕的范围之外)。

我也在这一过程中,并认为我看到了同样的问题。但是,请尝试在4.0英寸模拟器中运行。您的方块可能正在屏幕外收集(在3.5英寸屏幕的边界之外)