2013-11-02 97 views
4

我想创建一个侧面卷轴,我无法将我的对象的Y坐标设置为随机值。Y坐标没有正确更新

我打电话给我的对象平台。我希望每个平台都以不同的坐标出现在Y之间,并且相信我的方法是这样做的正确方法。但它运作得不太好。

所有的Y坐标登出相同的数字,我不完全确定为什么?我的意思是当我将它们实例化时,我明确地增加了间距。

我还注意到的一件事是,如果我不添加计时器并调用移动方法,它们会显示在正确的位置。所以它可能是在调用函数之间的东西。

我发现的另一个问题是,当我再次在平台中调用时,只有一个平台遵循幻灯片函数要求的操作,另外两个遵循要点,但对其他任何操作都没有反应。

任何帮助超级赞赏!

// 
// C4WorkSpace.m 
// TheGame 
// 
// 

#import "C4Workspace.h" 

@implementation C4WorkSpace { 

    C4Shape *player ; // player 
    CGPoint p, move; // CG point for moving platforms && Players 
    int speed; // Speed of the platforms 
    C4Timer *timer; // Timer 
    NSMutableArray *platforms; // Platform Array 

} 

-(void)setup { 


    speed = 5; // Speed Limit 
    p = CGPointMake(self.canvas.width, 400); // Making 2 coordinates for the platform shape to follow 
    move = CGPointMake(0, 0); // Making 2 coordinates for the user shape to follow 
    platforms = [NSMutableArray array]; // Pointer of Array for platforms 

    // Generating shapes 

    for (int i = 0; i < 3; i++) 
    { 
     C4Shape * s = [C4Shape rect:CGRectMake(0, 400, 50, [C4Math randomInt:50])]; // Making the platform 
     p.x = self.canvas.width; // x - coordinate for the platforms 
     p.y += 100; // y - coordinate of the platforms 
     s.center = p; // The Center of the Circle is P 
     [platforms addObject:s]; // Adding platforms to the platforms array 
     [self.canvas addShape:platforms[i]]; // Adding an instance of it 
     timer = [C4Timer automaticTimerWithInterval:1.0f/30 target:self method:@"slide" repeats:YES]; // Timer to shoot it off ever frame 

    } 


    player = [C4Shape ellipse:CGRectMake(0, 0, 50, 50)]; // The shape of the player 
    [self.canvas addSubview:player]; // Adding an instance of the player 



} 

//Moving the platform 

-(void) slide { 

    //Calling the platforms again to add movement 

    for (C4Shape *s in platforms){ 

    // Adding boundries 

    if (p.x <= 0) { 
     p.x = self.canvas.width; // if it's smaller than the width of the cavas auto transport 
     p.y = [C4Math randomInt:self.canvas.height]; // choose a different y coordinate for each 

    } 

    p.x-= speed; // Adding accelaration 
    C4Log(@"The Y is .%2f", p.y); // Logging the problem 
    s.center = p; // making the shape follow the point 

    } 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *place = [[event allTouches] anyObject]; // Get touches 
    move = [place locationInView:place.view]; // Gets the location of the current mouse point 
    player.center = move; // folllowing the move point 

    [self collisionCheck]; // collision check 
} 


-(void) collisionCheck { 
    //currently empty! 

    } 


@end 

回答

2

本的答案只关于更新p变量是正确的。你想要做的是检查每个形状的中心点并操纵它。

的原因错误是这样的逻辑:

for(every shape in platforms) { 
    check to see if a point p is off the screen 
     if it is, then change its value to a random number 
    then update the speed of p 
    set the centerpoint of the current shape to p 
} 

以上的逻辑是,你在这里编码的内容:

for (C4Shape *s in platforms) { 
    if (p.x <= 0) { 
     p.x = self.canvas.width; 
     p.y = [C4Math randomInt:self.canvas.height]; 
    } 
    p.x-= speed; // Adding accelaration 
    s.center = p; // making the shape follow the point 
} 

的问题,这是这行:

s.center = p; // making the shape follow the point 

因为将所有形状的中心点设置为相同的点。但是,只有最后一点才会有所作为。

你的方法应该如下所示:

-(void) slide { 
    //Calling the platforms again to add movement 
    for (C4Shape *currentShape in platforms) { 
     CGPoint currentCenterPoint = currentShape.center; 
     if (currentCenterPoint.x <= 0) { 
      // if it's smaller than the width of the cavas auto transport 
      currentCenterPoint.x = self.canvas.width; 
      // choose a different y coordinate for each 
      currentCenterPoint.y = [C4Math randomInt:self.canvas.height]; 
     } 
     currentCenterPoint.x-= speed; //Adding accelaration 
     currentShape.center = currentCenterPoint; 
    } 
} 

另外注意,这种方法将重命名变量使代码更易读。这对于帮助记住正在发生的事情以及让其他人更容易阅读代码是一种很好的做法。

注意:您真的很接近能够将这种功能添加到它自己的类中,以使事情变得更简单,做得更好!

+0

嗨特拉维斯,非常感谢你提供了非常精美的答案。我同意在自己的课堂上加入它会更好,但我不会100%地理解如何去做。我会尽快发布我为之做的代码!这主要是使用'@synthesis''@property',这让我感觉很不舒服。 – tailedmouse

+0

其易于使用的属性,但要真正了解他们需要一些阅读,所以看看:http://www.c4ios.com/tutorials/properties .. p.s.在大多数情况下,你不应该使用@synthesize –

+0

是的,我无法完全理解它。亚当教会了我们这一点,所以我拆开了他的代码,并尝试保留我需要的并修改了一下。我上传了这个问题:D – tailedmouse

3

y坐标是完全更新 - 在您检查,并在玻片法所有的C4Shapes'中心设置为单点。

在setup中,这很好地工作,因为在更改它之前将C4Shape s的中心设置为p,但是当您转到幻灯片方法中的For循环时,您只需记录并更新该点即可从设置中更新它的最后一个位置开始,假设在设置和幻灯片被调用之间没有任何反应。 p是C4Workspace类的ivar,所以每个实例只有一个。为了解决这个问题,我相信你应该改变幻灯片中的每一个事件到s.center,并摆脱最后一行。

顺便说一句,你应该认真考虑重命名这些方法中的变量,它们很难遵循 - 我实际上非常困惑,为什么p是一个伊娃而不是在安装程序中声明,这似乎是唯一你需要它的地方。

+0

对不起,延迟回复!非常感谢你的解释!非常有意义:D – tailedmouse