在板子精灵套件Xcode周围移动几个球

时间:2015-06-22 22:59:09

标签: sprite-kit cmmotionmanager

我有一个小应用程序,我有几个精灵“球”使用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);
        }

//这显示了我如何设置它如果它们在一侧并且ball1在角落而ball2是下一个并且ball3是下一个它们保持它们的相对位置。我想如果我设置了角球并且其他人已经开启了碰撞检测,他们就会停下来。他们这样做,但是他们将球从角落里推出了位置。所以我写了代码

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);
                }
            }

            }

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

有没有办法写它,以便如果一个球(任何球)在角落而另一个球在位置2,它将保持在那里等等。有点像所有球配置的通用代码。而不是为每个配置写它?

1 个答案:

答案 0 :(得分: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枚举数组,所以您可以访问所有相应的属性,如速度,位置,名称等......