#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Jittery movement

edited August 2014 in General Posts: 142

Hi,
So basically I'm trying to make a game (well, part of a game) where a sprite jumps on a trampoline when you flick it downwards. I've made it so the person "jumps" off the trampoline, but it isn't very smooth and I'm not sure what's wrong. I also don't want him to go flying off the screen or bounce off the top. I am trying to get the person to look like what would actually happened if they jumped off a trampoline.

My code is as follows:

-- Trampoline Jumper
displayMode(FULLSCREEN)
playT = false
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
pbx = WIDTH-150
pby = HEIGHT-100
inTouch = false
pbvx,pbvy = 0,0
r = 100
jumped = 0
gravity = 0
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
if playT == false then
background(23, 214, 40, 57)
font("Noteworthy-Bold")
fontSize(75)
fill(255, 255, 255, 255)
text("Trampoline Jumper",WIDTH/2,HEIGHT-250)
sprite(playbuttonT,WIDTH/2,HEIGHT/2)
end
if playT == true then
TrampolineGame:draw()
end
end

function touched(t)
if playT == true then
TrampolineGame:touched(t)
end
if t.x > WIDTH/2 - 100 and t.x < WIDTH/2 + 100 and t.y < HEIGHT/2 + 125 and t.y > HEIGHT/2 - 125 and playT == false and t.state == BEGAN then
end
if t.x > WIDTH/2 - 100 and t.x < WIDTH/2 + 100 and t.y < HEIGHT/2 + 125 and t.y > HEIGHT/2 - 125 and playT == false and t.state == ENDED then
playT = true
end
end

TrampolineGame = class()

function TrampolineGame:draw()
-- Codea does not automatically call this method
background(72, 255, 0, 40)
fill(41, 41, 41, 255)
sprite(trampoline,WIDTH-150,100,250)
sprite("Planet Cute:Character Boy",pbx,pby,1.25* r,2*r)

if inTouch then
pby = pby
else
pby = pby + pbvy

if pby < 80 + r then
pbvy = -pbvy + (0.5 * pbvy)
pby = 2 * (80 + r) - pby
gravity = gravity + 1
end
if pby > WIDTH - 3 then
pbvy = pbvy - gravity
end
if pby < 100 + r then
else
end
end

function TrampolineGame:touched(t)
-- Codea does not automatically call this method
ty = t.y

if t.state == BEGAN and jumped == 0 then
velocity = {}
pbvy = 0
jumped = 1
end

if t.state == BEGAN and jumped == 1 then
local n = 0
for i = 1, 10 do
if velocity[i] then
n = n + 1
pbvy = pbvy + velocity[i].y
end
end
if n > 0 then
pbvy = pbvy / n
end
inTouch = false
end

if t.state == MOVING then
local newVelocity = vec2(t.deltaX, t.deltaY)
table.insert(velocity,1,newVelocity)
end

if t.state == ENDED then
local n = 0
for i = 1, 10 do
if velocity[i] then
n = n + 1
pbvy = pbvy + velocity[i].y
end
end
if n > 0 then
pbvy = pbvy / n
end
inTouch = false
end
end
end

Tagged:

• Posts: 425

Something like this maybe

-- Jump

function setup()
player={x=WIDTH/2,y=400,w=100,h=150,vel=vec2(0,0)}
trampoline={x=WIDTH/2-50,y=0,w=200,h=100}
gravity=vec2(0,-0.1)
print("\nhold to fall faster\n")
parameter.boolean("ceiling",true)
end

function draw()
background(255,0,0)
fill(60)
spriteMode(CORNER)
rectMode(CORNER)
if ceiling then
if AABBquery(player.x,player.y,player.w,player.h,0,HEIGHT-10,WIDTH,10) then
player.vel.y=math.min(player.vel.y,0)
end
rect(0,HEIGHT-10,WIDTH,10)
end
if AABBquery(player.x,player.y,player.w,player.h,
trampoline.x,trampoline.y,trampoline.w,trampoline.h) then
player.vel.y = player.vel.y + 2
rect(trampoline.x,trampoline.y,trampoline.w,trampoline.h/2)
else
rect(trampoline.x,trampoline.y,trampoline.w,trampoline.h)
end
player.vel = player.vel + gravity
player.x = player.x + player.vel.x
player.y = player.y + player.vel.y
sprite("Platformer Art:Guy Standing",player.x,player.y,player.w)
end

function touched(t)
player.vel.y = player.vel.y - 1
end

function AABBquery(x,y,w,h,X,Y,W,H)
if x>X+W or X>x+w or y>Y+H or Y>y+h then
return false
end
return true
end

• Posts: 142

Ya that's much smoother than mine thanks!

• Mod
edited June 2014 Posts: 9,423

@Staples Here's an example. To make the guy go higher, you have to tap the screen multiple times when he's on the trampoline going in an up direction.

function setup() a=physics.body(CIRCLE,10) a.x=300 a.y=600 a.restitution=.8 b=physics.body(CIRCLE,10) b.x=300 b.y=150 b.type=STATIC end function draw() background(40,40,50) sprite("Platformer Art:Hill Short",a.x,a.y) stroke(255) strokeWidth(2) if a.y<275 then line(200,200,275,a.y-75) line(275,a.y-75,325,a.y-75) line(325,a.y-75,400,200) else line(200,200,400,200) end end function touched(t) if a.linearVelocity.y>0 and a.y<300 and t.state==BEGAN then a.linearVelocity=a.linearVelocity+vec2(0,120) end end
• edited June 2014 Posts: 142

@Coder @dave1707 great thanks! I have one more question though. I am trying to make it so ellipses are coming randomly from the right and when they collide with the player the game ends. I tried this, but it is not working. The balls are spawning, but nothing happened when the collide.

displayMode(FULLSCREEN)
playT = false
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
pbx = WIDTH-150
pby = HEIGHT-100
inTouch = false
pbvx,pbvy = 0,0
r = 100
jumped = 0
gravity = 0
player={x=WIDTH-(WIDTH/3),y=400,w=100,h=150,vel=vec2(0,0)}
trampoline={x=WIDTH-(WIDTH/3),y=50,w=200,h=100}
gravity=vec2(0,-0.1)
print("\nhold to fall faster\n")
dodgeballs = {}
cntT = 180
limitT = 240
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
if playT == false then
background(23, 214, 40, 57)
font("Noteworthy-Bold")
fontSize(75)
fill(255, 255, 255, 255)
text("Trampoline Jumper",WIDTH/2,HEIGHT-250)
sprite(playbuttonT,WIDTH/2,HEIGHT/2)
end
if playT == true then
TrampolineGame:draw()
end
end

function createT()
table.insert(dodgeballs,vec2(0,math.random(50,WIDTH-50)))
end

function touched(t)
if playT == true then
TrampolineGame:touched(t)
end
if t.x > WIDTH/2 - 100 and t.x < WIDTH/2 + 100 and t.y < HEIGHT/2 + 125 and t.y > HEIGHT/2 - 125 and playT == false and t.state == BEGAN then
end
if t.x > WIDTH/2 - 100 and t.x < WIDTH/2 + 100 and t.y < HEIGHT/2 + 125 and t.y > HEIGHT/2 - 125 and playT == false and t.state == ENDED then
playT = true
end
end

function AABBquery(x,y,w,h,X,Y,W,H)
if x>X+W or X>x+w or y>Y+H or Y>y+h then
return false
end
return true
end

TrampolineGame = class()

function TrampolineGame:draw()
-- Codea does not automatically call this method
background(72, 255, 0, 40)
fill(41, 41, 41, 255)
spriteMode(CENTER)

if AABBquery(player.x,player.y,player.w,player.h,0,HEIGHT,WIDTH,10) then
player.vel.y=math.min(player.vel.y,0)
end
rect(0,HEIGHT-10,WIDTH,10)

if AABBquery(player.x,player.y,player.w,player.h,
trampoline.x,trampoline.y,trampoline.w,trampoline.h) then
player.vel.y = player.vel.y + 10
sprite("Dropbox:TrampolinePressure",trampoline.x,trampoline.y,trampoline.w,trampoline.h)
else
sprite("Dropbox:Trampoline",trampoline.x,trampoline.y,trampoline.w,trampoline.h)
end
player.vel = player.vel + gravity
player.x = player.x + player.vel.x
player.y = player.y + player.vel.y
sprite("Planet Cute:Character Boy",player.x,player.y,player.w)

for a,db in pairs(dodgeballs) do
ellipse(db.x,db.y,30)
db.x=db.x+5
if db.x>WIDTH then
table.remove(dodgeballs,a)

sound(SOUND_PICKUP, 8734)

elseif db.x < player.x + 50 and db.x > player.x - 50 and db.y <player.y + 75 and db.y > player.y - 75 then
gameOverTrampoline=true
sound("Game Sounds One:Wrong")
end
end

cntT=cntT+1
if cntT>limitT then
if limitT < 181 then
limitT = limitT - 100
end
cntT=0
createT()
end
end

function TrampolineGame:touched(t)
-- Codea does not automatically call this method
player.vel.y = player.vel.y - 1
end

• Posts: 425

This seems to work

displayMode(FULLSCREEN)
playT = false
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
pbx = WIDTH-150
pby = HEIGHT-100
inTouch = false
pbvx,pbvy = 0,0
r = 100
jumped = 0
gravity = 0
player={x=WIDTH-(WIDTH/3),y=400,w=100,h=150,vel=vec2(0,0)}
trampoline={x=WIDTH-(WIDTH/3),y=50,w=200,h=100}
gravity=vec2(0,-0.1)
print("\nhold to fall faster\n")
dodgeballs = {}
cntT = 180
limitT = 200
end

-- This function gets called once every frame
function draw()

-- This sets a dark background color
if playT == false then
background(23, 214, 40, 57)
font("Noteworthy-Bold")
fontSize(75)
fill(255, 255, 255, 255)
text("Trampoline Jumper",WIDTH/2,HEIGHT-250)
sprite(playbuttonT,WIDTH/2,HEIGHT/2)
end
if playT == true then
TrampolineGame:draw()
end
end

function createT()
table.insert(dodgeballs,vec2(0,math.random(50,WIDTH-50)))
end

function touched(t)
if playT == true then
TrampolineGame:touched(t)
end
if t.x > WIDTH/2 - 100 and t.x < WIDTH/2 + 100 and t.y < HEIGHT/2 + 125 and t.y > HEIGHT/2 - 125 and playT == false and t.state == BEGAN then
end
if t.x > WIDTH/2 - 100 and t.x < WIDTH/2 + 100 and t.y < HEIGHT/2 + 125 and t.y > HEIGHT/2 - 125 and playT == false and t.state == ENDED then
playT = true
end
end

function AABBquery(x,y,w,h,X,Y,W,H)
if x>X+W or X>x+w or y>Y+H or Y>y+h then
return false
end
return true
end

TrampolineGame = class()

function TrampolineGame:draw()
pushStyle()spriteMode(CORNER)
-- Codea does not automatically call this method
background(72, 255, 0, 40)
fill(41, 41, 41, 255)

if AABBquery(player.x,player.y,player.w,player.h,0,HEIGHT-10,WIDTH,10) then
player.vel.y=math.min(player.vel.y,0)
end
rect(0,HEIGHT-10,WIDTH,10)

if AABBquery(player.x,player.y,player.w,player.h,
trampoline.x,trampoline.y,trampoline.w,trampoline.h) then
player.vel.y = player.vel.y + 10
rect(trampoline.x,trampoline.y,trampoline.w,trampoline.h)
sprite("Small World:Base Small",trampoline.x,trampoline.y,trampoline.w,trampoline.h)
else
rect(trampoline.x,trampoline.y,trampoline.w,trampoline.h)
sprite("Small World:Base Large",trampoline.x,trampoline.y,trampoline.w,trampoline.h)
end
player.vel = player.vel + gravity
player.x = player.x + player.vel.x
player.y = player.y + player.vel.y
rect(player.x,player.y,player.w,player.h)
sprite("Platformer Art:Guy Jump",player.x,player.y,player.w,player.h)

for a,db in pairs(dodgeballs) do
ellipse(db.x,db.y,30)
db.x=db.x+5
if db.x>WIDTH then
table.remove(dodgeballs,a)

sound(SOUND_PICKUP, 8734)
end
if AABBquery(player.x,player.y,player.w,player.h,db.x-15,db.y-15,30,30)then
print("end")
gameOverTrampoline=true
playT=false
dodgeballs=nil
dodgeballs={}
return
sound("Game Sounds One:Wrong")
end
end

cntT=cntT+1
if cntT>limitT then
if limitT < 181 then
limitT = limitT - 100
end
cntT=0
createT()
end
popStyle()
end

function TrampolineGame:touched(t)
-- Codea does not automatically call this method
player.vel.y = player.vel.y - 1
end

One question though,

Why did you put trampolinegame in a class?
A class is generally used for objects with lots of instances such as raindrop=class() or even player=class() but the entire game? There may be a completely logical reason for this.

• Posts: 142

I'm not sure I was just trying to figure out the best way to make a menu and this worked out pretty well.