@dave1707
That was what I was trying to do the whole time but I don’t understand why the ball still goes through the ground and how to fix it
Try this.
function setup()
p_rect = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
p_rect.x = 0
p_rect.y = 103
p_rect.type=STATIC
character = physics.body(CIRCLE,50)
character.x = WIDTH/2
character.y = 300
-- Start in the center of the screen
position = vec2( WIDTH/2, HEIGHT/2 )
-- Create two parameters to play with
parameter.number( "yCalibration", 0, 2.0, 0.6 )
parameter.number( "tiltStrength", 1, 40, 10 )
tab={ -- top 1=draw 0=no draw
{0,0,0,0,0,0,0,0,1,0},
{1,1,1,1,1,1,1,1,1,1}, -- bottom
}
end
function draw()
background(0, 145, 255, 255)
for x=1,#tab do
for y=1,#tab[1] do
if tab[x][y]==1 then
sprite("Platformer Art:Block Grass",y*70,(#tab+1-x)*70)
end
end
end
moveVec = vec2(Gravity.x, Gravity.y) + vec2( 0, yCalibration )
position = position + moveVec * tiltStrength
fill(255, 0, 0, 255)
ellipse( position.x, character.y, character.radius*2)
end
@magicskillz I didn’t like my above code, so I wrote this. It creates a physics object at each Sprite position making table changes easier.
EDIT: I also changed the way the character position is calculated.
supportedOrientations(LANDSCAPE_ANY)
function setup()
character = physics.body(CIRCLE,25)
character.x = WIDTH/2
character.y = HEIGHT
tab={ -- 1=draw 0=no draw
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,0,0,0,0,1},
{1,0,0,0,0,0,1,1,1,1},
{1,0,0,1,1,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
}
spTab={}
for x=1,#tab do
for y=1,#tab[1] do
if tab[x][y]==1 then
p1 = physics.body(POLYGON,vec2(0,0),vec2(70,0),vec2(70,70),vec2(0,70))
p1.x=y*70-35
p1.y=(#tab+1-x)*70-35
p1.type=STATIC
table.insert(spTab,p1)
p1=nil
end
end
end
end
function draw()
background(0, 145, 255, 255)
fill(255, 0, 0, 255)
character.linearVelocity=vec2(Gravity.x*1000,character.linearVelocity.y)
ellipse(character.x, character.y, character.radius*2)
for x=1,#tab do
for y=1,#tab[1] do
if tab[x][y]==1 then
sprite("Platformer Art:Block Grass",y*70,(#tab+1-x)*70)
end
end
end
end
Sigh,
A new question:
When the camera moves the tiles don’t work properly
Code:
--# Main
supportedOrientations(LANDSCAPE_ANY)
function setup()
person=readImage("Platformer Art:Guy Look Right")
canjump=nil
PlacesCantGo:init()
gamestate=1
cam = vec2(0,0)
parameter.watch("xpos")
parameter.watch("ypos")
parameter.watch("timer")
timer=0
starttimer=false
character = physics.body(CIRCLE,25)
character.x = 100
character.y = 300
tab={ -- 1=grass 0=no draw 2=spike
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,2,0,0,0,0,0,0,2,0,1,2},--skip one row for spike
{0,0,0,0,1,0,0,0,0,0,0,1,1,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1},
}
spTab={}
for x=1,#tab do
for y=1,#tab[1] do
if tab[x][y]==1 then
p1 = physics.body(POLYGON,vec2(0,0),vec2(70,0),vec2(70,70),vec2(0,70))
p1.x=y*70-35
p1.y=(#tab+1-x)*70-35
p1.type=STATIC
table.insert(spTab,p1)
p1=nil
end
end
end
end
function draw()
if gamestate==2
then
background(0, 145, 255, 255)
fill(255, 0, 0, 255)
character.linearVelocity=vec2(Gravity.x*1000,character.linearVelocity.y)
sprite(person,character.x, character.y+10, character.radius*2)
cam.x=cam.x-1
translate(cam.x,cam.y)
for x=1,#tab do
for y=1,#tab[1] do
if tab[x][y]==1 then
sprite("Platformer Art:Block Grass",y*70,(#tab+1-x)*70)
end
end
end
for x=1,#tab do
for y=1,#tab[1] do
if tab[x][y]==2 then
sprite("Platformer Art:Spikes",y*70,(#tab-x)*70)
end
end
end
ypos = character.y
ypos = string.format("%.0f", ypos)
xpos=character.x
xpos = string.format("%.0f", xpos)
if starttimer==true
then
character.y=character.y+5
timer=timer+1
end
for z=1,4 do
if xpos == PlacesCantGox[z]
then
if ypos == PlacesCantGoy[z]
then
restart()
end
end
end
if character.y < 0
then
restart()
end
end
if gamestate==1
then
sprite("Project:background",WIDTH/2,0,WIDTH,HEIGHT)
sprite("Project:background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
--Text
fontSize(59)
fill(0, 214, 255, 255)
font("Didot-Bold")
text("Tap Screen to Start",WIDTH/2,HEIGHT/2)
-- Codea does not automatically call this method
end
function touched(t)
if t.state==BEGAN
then
gamestate=2
end
end
function touched(t)
if gamestate==2
then
if t.state==BEGAN and canjump==true and starttimer==false
then
person=readImage("Platformer Art:Guy Jump")
starttimer=true
end
end
if gamestate==1
then
if t.state==BEGAN
then
gamestate=2
end
end
end
function collide( c )
if gamestate==2
then
if c.state==BEGAN
then
person=readImage("Platformer Art:Guy Look Right")
print("Collide")
canjump=true
starttimer=false
timer=0
end
end
end
end
--# PlacesCantGo
PlacesCantGo = class()
function PlacesCantGo:init(x)
-- you can accept and set parameters here
PlacesCantGox = {
"4",
"11",
"14"
}
PlacesCantGoy = {
"2",
"2",
"2"
}
end
function PlacesCantGo:draw()
-- Codea does not automatically call this method
end
function PlacesCantGo:touched(touch)
-- Codea does not automatically call this method
end
@magicskillz Your indenting is so screwed up that you can’t see what goes with what. You have 2 function touched() inside of the draw function. You only need one function touched(), and it shouldn’t be within another function. You also have the function collide() within the draw function, again a no. You’re trying to use a class, but you don’t seem to know how to properly use it. You’ve been a member of the forum for a year, which means you should know basic programming by now. Every time I work on your code, I have to spend time reformatting it just to see what doesn’t line up or what doesn’t belong before I even execute you code.
Ok I’ll work on reformatting my work!
@magicskillz Maybe this will help you. Press the up arrow to jump. Press the left or right arrows to scroll the blocks left or right. Tilt the iPad to move the character left or right. If the character hits the spikes, he dies. Look at this code to see how I did things so you can add whatever else you want. Any questions, just ask. Also, look at the indenting to see how easy it is to understand what belongs to what.
EDIT: Code removed. A better version is below.
@magicskillz Here’s a better version. The character always stays in the middle of the screen. Tilt the iPad to move the background left or right. Tap the screen to jump. Landing on the spikes kills the character.
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
function setup()
character = physics.body(CIRCLE,25)
character.x = WIDTH/2
character.y = 200
tab={
{1,0,4,0,0,0,0,4,0,0,4,0,0,0,0,0,0,0,0,4,0,0,4,0,0,1},
{1,0,4,0,0,0,0,4,0,0,4,0,0,0,0,0,0,0,0,4,0,0,4,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1},
{1,1,0,0,0,2,0,0,3,2,1,1,1,1,2,0,2,0,1,1,1,0,3,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, -- bottom
}
spTab={}
for x=1,#tab do
for y=1,#tab[1] do
if tab[x][y]>0 then
if tab[x][y]==1 then
p1 = physics.body(POLYGON,vec2(0,0),vec2(70,0),
vec2(70,70),vec2(0,70))
elseif tab[x][y]==2 then
p1 = physics.body(POLYGON,vec2(0,0),vec2(70,0),
vec2(70,10),vec2(0,10))
elseif tab[x][y]==3 then
p1 = physics.body(POLYGON,vec2(0,0),vec2(70,0),
vec2(70,50),vec2(0,50))
elseif tab[x][y]==4 then
p1 = physics.body(POLYGON,vec2(17,0),vec2(53,0),
vec2(53,50),vec2(17,50))
p1.loc=vec2(x,y)
end
p1.x=y*70-35
p1.y=(#tab+1-x)*70-35
p1.type=STATIC
p1.info=tab[x][y]
table.insert(spTab,p1)
p1=nil
end
end
end
dx=0
ch=character.x
coins=0
end
function draw()
background(0, 145, 255, 255)
fill(255, 0, 0, 255)
fontSize(50)
text("COINS "..coins,WIDTH/2,HEIGHT-50)
pushMatrix()
translate(dx,0)
for x=1,#tab do
for y=1,#tab[1] do
if tab[x][y]==1 then
sprite("Platformer Art:Block Grass",y*70,(#tab+1-x)*70)
elseif tab[x][y]==2 then
sprite("Platformer Art:Spikes",y*70,(#tab+1-x)*70)
elseif tab[x][y]==3 then
sprite("Platformer Art:Fence Broken",y*70,(#tab+1-x)*70)
elseif tab[x][y]==4 then
sprite("Platformer Art:Coin",y*70,(#tab+1-x)*70)
end
end
end
if character~=nil then
dx=dx-(character.x-ch)
ch=character.x
character.linearVelocity=vec2(Gravity.x*1000,character.linearVelocity.y)
sprite("Platformer Art:Guy Standing",character.x,character.y+10,50)
end
popMatrix()
if character==nil then
text("Character died",WIDTH/2,HEIGHT/2)
end
end
function touched(t)
if t.state==BEGAN then
if character~=nil then
if character.linearVelocity.y>-.001 and character.linearVelocity.y<1 then
character.linearVelocity=vec2(character.linearVelocity.x,250)
end
end
end
end
function collide(c)
if c.state==BEGAN then
if c.bodyA.info==2 or c.bodyB.info==2 then
character:destroy()
character=nil
dir=0
return
end
if c.bodyA.info==4 then
coins=coins+1
tab[c.bodyA.loc.x][c.bodyA.loc.y]=-1
c.bodyA:destroy()
end
if c.bodyB.info==4 then
coins=coins+1
tab[c.bodyB.loc.x][c.bodyB.loc.y]=-1
tab[x][y]=-1
c.bodyB:destroy()
end
end
end
Added code to the above program to show coins that can be collected for points.
Thanks to the code @dave1707 provided I created this.
For some reason the background messed up when I took a photo
Photo: