0

The collision of river is not working, its just an if statement where i've written the coordinates of river when the frog collides with those coordinates it should change the state. When frog collides with truck, yellowcar or whitecar it works but when it collides with river(in the if statement i wrote rivers coordinates) it doesn't work and i don't have any type of error.

-- PlayState.lua
PlayState = Class{__includes = BaseState}

function PlayState:init()
    self.frog = Frog()
    self.yellowcars = {}
    self.whitecars = {}
    self.trucks = {}
    self.turtles = {}
    self.timer1 = 0
    self.timer2 = 0
    self.turtleSpawnInterval = 2.5 


    
    self.turtleRows = {
        VIRTUAL_HEIGHT - 160,
        VIRTUAL_HEIGHT - 190,
        VIRTUAL_HEIGHT - 220
    }
    lily = love.graphics.newImage("lily.png")
    river = love.graphics.newImage('graphics/river.png')

    self:spawnCars()
    self:spawnTurtles()
    self.onLilyPad = false
end

function PlayState:update(dt)
    if not self.frog.onTurtle then
        self.frog:update(dt)
    else
        if love.keyboard.wasPressed('up') then
            local currentTurtleIndex = self:getCurrentTurtleIndex()
            local nextTurtleIndex = currentTurtleIndex + 1

            if nextTurtleIndex <= #self.turtles and not self.frog.jumpToThirdTurtle then
                local nextTurtle = self.turtles[nextTurtleIndex]
                self.frog.x = nextTurtle.x
                self.frog.y = nextTurtle.y
            end
        elseif love.keyboard.wasPressed('down') then
            local currentTurtleIndex = self:getCurrentTurtleIndex()
            local prevTurtleIndex = currentTurtleIndex - 1

            if prevTurtleIndex >= 1 and not self.frog.jumpToFirstTurtle then
                local prevTurtle = self.turtles[prevTurtleIndex]
                self.frog.x = prevTurtle.x
                self.frog.y = prevTurtle.y
            end
        end
    end

    self.timer1 = self.timer1 + dt
    if self.timer1 > 2 then
        self:spawnCars()
        self.timer1 = 0
    end

    self.timer2 = self.timer2 + dt
    if self.timer2 > self.turtleSpawnInterval then
        self:spawnTurtles()
        self.turtleSpawnInterval = math.max(2.0, self.turtleSpawnInterval - 0.1)
        self.timer2 = 0
    end

    for k, yellowcar in pairs(self.yellowcars) do
        yellowcar:update(dt)
        if self.frog:collides(yellowcar) then
            stateMachine:change('score')
        end
    end

    if self.frog:collides(riverX, riverY, riverWidth, riverHeight) then
        stateMachine:change('score')
    end

    for k, whitecar in pairs(self.whitecars) do
        whitecar:update(dt)
        if self.frog:collides(whitecar) then
            stateMachine:change('score')
        end
    end


    for k, truck in pairs(self.trucks) do
        truck:update(dt)
        if self.frog:collides(truck) then
            stateMachine:change('score')
        end
    end



    for k, turtle in pairs(self.turtles) do
        turtle:update(dt)
    end

        if self.frog.y < VIRTUAL_HEIGHT - 142 then
        else
            local riverX = 0
            local riverY = 0
            local riverWidth = VIRTUAL_WIDTH
            local riverHeight = VIRTUAL_HEIGHT - 142
            if self.frog:collides(riverX, riverY, riverWidth, riverHeight) then
                stateMachine:change('score')
            end
        end
        



 local onTurtle = false
    for k, turtle in pairs(self.turtles) do
        if self.frog:collides(turtle) then
            onTurtle = true
            if not self.frog.onTurtle then
                self.frog.x = turtle.x
                self.frog.y = turtle.y
                self.frog.onTurtle = true

                self.frog.jumpToFirstTurtle = false
                self.frog.jumpToThirdTurtle = false
            end
        end
    end

    if not onTurtle then
        self.frog.onTurtle = false
    end
end

function PlayState:render()
    love.graphics.clear(20 / 255, 0 / 255, 100 / 255, 0 / 255)

    love.graphics.draw(purplepart, 0, VIRTUAL_HEIGHT - 142)
    love.graphics.draw(river, 0, 0)
    love.graphics.draw(grass, 0, 0)
    love.graphics.draw(lily, VIRTUAL_WIDTH / 2 - lily:getWidth() / 2, 0)
    love.graphics.draw(lily, VIRTUAL_WIDTH / 2 - lily:getWidth() / 2 - 89)
    love.graphics.draw(lily, VIRTUAL_WIDTH / 2 - lily:getWidth() / 2 + 89)
    love.graphics.draw(lily, VIRTUAL_WIDTH / 2 - lily:getWidth() / 2 + 178)
    love.graphics.draw(lily, VIRTUAL_WIDTH / 2 - lily:getWidth() / 2 - 178)

    for k, turtle in pairs(self.turtles) do
        turtle:render()
    end

    self.frog:render()

    for k, yellowcar in pairs(self.yellowcars) do
        yellowcar:render()
    end

    for k, whitecar in pairs(self.whitecars) do
        whitecar:render()
    end

    for k, truck in pairs(self.trucks) do
        truck:render()
    end
end

function PlayState:getCurrentTurtleIndex()
    for i, turtle in ipairs(self.turtles) do
        if self.frog:collides(turtle) then
            return i
        end
    end
    return nil
end

function PlayState:spawnCars()
    local x1, y1 = VIRTUAL_WIDTH - 24, VIRTUAL_HEIGHT - 80
    table.insert(self.yellowcars, Yellowcar(x1, y1))

    local x2, y2 = -10, VIRTUAL_HEIGHT - 100
    table.insert(self.whitecars, Whitecar(x2, y2))

    local x3, y3 = VIRTUAL_WIDTH + 10, VIRTUAL_HEIGHT - 120
    table.insert(self.trucks, Truck(x3, y3))
end

function PlayState:spawnTurtles()
    for i, rowY in ipairs(self.turtleRows) do
        table.insert(self.turtles, Turtle(VIRTUAL_WIDTH + 10, rowY))
    end
end


I tried using coordinates:


        if self.frog.y < VIRTUAL_HEIGHT - 142 then
        else
            local riverX = 0
            local riverY = 0
            local riverWidth = VIRTUAL_WIDTH
            local riverHeight = VIRTUAL_HEIGHT - 142
            if self.frog:collides(riverX, riverY, riverWidth, riverHeight) then
                stateMachine:change('score')
            end
        end
        
2
  • 1
    Please provide a minimal reproducible example. Your code doesn't seem runnable as-is; where are BaseState and Class coming from? Commented Aug 7, 2023 at 12:06
  • 1
    Where is frog.collides? In the first call, the river coordinates appear to be undefined. Commented Aug 7, 2023 at 12:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.