The Y coordinate does not update properly

advertisements

I am trying to create a side scroller and I am having trouble setting the Y coordinates of my objects to random values.

I call my objects platforms. I want each of the platform to appear at different Y coordinates, and believe that my approach was the right way of doing this. But it isn't working so well.

All the Y coordinates log out the same number and I am not entirely sure why? I mean I am clearly adding spacing when I am instantiating them.

Also one thing I noticed was, if i don't add the timer and call the moving method they do show up in the right place. so it may be something inbetween calling the function.

Also another problem I found was when I call in the platforms again , only one of the platforms follows what the slide functions asks it to do, the other 2 follows the points, however does not react to anything else.

Any help is super appreciated!

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


Ben's answer about updating only the p variable is right. What you want to do is check the center point of each individual shape and manipulate that.

The reason for the error is this logic:

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
}

The above logic is what you've coded here:

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
}

The problem with this is the line that says:

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

Because is sets ALL the centre points of the shapes to the same point. But, it is only the LAST point that makes a difference.

Your method should look like the following:

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

Also, notice that this method renames the variables so the code is more readable. This is a good practice for helping to remember what's going on, and for other people to be able to read your code more easily.

NOTE: you're really close to being able to add this kind of functionality to a class of its own to make things simpler for yourself, good work!