Page 1 of 1

Bullet system does not work [SOLVED!!!]

Posted: Tue Nov 01, 2022 3:53 pm
by EvolvedMonke
hi, i am making a game about a circle that goes around your screen and you can control it with your mouse, so i made a bullet system but it does not work also the bullet disappears if i don't press the button, can someone help me?

Code: Select all



Bullet = {}


function Bullet:load()
    self.x = love.graphics.getWidth() / 2
    self.y = love.graphics.getHeight() / 2
    self.width = 20
    self.height = 20
    self.speed = 300
    self.xVel = 0
    self.yVel = 0
    self.visible = 0
end


function Bullet:update(dt)
    local x, y = love.mouse.getPosition() -- get the position of the mouse
    local y, x = love.mouse.getPosition() -- get the position of the mouse
    self.x = y
    self.y = x

    ----------------------------------------------- start of bullet system
    if love.keyboard.isDown("a") then
        self.visible = 1
        self.bulletleft = 1

        self.xVel = self.xVel + self.speed
        self.x = self.x - self.xVel * dt

    elseif love.keyboard.isDown("w") then
        self.visible = 1
        self.bulletup = 2
        
        self.yVel = self.yVel + self.speed
        self.y = self.y + self.yVel * dt

    elseif love.keyboard.isDown("d") then
        self.visible = 1
        self.bulletright = 3

        self.xVel = self.xVel - self.speed
        self.x = self.x + self.xVel * dt

    elseif love.keyboard.isDown("s") then
        self.visible = 1
        self.bulletdown = 4

        self.yVel = self.yVel - self.speed
        self.y = self.y - self.yVel * dt

    else 
        self.x = y
        self.y = x
        self.visible = 0
    end
    ----------------------------------------------- end of bullet system
end


function Bullet:draw()
    if self.visible == 1 then
        love.graphics.rectangle("fill", self.x, self.y, 10, 50)
    end
end
(with "a" the bullet should go left, "s" down, "w" up and with "d" right but everytime it goes where he wants like i can't control it)

Re: Bullet system does not work

Posted: Tue Nov 01, 2022 5:14 pm
by Bigfoot71
Because you wrote in your code, roughly: "IF press D then visible and move ELSE IF press S then visible and move .... ELSE not visible".
So if you do not press any key it is no longer visible and does not move.

There are other problems but I'll let you compare with what I did, this example works even if it could be better written:

Code: Select all

Bullet = {}


function Bullet:load()

    self.x = 0 -- defined after
    self.y = 0 -- defined after

    self.width = 20
    self.height = 20

    self.speed = 300
    self.xVel = 0 -- 1 : right | -1 : left
    self.yVel = 0 -- 1 : down  | -1 : up

    self.visible = false

end


function Bullet:update(dt)

    ----------------------------------------------- start of bullet system
    
    if not self.visible then
    
        self.x, self.y = love.mouse.getPosition()

        if love.keyboard.isDown("a") then
            self.visible = true
            self.xVel = -1

        elseif love.keyboard.isDown("w") then
            self.visible = true
            self.yVel = -1

        elseif love.keyboard.isDown("d") then
            self.visible = true
            self.xVel = 1

        elseif love.keyboard.isDown("s") then
            self.visible = true
            self.yVel = 1
        end

    else -- in movement

        self.x = self.x + self.xVel * self.speed * dt
        self.y = self.y + self.yVel * self.speed * dt
        
        -- If outside the screen --

        if self.x > love.graphics.getWidth()
        or self.x+self.width < 0
        or self.y > love.graphics.getHeight()
        or self.y+self.height < 0 then
            self.xVel, self.yVel = 0, 0
            self.visible = false
        end

    end

    ----------------------------------------------- end of bullet system
end


function Bullet:draw()
    if self.visible then
        love.graphics.rectangle("fill", self.x, self.y, 10, 50)
    end
end

Re: Bullet system does not work

Posted: Tue Nov 01, 2022 6:40 pm
by EvolvedMonke
thank you fo the support!