4

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
C4 - Travis
  • 4,502
  • 4
  • 31
  • 56
tailedmouse
  • 365
  • 3
  • 16

2 Answers2

3

The y coordinate is updating perfectly -- on the single point you are checking and setting all your C4Shapes' centers to in the slide method.

In setup this works fine, because you set the center of the C4Shape s to p before you change it, but when you go to the For loop in the slide method, you are just logging and updating that one point, which begins at the last place you updated it in setup assuming nothing happens between when setup and slide are called. p is an ivar of your C4Workspace class, so you get one per instance. To fix this, I believe that you should be changing every occurrence of p in slide to s.center, and getting rid of the last line.

Incidentally, you should give serious consideration to renaming the variables in these methods, they are very hard to follow -- I'm actually very confused as to why p is an ivar and not declared in setup, which seems to be the only place you need it.

Ben Pious
  • 4,765
  • 2
  • 22
  • 34
2

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!

C4 - Travis
  • 4,502
  • 4
  • 31
  • 56
  • Hey Travis, thanks so much for awesome elaborate answer. I agree adding it in it's own class would be better, but I don't 100% understand it on how to do it. I will post the code that I did for it soon! It's mostly just using '@synthesis' '@property' that screws me up. – tailedmouse Nov 03 '13 at 14:10
  • its easy to use properties, but to really understand them takes a some reading, so have a look at: http://www.c4ios.com/tutorials/properties .. p.s. you shouldn't have to @synthesize in most cases – C4 - Travis Nov 03 '13 at 17:07
  • Yah I am having trouble understanding it fully. Adam taught us this a little bit, so I took apart his code and tried keeping what I needed and modified a bit. I uploaded the issue :D – tailedmouse Nov 04 '13 at 02:35