Log In  

Im not sure what is wrong, but whenever I press Z it never shoots

TAB 0

function _init()
init_objects()

-- set up craft
px=63
py=100
pspr=1
vx=0
thrust=0.1
f=0.97
t_spr=17

-- set up stars
map1y=0
map2y=0
map3y=0
map1_spd=1
map2_spd=0.5
map3_spd=0.25
map_height=128
end

function _update()
--scroll map
map1y+=map1_spd
map2y+=map2_spd
map3y+=map3_spd
if map1y>127 then map1y=0 end
if map2y>127 then map2y=0 end
if map3y>127 then map3y=0 end

-- get input and change sprite
-- and thrust
pspr=1
if btn(0) then
vx-=thrust
pspr=3
end
if btn(1) then
vx+=thrust
pspr=2
end

-- apply thrust and friction
vx*=f
px+=vx

--check edges
if px<0 then
px=0
vx=0
end
if px>120 then
px=120
vx=0
end

-- change thrust sprite
t_spr+=1
if t_spr>19 then t_spr=17 end

--fire bullet
if btn(2) then
add_new_bullet(px,py,0,-3)
end

--draw bullet
for b in all(bullets) do
b:update()
end

end

function _draw()
cls()

-- draw stars
map(0,0,0,map1y,16,16)
map(16,0,0,map2y,16,16)
map(32,0,0,map3y,16,16)
map(0,0,0,map1y-map_height,16,16)
map(16,0,0,map2y-map_height,16,16)
map(32,0,0,map3y-map_height,16,16)

spr(pspr,px,py)
spr(t_spr,px,py+8)

--draw bullet
print(#bullets,1,2)
for b in all(bullets) do

b:draw()
end

end

TAB 1

--objects

function init_objects()
--declare objects
bullets={}

end

function add_new_bullet(_x,_y,_dx,_dy)
add(bullets,{
x=_x,
y=_y,
dx=_dx,
dy=_dy,
life=20,
draw=function(self)
circfill(self.x,self.y,2,11)
end,
update=function(self)
self.x=self.dx
self.y=self.dy
self.life-=1
if self.live<0 then
del(bullets,self)
end
end
})
end

P#72912 2020-02-08 16:58 ( Edited 2020-02-08 16:59)

does it shoot when you hit [↑]? it looks like the code checks btn(2), but the button codes are

0: [←]
1: [→]
2: [↑]
3: [↓]
4: [o] (usually z or c on the keyboard)
5: [x] (usually x on the keyboard)

so btn(4) might be what you want to check

(also, that'll shoot every frame while the button is held down - idk if you want that or if you want to insert some kind of delay)

P#72940 2020-02-09 20:24

I dont really care if it shoots every frame or not right now. I've changed it and it still doesnt work. I've changed it to btn(4) but it still doesnt work. Whenever I press z or c it just pauses the game.

P#72953 2020-02-10 03:14
1

There are a few typos in bullet:update(): '=' instead of '+=' and 'live' instead of 'life'. After fixing those, it seems to be working fine for me.

    update=function(self)
        self.x+=self.dx
        self.y+=self.dy
        self.life-=1
        if self.life<0 then
            del(bullets,self)
        end
    end
P#72974 2020-02-10 21:35

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 15:20:38 | 0.006s | Q:14