Search code examples
iosdynamiccocos2d-iphonepositioning

Adaptive Positioning Based on iOS Device


I made an iPhone game a few months ago, and am now trying to port it as a universal app to both the iPad and iPhone 5 with Cocos2D. I was wondering if there was a simple-ish way to determine where an object should be placed based on the device running the game.

I could use if statements to figure out which device the game is running on, so when I get the correct sized images for the device I could have separate positions for each object, but it seems like there would be a maths formula which would allow me to use a lot less code. Obviously something like a full screen background is very simple, because it just needs to be centred with:

[background setPosition:CGPointMake(screenSize.width/2,screenSize.height/2)];

I haven't a clue how to adapt a button that would be X = 144 & Y = 330 on the old 3.5inch, 640 by 960 resolution iPhone to an iPad or iPhone 5 resolution.

I'm willing to use a more recent version of iOS if it will make my life easier, but because I'm not using any of Apple's objects I don't know if that is possible.

Maybe this isn't even possible because the button will be different sizes for the iPhone and iPad version, but I thought I would ask.


Solution

  • yeah, i am usually facing the same problem, but if it is just a static objects placement i would have relative coordinates instead of absolute for every object and then use screen sizes to place them correctly so you might want to use a function like:

    -(CGPoint) relativeToScreen:(CGPoint) p { 
        return ccp(screenSize.width * p.x, screenSize.height * p.y) 
    }
    

    where 0.0 <= p.x =< 1.0 and the same for p.y

    and don't forget about your anchorPoint, because the node position is based on it as well

    and i hope you have discovered that cocos2d already does image choosing instead of you, you just have to set right suffixes for your images: -hd, -ipad, -ipadhd