Log In  
Log In  

Hello, i'm rather new to this and I am having issues firing a projectile when I press the z key. This is in a simple game I am making. My code is below:

x = 64  y = 64 
    function _update()
     if (btn(0)) then x=x-1 end
     if (btn(1)) then x=x+1 end
     if (btn(2)) then y=y-1 end
     if (btn(3)) then y=y+1 end
     if (btn(4)) then shoot(x,y)
 end
    end 
    function _draw()    
     circfill(x,y,7,8)

    end

    function shoot(bullx,bully)
    local x = bullx
    local y = bully
    local k = y
    while k <= 128
    do
    k = k + 1

 end
 end

Currently It just flys off the screen super quick, how would i slow it down?

P#10915 2015-05-25 00:26 ( Edited 2015-05-26 03:09)

:: 23

Where do you draw the bullet? The only drawing call I see is "circfill", which I guess is the player?

When you fire the bullet, you're using a while loop, but that means that the bullet will move all the way off the screen in one frame. You need to update the bullet position each frame (just like the character).

P#10916 2015-05-25 01:13 ( Edited 2015-05-25 05:13)

I think you want it to look something more like this:

player={x=64,y=120)
shots={}

function _update()
 if btn(0) then
  player.x-=1
 end
 if btn(1) then
  player.x+=1
 end
 if btn(4) then
  add(shots, {x=player.x, y=player.y})
 end
 for s in all(shots) do
  s.y-=1
 end
end

function _draw()
 cls()
 spr(0, player.x, player.y)
 for s in all(shots) do
  spr(1, s.x, s.y)
 end
end

Note: I have not run this code. It may have bugs. (It may be illustrative, even if it has bugs.) If you want something that definitely works, a better course of action is to open up a cart (that works) and look at the code.

P#10918 2015-05-25 10:16 ( Edited 2015-05-25 14:18)

Thanks For The Help, John, your example helped me a lot. Im not used to this development platform, and it is different from what i usually do(c# in visual studio). I understand where i went wrong now.

P#10930 2015-05-25 23:09 ( Edited 2015-05-26 03:09)

[Please log in to post a comment]

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-04-19 18:21 | 0.014s | 2097k | Q:19