如何改进Pico-8 Collision?

问题描述 投票:0回答:1

错误:尝试索引全局“W”(零值)

您好!我在使用从地图屏幕生成精灵的不同系统时遇到碰撞问题。

这是我使用的碰撞教程:https://youtu.be/Recf5_RJbZI?si=Df6FbJ2FYfCN39Qx 这是我使用的精灵生成教程:https://youtu.be/8jb8SHNS66c?si=233nn8z_S1R4R64n

我已经看过大量这方面的教程,但仍然无法理解它。许多教程创建了非常基本的碰撞,但存在一些问题,例如只能使用某些速度值(1、2、4、8、16 等),或者在按住时不允许角色沿着墙壁滑动一条对角线。

有人可以帮忙吗?我绝对想在将来使用这个生成系统来生成不同类型的墙壁和敌人。我有一种感觉,我误解了如何正确使用表变量,所以任何解释将不胜感激!

-- game loop --

function _init()
    cls()
    walls={}
    make_player()
    make_walls()
    -- top-left and lower-right
    -- bounds of player area
    a1,b1=8,8
    a2,b2=112,112
end

function _update60()
    -- keep inside the play area
    move_player()
    p.x=mid(a1,p.x,a2)
    p.y=mid(b1,p.y,b2)
end

function _draw()
    cls()
    draw_map()
    draw_player()
    
    for w in all(walls) do
        spr(w.wsp,w.wx,w.wy)
    end 
end
-- map --

function draw_map()
    map(0,0,0,0,16,16)
end
-- player --

function make_player()
    p={
    x=40,
    y=40,
    w=8,
    h=8,
    speed=2,
    sprite=1,
    }
end

function move_player()
    --move player with buttons
    --interacts with wall collision
    if (btn(⬅️)) then
        for newx=p.x,p.x-p.speed,-1 
            do
                if not box_hit(newx,p.y,
                    p.w,p.h,
                    w.wx,w.wy,
                    w.ww,w.wh) 
                then
                    p.x=newx
            end
        end
    end
        
        if (btn(➡️)) then
        for newx=p.x,p.x+p.speed 
            do
                if not box_hit(newx,p.y,
                        p.w,p.h,
                        w.wx,w.wy,
                        w.ww,w.wh) 
                then
                    p.x=newx
            end
        end
    end
        
        if (btn(⬆️)) then
        for newy=p.y,p.y-p.speed,-1 
            do
                if not box_hit(p.x,newy,
                        p.w,p.h,
                        w.wx,w.wy,
                        w.ww,w.wh) 
                then
                    p.y=newy
            end
        end
    end
        
        if (btn(⬇️)) then
        for newy=p.y,p.y+p.speed 
            do
                if not box_hit(p.x,newy,
                        p.w,p.h,
                        w.wx,w.wy,
                        w.ww,w.wh) 
                then
                    p.y=newy
            end
        end
    end
end

--draw player
function draw_player()
    spr(p.sprite,p.x,p.y)
end
-- walls --

function make_walls()
    for x=0,15 do
        for y=0,15 do
            if mget(x,y)==65 then
                add(walls,{
                    wx=x*8,
                    wy=y*8,
                    ww=8,
                    wh=8,
                    wsp=66
                })
                mset(x,y,64)
            end
        end
    end
end

--wall collision calculations

function box_hit(x1,y1,
        w1,h1,
        x2,y2,
        w2,h2)

        local hit=false
        local xd=abs((x1+(w1/2))-(x2+w2/2))
        local xs=w1/2+w2/2
        local yd=abs((y1+(h1/2))-(y2+h2/2))
        local ys=h1/2+h2/2  
    
    if xd < xs and yd < ys then
        hit=true
    end
    
    return hit
    
end
lua collision-detection pico-8
1个回答
0
投票

在许多较旧的游戏中,使用了一种称为子像素的系统。他们会在小于实际像素的比例上计算碰撞(例如,0.1 像素而不是 1 像素),并且在绘制时,他们会简单地使用 x 和 y 值的下限。这可以提高精度和平滑度。 本教程系列有很多很好的建议,尽管它适用于另一个平台,但我仍然会返回以寻求帮助:https://www.youtube.com/playlist?list=PLy4zsTUHwGJIc90UaTKd-wpIH12FCSoLh 这篇文章似乎也非常有用,尽管我还没有通读全文:http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/

© www.soinside.com 2019 - 2024. All rights reserved.