This started out as a question about making an app universal, but I ended up figuring it out.
So here is a short little guide for anyone trying to figure it out.
If you want to make your app universal, you need to set every value according to the WIDTH and HEIGHT as in the code below, then it works as a universal app.
Whether the app runs in landscape or portrait, make sure you set values in the horizontal axis (x) relative to the WIDTH and values in the vertical axis (y) relative to the HEIGHT. Else you will run into scaling problems.
Here is the original code before being converted to universal, so you can compare and see what I did. All fixed values have been converted to values that are relative to WIDTH or HEIGHT.
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)
function setup()
ast={}
ship=readImage("Space Art:Red Ship")
asteroid=readImage("Space Art:Asteroid Large")
astFreq=1.5
astSpeed=-9
score=0 -- starting score
timer=0
rectMode(CENTER)
if readLocalData("HS")== nil then
saveLocalData("HS", 0)
end
music("A Hero's Quest:Battle",true,.5)
gameState=0
font("Futura-CondensedExtraBold")
fontSize(64)
fill(0, 255, 0, 255)
shipX=WIDTH/2
shipY=HEIGHT/4
end
function spawn()
table.insert(ast,vec2(math.random(96,WIDTH-96),1152))
end
function draw()
if gameState==0 then
background(0, 0, 0, 255)
text("PLAY",WIDTH/2,HEIGHT/2)
if CurrentTouch.state==BEGAN then
if CurrentTouch.x>WIDTH/2-256 and CurrentTouch.x<WIDTH/2+256
and CurrentTouch.y>HEIGHT/2-128 and CurrentTouch.y<HEIGHT/2+128 then
gameState=1
end
end
end
if gameState==1 then
background(0, 0, 0, 255)
pushStyle()
text("Score: "..score, WIDTH/2, HEIGHT/1.2)
popStyle()
for a,b in pairs(ast) do
sprite(asteroid,b.x,b.y,192,256)
b.y=b.y+astSpeed
end
timer=timer+DeltaTime
if timer>astFreq then
spawn()
spawn()
spawn()
timer=0
end
for a,b in pairs(ast) do
if b.y<-128 then
table.remove(ast,a)
score=score+10
end
end
if CurrentTouch.x <= WIDTH / 2 and CurrentTouch.state ~= ENDED and CurrentTouch.state ~= CANCELLED then
shipX=shipX-12
end
if CurrentTouch.x>WIDTH/2 and CurrentTouch.state ~= ENDED and CurrentTouch.state ~= CANCELLED then
shipX=shipX+12
end
sprite(ship,shipX,shipY,64,96)
for a,b in pairs(ast) do
if b.y-96<=shipY+48 and b.y+160>=shipY-48 and
b.x-96<=shipX+32 and b.x+96>=shipX-32 then
gameState=2
end
end
if shipX-32<0 or shipX+32>WIDTH then
gameState=2
end
end
if gameState==2 then
background(0, 0, 0, 255)
text("RETRY",WIDTH/2,HEIGHT/4)
if score > readLocalData("HS") then
saveLocalData("HS", score)
end
text("Score: "..score, WIDTH/2,HEIGHT/1.2)
text("HighScore: "..readLocalData("HS"),WIDTH/2,HEIGHT/2)
if CurrentTouch.state==BEGAN then
if CurrentTouch.x>WIDTH/2-128 and CurrentTouch.x<WIDTH/2+128
and CurrentTouch.y>HEIGHT/4-64 and CurrentTouch.y<HEIGHT/4+64 then
score=0
gameState=0
shipX=WIDTH/2
for i,t in pairs(ast) do ast[i]= nil
end
end
end
end
end
And here is the app converted to Universal so it runs on iphones or ipads.
The comments in the beginning of setup were used for testing. Delete the double dash before the WIDTH and HEIGHT for the device size that you want to test in Codea. Make sure that you don’t have the WIDTH and HEIGHT set to values before exporting the code to Xcode, or the final app will not work as universal and will be set to whatever size you set WIDTH and HEIGHT to.
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)
function setup()
-- iphone 4
--WIDTH=320
--HEIGHT=480
-- iphone 5
--WIDTH=320
--HEIGHT=568
ast={}
ship=readImage("Space Art:Red Ship")
asteroid=readImage("Space Art:Asteroid Large")
bg=readImage("SpaceCute:Background")
astFreq=1.5
astSpeed=-HEIGHT/114
score=0 -- starting score
timer=0
rectMode(CENTER)
if readLocalData("HS")== nil then
saveLocalData("HS", 0)
end
music("A Hero's Quest:Battle",true,.5)
gameState=0
font("Futura-CondensedExtraBold")
fontSize(HEIGHT/16)
fill(0, 255, 0, 255)
shipX=WIDTH/2
shipY=HEIGHT/4
end
function spawn()
table.insert(ast,vec2(math.random(WIDTH/8,WIDTH-WIDTH/8),HEIGHT+HEIGHT/8))
end
function draw()
if gameState==0 then
sprite(bg,WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
text("PLAY",WIDTH/2,HEIGHT/2)
if CurrentTouch.state==BEGAN then
if CurrentTouch.x>WIDTH/2-WIDTH/3 and CurrentTouch.x<WIDTH/2+WIDTH/3
and CurrentTouch.y>HEIGHT/2-HEIGHT/8 and
CurrentTouch.y<HEIGHT/2+HEIGHT/8 then
gameState=1
end
end
end
if gameState==1 then
sprite(bg,WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
pushStyle()
text("Score: "..score, WIDTH/2, HEIGHT/1.2)
popStyle()
for a,b in pairs(ast) do
sprite(asteroid,b.x,b.y,WIDTH/4,HEIGHT/4)
b.y=b.y+astSpeed
end
timer=timer+DeltaTime
if timer>astFreq then
spawn()
spawn()
spawn()
timer=0
end
for a,b in pairs(ast) do
if b.y<-HEIGHT/8 then
table.remove(ast,a)
score=score+10
end
end
if CurrentTouch.x <= WIDTH / 2 and CurrentTouch.state ~= ENDED and
CurrentTouch.state ~= CANCELLED then
shipX=shipX-WIDTH/64
end
if CurrentTouch.x>WIDTH/2 and CurrentTouch.state ~= ENDED and
CurrentTouch.state ~= CANCELLED then
shipX=shipX+WIDTH/64
end
sprite(ship,shipX,shipY,WIDTH/12,HEIGHT/10.67)
for a,b in pairs(ast) do
if b.y-HEIGHT/10.667<=shipY+HEIGHT/21.33 and
b.y+HEIGHT/6.4>=shipY-HEIGHT/21.33 and
b.x-WIDTH/8<=shipX+WIDTH/24 and b.x+WIDTH/8>=shipX-WIDTH/24 then
gameState=2
end
end
if shipX-WIDTH/24<0 or shipX+WIDTH/24>WIDTH then
gameState=2
end
end
if gameState==2 then
sprite(bg,WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
text("RETRY",WIDTH/2,HEIGHT/4)
if score > readLocalData("HS") then
saveLocalData("HS", score)
end
text("Score: "..score, WIDTH/2,HEIGHT/1.2)
text("HighScore: "..readLocalData("HS"),WIDTH/2,HEIGHT/2)
if CurrentTouch.state==BEGAN then
if CurrentTouch.x>WIDTH/2-WIDTH/7.68 and
CurrentTouch.x<WIDTH/2+WIDTH/7.68
and CurrentTouch.y>HEIGHT/4-HEIGHT/16 and
CurrentTouch.y<HEIGHT/4+HEIGHT/16 then
score=0
gameState=0
shipX=WIDTH/2
for i,t in pairs(ast) do ast[i]= nil
end
end
end
end
end
This may be easy stuff for the experienced programmers, but for a newbie like me it threw me of a bit. Hopefully this helps someone in the future.