“Newton” (in my practice project) is supposed to be a square object that jumps constantly, moves to the side when you tilt your screen, and glides when you touch the screen. I also set it so that if he goes too far in any direction, he will be stopped (in terms of where he is on the screen) and the background will move in the opposite direction in his exact speed. Think of the background scrolling in super mario bros, but he just WONT MOVE!!! I can not figure out why this block isn’t jumping constantly, gliding when you touch the screen, and moving side to side when you tilt the screen. Please someone help me with this. Side note: I am fairly experienced with codea programming as of now, so I knew what I was doing when constructing this code.
Main class:
supportedOrientations(PORTRAIT_ANY)
function setup()
parameter.watch("test")
parameter.watch("touching")
parameter.watch("newtonxspeed")
parameter.watch("newtonyspeed.linearVelocity.y")
parameter.watch("newton.linearVelocity.x")
parameter.watch("newton.linearVelocity.y")
parameter.watch("stuff1center.y")
displayMode(OVERLAY)
x=TitleScreen
i=0
end
function draw()
x:draw()
end
function touched(t)
x:touched(t)
end
----TitleScreen class: (only press start game when in this class)
TitleScreen = class()
--doodle jump like game
--sir newton jumps up endlessly tall tree
--falling apples that kill you if you hit below them, but you can jump off of
--branches produce apples, cam be jumped off of
function TitleScreen:init()
rectMode(CORNER)
textMode(CENTER)
st=false
cr=false
he=false
end
function TitleScreen:draw()
fontSize(HEIGHT/51.2)
local rectheight=HEIGHT/20.48
local rectwidth=rectheight*4
sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
stroke(0, 231, 255, 255)
strokeWidth(5)
fill(8, 255, 0, 255)
if st then
fill(11, 0, 255, 255)
end
rect(WIDTH/2-100,HEIGHT/2+125,rectwidth,rectheight)
fill(8, 255, 0, 255)
if cr then
fill(11, 0, 255, 255)
end
rect(WIDTH/2-100,HEIGHT/2-25,rectwidth,rectheight)
fill(8, 255, 0, 255)
if he then
fill(11, 0, 255, 255)
end
rect(WIDTH/2-100,HEIGHT/2-175,rectwidth,rectheight)
fill(255, 0, 22, 255)
text("Start Game",WIDTH/2,HEIGHT/2+HEIGHT/6.8)
text("Credits",WIDTH/2,HEIGHT/2)
text("Help",WIDTH/2,HEIGHT/2-HEIGHT/6.8)
textSize(150,30)
end
function TitleScreen:touched(touch)
if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and
touch.y >= HEIGHT/2+125 and touch.y <= HEIGHT/2+175 then
if touch.state==BEGAN then
st=true
elseif touch.state==ENDED then
x=Game()
end
end
if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and
touch.y >= HEIGHT/2-25 and touch.y <= HEIGHT/2+25 then
if touch.state==BEGAN then
cr=true
elseif touch.state==ENDED then
x=Credits()
end
end
if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and
touch.y >= HEIGHT/2-175 and touch.y <= HEIGHT/2-125 then
if touch.state==BEGAN then
he=true
elseif touch.state==ENDED then
x=Help()
end
end
if touch.state==ENDED then
st=false
cr=false
he=false
end
end
----Game class:
Game=class()
function Game:init()
rectMode(CENTER)
spriteMode(CENTER)
textMode(CENTER)
newtonsize=HEIGHT/102.4
newton=physics.body(POLYGON,vec2(-newtonsize,newtonsize),vec2(-newtonsize,-newtonsize),vec2(newtonsize,-newtonsize),vec2(newtonsize,newtonsize))
newton.type=DYNAMIC
newton.info="main"
newton.x=WIDTH/2
newton.y=newtonsize
newton.gravityScale=1
stuff1center=physics.body(CIRCLE,1)
stuff1center.x=WIDTH/2
stuff1center.y=1
stuff1center.gravityScale=-1
stuff1center.type=KINEMATIC
stuff1={}
stuff2={}
stuff3={}
newtonxspeed=0
newtonyspeed=physics.body(CIRCLE,1)
newtonyspeed.type=KINEMATIC
newtonyspeed.gravityScale=1
minheight1=0
minheight2=-stuff1center.x
floor=physics.body(EDGE,vec2(-WIDTH,-1),vec2(WIDTH*2,-1))
floor.type=STATIC
--make sure newtonyspeed is given a gravity scale and it is what newton.linearVelocity.y AND stuff1center.linearVelocity.y follow, those two variables will no longer need a gravity scale after that.
test=false
touching=false
end
function Game:draw()
sprite("Cargo Bot:Game Area",stuff1center.x,stuff1center.y+HEIGHT/2,WIDTH*3,HEIGHT)
fill(255, 19, 0, 255)
rect(newton.x,newton.y,newtonsize*2,newtonsize*2)
--remember: have everything in the main perspective backgrounds coordinates be relative to stuff1center
minheight1=stuff1center.x+minheight2
--if newton goes below minheight1, the game will end. the minheight2 will become -stuff1center every time newton hits a platform so that minheight1 will be brought up to that point.
newtonxspeed=Gravity.x*100
--If newton goes to the right
if stuff1center.x<=-WIDTH and newtonxspeed>0 then
stuff1center.x=-WIDTH
stuff1center.linearVelocity=0
if newton.x>=WIDTH-WIDTH/51.2 and newtonxspeed>0 then
newton.linearVelocity.x=0
newton.x=WIDTH-WIDTH/51.2
end
elseif stuff1center.x>=WIDTH*2 and newtonxspeed<0 then
stuff1center=WIDTH*2
stuff1center.linearVelocity.x=0
if newton.x<=0+WIDTH/51.2 and newtonxspeed<0 then
newton.linearVelocity.x=0
newton.x=0+WIDTH-WIDTH/51.2
end
else
if newton.x>=WIDTH/2+WIDTH/5.12 and stuff1center.x>=-WIDTH+WIDTH/5.12 and newtonxspeed>0 then
stuff1center.x=stuffcenter.x-newtonxspeed
newton.linearVelocity.x=0
elseif newton.x<=WIDTH/2-WIDTH/5.12 and stuff1center.x<=WIDTH*2-WIDTH*5.12 and newtonxspeed<0 then
stuff1center.x=stuffcenter.x+newtonxspeed
else
if newton.x>=-WIDTH and newton.x<=WIDTH*2 then
newton.linearVelocity.x=newtonxspeed
test=true
else test=false
end
stuff1center.x=0
end
end
--Complete: have newton be stopped byright wall wall once center gets past a certain point.
if newton.y>=HEIGHT/5.12 and newtonyspeed.linearVelocity.y>0 then
newton.linearVelocity.y=0
stuff1center.linearVelocity.y=-newtonyspeed.linearVelocity.y
elseif newton.y<=0 and newtonyspeed.linearVelocity.y<0 then
newton.linearVelocity.y=0
stuff1center.linearVelcotiy.y=newtonyspeed.linearVelocity.y
else newton.linearVelocity.y=newtonyspeed.linearVelocity.y
stuff1center.linearVelocity.y=0
end
end
function Game:touched(touch)
if touch.state==BEGAN then
touching=true
if newtonyspeed.linearVelocity.y~=0 then
newtonyspeed.linearVelocity.y=-10
end
else touching=false
end
end
function collide(c)
if c.state==BEGAN then
if c.bodyA.info==main then
newtonyspeed.linearVelocity.y=20
elseif c.bodyB.info==main then
newtonyspeed.linearVelocity.y=20
end
end
end