1

我有一个小应用程序,我有几个使用 MotionManager.gravity 在板上移动的精灵“球”。
我有代码,当任何一个球遇到边缘时,就会在边缘停止。如果任何一个球到达角落,它也会停在角落里。我还编写了代码,其中如果两个或三个球在任何一个边缘上并相互接触(它们打开了碰撞检测),它们就会停下来,保持它们与另一个球的相对位置。

这是我的代码:

//这会分配motionManager并设置重力值。

self.motionManager = [[CMMotionManager alloc] init];
    self.motionManager.deviceMotionUpdateInterval = 0.005f;
    self.motionQueue = [[NSOperationQueue alloc] init];
    self.motionQueue.name = [[[NSBundle mainBundle] bundleIdentifier] stringByAppendingString:@".motion"];
    self.updatePosition = NO;
    [self.motionManager startDeviceMotionUpdatesToQueue:self.motionQueue withHandler:^(CMDeviceMotion *motion, NSError *error) {
        @synchronized(self) {
            NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];

            [formatter setNumberStyle:NSNumberFormatterDecimalStyle];

            [formatter setMaximumFractionDigits:2];

            [formatter setRoundingMode: NSNumberFormatterRoundUp];
            {
                _numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:motion.gravity.x / 15.0 *200]];
                _numberStringy = [formatter stringFromNumber:[NSNumber numberWithFloat:motion.gravity.y / 15.0 *200]];
                n = [_numberString intValue];
                 y = [_numberStringy intValue];
             }
            self.gravity = motion.gravity;
            self.updatePosition = YES;
        }
    }];
    [self startDisplayLink];

//这显示了我如何将球停在边缘

- (void)handleDisplayLink:(CADisplayLink *)displayLink
{


    @synchronized(self) {
        if (!self.updatePosition)
            return;

        self.ball.position = CGPointMake(self.ball.position.x + (n*.99), self.ball.position.y + (y*.99));
        self.ball2.position = CGPointMake(self.ball2.position.x + (n*.98), self.ball2.position.y +(y*.98));
        self.ball3.position = CGPointMake(self.ball3.position.x + n, self.ball3.position.y + y);

        if (ball.position.x <=340) {
            ball.position = CGPointMake(340, ball.position.y);
        }

        if (ball.position.x >=684) {
            ball.position = CGPointMake(684, ball.position.y);
        }
        if (ball.position.y <=148) {
            ball.position = CGPointMake( ball.position.x,148);
        }
        if (ball.position.y >=620) {
            ball.position = CGPointMake(ball.position.x,620);
        }

//这会把它停在角落里

if ( ball.position.x >=684 && ball.position.y <=148 ) {
            ball.position = CGPointMake(684, 148);
        }
        if ( ball.position.x >=684 && ball.position.y >=620 ) {
            ball.position = CGPointMake(684, 620);
        }
        if ( ball.position.x <=340 && ball.position.y <=148 ) {
            ball.position = CGPointMake(340, 148);
        }
        if ( ball.position.x <=340 && ball.position.y >=620 ) {
            ball.position = CGPointMake(340, 620);
        }

//这显示了我是如何设置的,如果它们在一侧并且球1在角落里,球2在下一个球3在下一个,它们保持它们的相对位置。我想如果我设置角球并且其余的都打开了碰撞检测,他们就会停下来。他们这样做了,但他们将角落里的 ball1 推到了它的位置之外。所以我写了代码

if (ball2.position.y == ball3.position.y && ball.position.y == ball2.position.y && ball.position.x < ball2.position.x && ball2.position.x < ball3.position.x  ) {
           int s = (self.ball3.position.x - self.ball2.position.x);

           int t = ((self.ball2.position.y - self.ball3.position.y));

           k = t + s;

           int f =(self.ball2.position.x - self.ball.position.x);
           int g = ((self.ball2.position.y - self.ball.position.y));
           j = f+g;

           int h =(self.ball3.position.x - self.ball.position.x);
           int i = ((self.ball3.position.y - self.ball.position.y));
           l = h + i;



            NSLog(@"ball2centerx:%f",ball2.position.x);
            NSLog(@"ball2centery:%f",ball2.position.y);
            NSLog(@"ballcenterx:%f",ball.position.x);
            NSLog(@"ballcentery:%f",ball.position.y);
            NSLog(@"ball3centerx:%f",ball3.position.x);
            NSLog(@"ball3centery:%f",ball3.position.y);

            CGPoint w = CGPointMake(340, 148);
            CGPoint x = CGPointMake(340, 620);
            CGPoint v = CGPointMake(684, 148);
            CGPoint z = CGPointMake(684, 620);
            CGPoint m = CGPointMake(370, 148);
            CGPoint q = CGPointMake(370, 620);
            CGPoint o = CGPointMake(654, 148);
            CGPoint p = CGPointMake(654, 620);



            if (CGPointEqualToPoint(ball.position,w) ) {
                if (f < 30) {
                ball2.position = CGPointMake(370, 148);
                }
            }
            if (CGPointEqualToPoint(ball.position,x)) {
                if (f<30) {
                ball2.position = CGPointMake(370, 620);
                }
            }

            if (CGPointEqualToPoint(ball3.position,v)) {
                if  (s <30){
                ball2.position = CGPointMake(654, 148);
                }
            }


            if (CGPointEqualToPoint(ball3.position,z)) {
                if  (s <30){
                ball2.position = CGPointMake(654, 620);
                }
            }

            if (CGPointEqualToPoint(ball2.position,m)) {
                if  (s <30){
                    ball3.position = CGPointMake(400, 148);
                }

            }

            if (CGPointEqualToPoint(ball2.position,o)) {
                if (f<30) {
                ball.position = CGPointMake(624, 148);
                }
            }

            if (CGPointEqualToPoint(ball2.position,q)) {
                 if  (s <30){
                ball3.position = CGPointMake(400, 620);
                 }
            }

            if (CGPointEqualToPoint(ball2.position,p)) {
                if (f<30) {
                ball.position = CGPointMake(624, 620);
                }
            }

            }

问题是当我添加更多球时,代码将变得非常复杂。

有没有办法这样写,如果一个球(任何球)在角落里,而另一个球在第二个位置,它将停留在那里,依此类推。有点像所有球配置的通用代码。而不是为每个配置编写它?

4

1 回答 1

0

如果场景中有多个节点,为每个节点创建一个属性变得乏味且不切实际。一个好的解决方案是创建每个节点并将其添加到数组中。该数组将允许您保留对它的引用。重要的是创建具有自己唯一名称的每个节点。您可以通过创建计数器属性来做到这一点。

@property (nonatomic) int myCounter;

然后,当您创建节点时,设置节点的名称属性,如下所示:

myCounter++;
myNode.name = [NSString stringWithFormat:@"myNode-%i",myCounter];

完全创建节点后,将其添加到 NSMutableArray 中,如下所示:

[self.myArray addObject:myNode];

您可以根据需要向阵列添加任意数量的节点。

要枚举数组,您可以这样做:

for (SKSpriteNode *object in myArray) {
    // let's look for a node with the name of myNode-2
    if([myNode.name isEqualToString:@"myNode-2"]) {
        NSLog(@"Found it");
    }
}

这是一个枚举数组以检查每个节点的位置并将其速度设置为零的示例,具体取决于 IF 语句:

for (SKSpriteNode *object in myArray) {
    if((object.position.x >= 300) && (object.position.y >= 300)) {
        object.physicsBody.velocity = CGVectorMake(0, 0);
    }
}

请记住,因为您将数组中的对象存储为 SKSpriteNodes 并使用 SKSpriteNode 枚举您的数组,所以您可以访问所有相应的属性,如速度、位置、名称等...

于 2015-06-23T13:55:20.677 回答