Hi,
I’m trying to randomly spawn ellipses at the top of the screen and have them drop, influenced by gravity. I have a physics.body shown with an image on the bottom of the screen that I can drag along an x-axis. When the ellipses fall, I would like to be able to “catch” them. Eventually, I would like to have them spawn faster based on how long you survive (you have to catch all the ellipses). So far I’ve been able to spawn an ellipse at random x values, but for some reason they are not dropping even though I have set gravityScale to 1. Sorry for my ignorance on the topic. I’ve just begun using Codea. I’ve posted my code below and replaced any images with images that come default with Codea in case you wanted to test my code. Warning, it is very basic.
Thanks!
--# Main
-- Basket Catch
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
bmaWidth = 100
bmaHeight = 100
inTouch = false
bma = physics.body(POLYGON,
vec2(-bmaWidth/2,-bmaHeight/2),
vec2(-bmaWidth/2,bmaHeight/2),
vec2(bmaWidth/2, bmaHeight/2),
vec2(bmaWidth/2, -bmaHeight/2))
bma.x = WIDTH/2
bma.y = 100
bma.gravityScale = 0
box = DragMe()
b1 = Ball(100,50,300,color(128,128,128))
timer = 0
b = physics.body(CIRCLE,50)
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(3)
-- Do your drawing here
timer = timer + 1
text("Score:0",WIDTH-100,HEIGHT-50)
box:draw()
b1:draw()
end
function touched(t)
box:touched(t)
end
--# DragMe
DragMe = class()
function DragMe:init()
-- you can accept and set parameters here
self.pos = vec2(bma.x,bma.y)
self.size = 100
end
function DragMe:draw()
-- Codea does not automatically call this method
pushStyle()
fill(0, 33, 255, 255)
rectMode(CENTER)
sprite("Cargo Bot:Crate Blue 1",self.pos.x,self.pos.y,self.size,self.size)
popStyle()
end
function DragMe:hit(point)
if point.x > (self.pos.x - self.size/2) and
point.x < (self.pos.x + self.size/2) and
point.y > (self.pos.y - self.size/2) and
point.y < (self.pos.y + self.size/2) then
return true
end
return false
end
function DragMe:touched(t)
-- Codea does not automatically call this method
if self:hit(vec2(t.x,t.y)) and
t.state == MOVING then
self.pos = self.pos + vec2(t.deltaX, 0)
end
end
--# Ball
Ball = class()
function Ball:init()
-- you can accept and set parameters
self.pos = vec2(math.random(WIDTH),HEIGHT-100)
gravityScale = 1
end
function Ball:draw()
-- Codea does not automatically call this method
pb = physics.body(CIRCLE,50)
pb.x = self.pos.x
pb.y = self.pos.y
pb.gravityScale = 1
ellipse(pb.x,pb.y,50)
fill(0, 46, 255, 255)
end
function Ball:touched(touch)
-- Codea does not automatically call this method
end