Universal Game

i have my game right now inside codea. I’m getting a MacBook pro this wednesday and way wondering if it’s possible to create a universal app with codea. Will the positioning adjust correctly for the iPhone? thanks guys

It’s possible — when you run your Codea project on an iPhone it will behave the same as iPad except that WIDTH and HEIGHT will be iPhone dimensions. You can use these dimensions in your Lua code to ensure that your game adjusts to fit the screen.

so let’s say a button in my game is 150 x 50 pixels. it won’t adjust/scale to the iphone size when i run it in the simulator?

No. So you may instead want the button to be sized WIDTH/7 x WIDTH/21 (assuming you have a landscape game.) Then it will scale properly.

would I have to change the positioning to in terms of WIDTH and HEIGHT. and so basically I would have to convert all my numbers in terms WIDTH and HEIGHT. for example, if I have 80, I would divide it by 768 and get 9.6. I would write WIDTH/9.6 and it would fit to the screen on the iPhone

Yeah, or you could use code like this:

-- s is the scale factor
if WIDTH > HEIGHT then s = math.floor(WIDTH / 1024 + .5)
else s = math.floor(HEIGHT / 1024 + .5) end

Then on every graphic you use, you would just multiply by the scale:


I haven’t actually tested this, but it should work.

@Vega where would I put that code? in function setup()? I’m interested in actually testing it with my game. I would have to add *s to almost everything with numbers right?

Except it’s probably not a linear scale. You should choose the size of things according to the screen and have a test at the beginning that detects whether on an iPad or iPhone.

Yeah, you could put that code in setup. On some programs that will probably work like a charm, on others it may not look perfect. Maybe give it a try and see how it looks.

just a question. would i need to multiply the font size by *s.
edit: i added *s to everything and now i just need to see if it works on the iphone. im getting my macbook pro soon. my dad has it right now because he needs to put his adobe software on it. its looking fine right now on my ipad.

hey @Vega and @Simeon, i used @Reefwing’s tutorial to compile my game in xcode. its working fine on the ipad (retina and regular) simulator but i can’t get it to work on the iphone. when i change the hardware to iphone, i just see a black screen. is there anything i need to change to the codea template so that iphone works. thanks

.@veeeralp - the iPhone has a different resolution to the iPad. Do you detect when you are using the iPhone and use different sprites and positions to suit? You need to do something similar to what happens when the iPad orientation changes.

Not sure. I actually have not yet compiled anything with xCode. Maybe you should try a very basic program on iPhone to test, like just set the background color, draw a few lines to the screen, or something… then test that on iPhone.

by the way, I just realized my code above is bad, it should be:

-- s is the scale factor
if WIDTH > HEIGHT then s = WIDTH / 1024 
else s = HEIGHT / 1024 end

Sorry I wasn’t thinking straight.

maybe its easier to scale your view in the beginning of drawing a frame. so design your game for say 1024x768 and then downscale it for iPhone with a call to scale(WIDTH/1024).

@tnlogy i just went back to my previous game that i wanted to make universal after publishing my ipad only game. how would i downscale it at the beginning of each frame? im thinking of using @Vega’s method. for this game, i see everything as i see it on the ipad, its just cut off

@vega I tried this for my app and I have an Iphone 5 and it worked perfect but when I tried it on an iPhone 4S it didn’t work the same.

@Andrewsimpson4 - Just a quick note about my pet peeve - this is a 1.5 year old thread. Please don’t bump it.

Well since this topic is up here anyways and some people are still wondering… it’s absolute coincidance but I worked on a few tests a few hours ago, and this was one of the results

-- scale

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    s = 0.5

-- This function gets called once every frame
function draw()
    translate(WIDTH/s/2 - WIDTH/2, HEIGHT/s/2 - HEIGHT/2)
    rect(0, 0, WIDTH, HEIGHT)

function touched(t)
    l = vec2(t.x - (WIDTH/s/2 - WIDTH/2)/2, t.y - (HEIGHT/s/2 - HEIGHT/2)/2)
    if l.x <= WIDTH*s and l.x >= 0 and l.y >= 0 and l.y <= HEIGHT*s then

with this you can scale your program with the


from setup, this will scale the drawing and put it in the center of the screen, this will also translate touches so that they are correct for the new ‘screens size’

I could work on this a little more so that it could be made to build universal apps, like, make everything on ipad dimensions, and then if you export it, everything would be scaled down to the device dimensions