Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

CGPointMake Center on iPad vs iPhone

So I'm making a game (Pong) for both the iPad and iPhone. Currently it works great on the iPhone but NOT on the iPad. It seems to stay within the same are of an iPhone when running it on the iPad and I don't know why. Can anyone help me with this?

''' -(void)BallMovement{

[self ComputerMovement];
[self Collision];

Ball.center = CGPointMake(Ball.center.x + X, Ball.center.y + Y);

if (Ball.center.x < 15) {
    X = 0 - X;
}

if (Ball.center.x > 305) {
    X = 0 - X;
}

//Scoreboard
if (Ball.center.y < 0) {
    PlayerScoreNumber = PlayerScoreNumber + 1;
    PlayerScore.text = [NSString stringWithFormat:@"%i", PlayerScoreNumber];

    [timer invalidate];
    StartButton.hidden = NO;

    //Ball reset and Computer
    Ball.center = CGPointMake(160, 282);
    Computer.center = CGPointMake(160, 27);

    if (PlayerScoreNumber == 10) {
        StartButton.hidden = YES;
        Exit.hidden = NO;
        WinOrLose.hidden = NO;
        WinOrLose.text = [NSString stringWithFormat:@"You Win!"];
    }
}//END Game

if (Ball.center.y > 550) {
    ComputerScoreNumber = ComputerScoreNumber + 1;
    ComputerScore.text = [NSString stringWithFormat:@"%i", ComputerScoreNumber];
    [timer invalidate];
    StartButton.hidden = NO;

    //Ball Reset and Computer
    Ball.center = CGPointMake(160, 282);
    Computer.center = CGPointMake(160, 27);

    if (ComputerScoreNumber == 10) {
        StartButton.hidden = YES;
        Exit.hidden = NO;
        WinOrLose.hidden = NO;
        WinOrLose.text = [NSString stringWithFormat:@"Try Again!"];
    }
}

}

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; }

  • (void)viewDidLoad { PlayerScoreNumber = 0; ComputerScoreNumber = 0;

    [super viewDidLoad]; // Do any additional setup after loading the view. }

  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }'''

2 Answers

Instead of hard coding the coordinates, consider calculating them relative to screen width and height:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

I'll try that thanks. Also, would it be better if I created a different .h & .m file for the iPad part of the game?

Up to you. The difference in real estate can provide opportunities for different behaviors, but be very careful with duplicated codes. They are often hard to maintain and error prone.

Cheers, Thanks again John.