It looks like you're new here. If you want to get involved, click one of these buttons!
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!")
playbuttonT = readImage("Dropbox:PlayButton")
pbx = WIDTH-150
pby = HEIGHT-100
inTouch = false
pbvx,pbvy = 0,0
r = 100
trampoline = readImage("Dropbox:Trampoline")
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
playbuttonT = readImage("Dropbox:PlayButtonGrey")
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
playbuttonT = readImage("Dropbox:PlayButton")
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
trampoline = readImage("Dropbox:TrampolinePressure")
else
trampoline = readImage("Dropbox:Trampoline")
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
Comments
Something like this maybe
Ya that's much smoother than mine thanks!
@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.
@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.
This seems to work
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 evenplayer=class()
but the entire game? There may be a completely logical reason for this.I'm not sure I was just trying to figure out the best way to make a menu and this worked out pretty well.