【问题标题】:Y coordinate not updating properlyY 坐标未正确更新
【发布时间】:2013-11-02 04:56:27
【问题描述】:

我正在尝试创建一个横向滚动条,但无法将我的对象的 Y 坐标设置为随机值。

我称我的对象为平台。我希望每个平台出现在不同的Y 坐标上,并相信我的方法是正确的做法。但它工作得不是很好。

所有Y 坐标都注销相同的数字,我不完全确定为什么?我的意思是我在实例化它们时显然是在添加间距。

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

我发现的另一个问题是,当我再次调用平台时,只有一个平台遵循滑动功能要求它执行的操作,其他 2 个遵循点,但对其他任何内容没有反应。

非常感谢任何帮助!

//
//  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

【问题讨论】:

    标签: ios objective-c c4


    【解决方案1】:

    y 坐标正在完美更新 - 在您正在检查的单点上,并将所有 C4Shapes 的中心设置为幻灯片方法。

    在设置中这工作正常,因为您在更改之前将 C4Shape s 的中心设置为 p,但是当您转到幻灯片方法中的 For 循环时,您只是记录和更新那个点,它开始在您在 setup 中更新它的最后一个地方,假设在调用 setup 和 slide 之间没有任何反应。 p 是您的 C4Workspace 类的一个 ivar,因此每个实例都有一个。为了解决这个问题,我认为您应该将幻灯片中每次出现的 p 更改为 s.center,并去掉最后一行。

    顺便说一句,你应该认真考虑重命名这些方法中的变量,它们很难理解——我实际上很困惑为什么 p 是一个 ivar 而不是在 setup 中声明的,这似乎是只在你需要的地方。

    【讨论】:

    • 抱歉回复晚了!非常感谢您的解释!很有意义:D
    【解决方案2】:

    Ben 关于仅更新 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;
        }
    }
    

    另外,请注意此方法重命名变量,因此代码更具可读性。这是一个很好的做法,可以帮助记住正在发生的事情,并让其他人能够更轻松地阅读您的代码。

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

    【讨论】:

    • 嘿,特拉维斯,非常感谢您提供的精彩详尽的回答。我同意将它添加到它自己的课程中会更好,但我不是 100% 了解如何做到这一点。我很快就会发布我为它做的代码!主要是使用 '@synthesis' '@property' 把我搞砸了。
    • 它易于使用的属性,但要真正理解它们需要一些阅读,所以看看:c4ios.com/tutorials/properties .. p.s.在大多数情况下,您不必@synthesize
    • 是的,我无法完全理解它。亚当教了我们一点,所以我把他的代码拆开,试着保留我需要的东西,并做了一些修改。我上传了问题:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多