well there is no error now, but it doesn't detect any collisions, and it runs slow. i'll post my updated code and soo if anyone can find an error.
Double Post MergeCode:-----------------------------------------------------------------Initiate
----Functions:
function collision(x1, y1, w1, h1, x2, y2, w2, h2)
if (y2 >= y1 and y1 + h1 >= y2) or (y2 + h2 >= y1 and y1 + h1 >= y2 + h2) or (y1 >= y2 and y2 + h2 >= y1) or (y1 + h1 >= y2 and y2 + h2 >= y1 + h1) then
if x2 >= x1 and x1 + w1 >= x2 then
return true
elseif x2 + w2 >= x1 and x1 + w1 >= x2 + w2 then
return true
elseif x1 >= x2 and x2 + w2 >= x1 then
return true
elseif x1 + w1 >= x2 and x2 + w2 >= x1 + w1 then
return true
end
end
return false
end
canjump=true
----resources:
green = Color.new(0,100,0)
red = Color.new(200,0,0)
background=Image.load("background.png")
orb=Image.load("orb.png")
tiles=Image.load("blocktile.png")
sndjump= Sound.load("jump.wav")
blueblock= Image.load("blue.png")
----background music:
Music.playFile("amazon.xm",true)
Music.volume(128)
screen:clear()
----Init Variables
-- the map
map = {
"a a",
"a aaaaaa",
"a a",
"a a",
"a a",
"a a",
"a aaaaaa",
"a a",
"a a",
"a a",
"a aa a",
"a a",
"a a",
"a aa a",
"a a",
"a a",
"cccccccccccccccccccccccccccccc",
}
-- offsreen image, where the map will be drawn
board = Image.createEmpty(480, 272)
function drawTile(tile, x, y)
local tileX = math.mod(tile, 3)
local tileY = math.floor(tile / 3)
board:blit(16 * x, 16 * y, tiles, 16 * tileX , 16 * tileY, 16, 16, false)
end
n=1
board:blit(0, 0, background, false)
local solidX = {}
local solidY = {}
for y = 1, 17 do
line = map[y]
for x = 1, 30 do
solidX["n"] = (x-1)*16
solidY["n"] = (y-1)*16
tile = string.byte(line, x) - string.byte("a")
drawTile(tile, x - 1, y - 1)
n=n+1
end
end
v=n
-- current player position
playerX = 400
playerY = 32
vspeed=0
-- current player animation image
animation = 0
-- 1 = animation images increases, -1: decreases
animationDirection = 1
-- animation slowdown counter
animationSlowDown = 0
---------------------------------------------------------------------------Main loop
while true do
----------------------------------Gravity and friction and block detection
g=3
n=1
for n = 1,v do
if collision(playerX, playerY+1,32,32,solidX["n"],solidY["n"], 16,16) == true then----------------This is the piece of code that is ignored.
vspeed=0
canjump=true
g=2
else
if n == v then
if g == 3 then---------------the following happens all the time, as if it never detects a collision and g is never changed to 2
canjump=false
vspeed=vspeed + 0.5
end
end
end
end
if vspeed <= -12 then
vspeed=-12
end
if vspeed >= 12 then
vspeed=12
end
-----------------------------------Collision Detection:
-------------if floor collision
--if collision(playerX, playerY+1, 32, 32, 0, 256, 480,16) then
--vspeed=0
--end
------------------------------------controls
pad = Controls.read()
if pad:select() then
Music.stop()
break
end
g=3
n=1
if pad:left() then
for n = 1,v do
if collision(playerX-4, playerY,32,31, solidX["n"],solidY["n"], 16,16) == true then----------------This is another piece of code that is problematic
g=2
else
if n == v then
if g == 3 then---------------The following happens all the time, whether it detects a collision or not.
playerX=playerX-4
end
end
end
end
end
g=3
n=1
if pad:right() then
for n = 1,v do
if collision(playerX+4, playerY, 32,31,solidX["n"],solidY["n"], 16,16) == true then
g=2
else
if n == v then
if g == 3 then---------------The following happens all the time, whether it detects a collision or not.
playerX=playerX+4
end
end
end
end
end
if pad:up() then
if canjump == true then
sndjump:play()
vspeed=-10
end
end
prevx=playerX
prevY=playerY
playerY=playerY+vspeed
------------------------------------------Draw Images:
screen:blit(0, 0, board, 0, 0, board:width(), board:height(), false)
-- blit current player animation image
Image:blit(playerX , playerY, orb, 32 * animation, 1, 32, 32,true)
-- calculate next animation image
animationSlowDown = animationSlowDown + 1
if animationSlowDown == 3 then
animationSlowDown = 0
animation = animation + animationDirection
if animation == 6 then
animation = 0
end
end
screen.waitVblankStart()
screen:flip()
end
DtotheK:anything between
is checked every frame, and anything before it is only checked once. have the pad check inside these 2 pieces of code and it'll check every frame and not just the beginning. if you have it in between then just paste your code and we'll take a look at it.Code:while true do
--and
end
