GravityX = 0
GravityY = 0 -- we are just using this for the watch()
-- Only support one orientation so you can tilt a lot
supportedOrientations(LANDSCAPE_LEFT)
-- Use this function to perform your initial setup
function setup()
displayMode(FULLSCREEN_NO_BUTTONS)
print("An example that shows how to use the Gravity vector")
watch("GravityX")
watch("GravityY")
end
-- This function gets called once every frame
function draw()
GravityX = Gravity.x -- a bit not nice
GravityY = Gravity.y
background(127, 127, 127, 255)
stroke(255, 255, 255, 255)
strokeWidth(15)
lineCapMode(ROUND)
pushMatrix()
translate(WIDTH/2, HEIGHT/2)
grav = vec2(Gravity.x * 300, Gravity.y * 300)
--print(grav)
line(0, 0, grav.x, grav.y)
-- Arrowhead
down = vec2(1, 0)
orient = down:angleBetween(grav)
pushMatrix()
resetMatrix()
translate(WIDTH/2,HEIGHT/2)
translate(grav.x,grav.y)
rotate(math.deg(orient))
line(0, 0, -25, -20)
line(0, 0, -25, 20)
popMatrix()
-- End Arrowhead
popMatrix()
end
--Main
supportedOrientations(LANDSCAPE_ANY)
function setup()
displayMode(FULLSCREEN)
objectTable = {}
lastGenerated = ElapsedTime
interval = 0.1
num = 1
table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))
end
function draw()
spriteMode(CENTER)
sprite("Documents:Black Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
pushStyle()
spriteMode(CENTER)
randomObjectGenerate(1)
popStyle()
end
function randomObjectGenerate()
for k,v in pairs(objectTable) do
v:draw()
end
if ElapsedTime - lastGenerated > interval then
lastGenerated = ElapsedTime
num = num + 1
table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))
end
for k,v in pairs(objectTable) do
if v.a.y < 0 or (v.a.x > WIDTH or v.a.x< 0) or v.a.info.id == 0 then
table.remove(objectTable,k)
v.a:destroy()
v.a = nil
v=nil
end
end
end
--Objects
Objects = class()
function Objects:init(x,cat,id)
-- you can accept and set parameters here
self.a = physics.body(CIRCLE,20)
self.a.x = x
self.a.y = HEIGHT
self.a.gravityScale = 1
self.a.restitution = 0.0
self.a.interpolate = true
self.asleepingAllowed = true
self.flag = true
self.category = cat
self.a.info = {}
self.a.info.id = id
end
function Objects:draw()
pushMatrix()
translate(self.a.x, self.a.y)
if self.category == 1 then
sprite("Cargo Bot:Condition Blue",0,0,50,50)
elseif self.category == 2 then
sprite("Cargo Bot:Condition Green",0,0,50,50)
elseif self.category == 3 then
sprite("Cargo Bot:Condition Red",0,0,50,50)
elseif self.category == 4 then
sprite("Documents:Coin",0,0,50,50)
end
popMatrix()
end
function Objects:touched(touch)
-- Codea does not automatically call this method
end