I am a newbie.I want to make a simple coin .where ball can have coins.
_G.love = require("love")
function love.load()
w, h = love.graphics.getWidth(), love.graphics.getHeight()
local wf = require("lib.windfield.windfield")
world = wf.newWorld(0, 0, true)
world:setGravity(0, 100)
-- Create the ground
ground = world:newRectangleCollider(0, h - 100, 500, 200)
ground:setType("static")
ball = world:newCircleCollider(100, 0, 20)
coins = {}
score = 0
-- Create initial coins
createCoin(300, h - 150)
createCoin(600, h - 150)
end
function createCoin(x, y)
local coin = world:newCircleCollider(x, y, 10, { "Coin" })
coin.type = "coin"
table.insert(coins, coin)
end
function love.update(dt)
world:update(dt)
local ball_x, ball_y = ball:getPosition()
for i = #coins, 1, -1 do
local coin = coins
if coin:enter("ball") then
-- Coin has been collected
coin:destroy()
table.remove(coins, i)
score = score + 1
end
end
if love.keyboard.isDown("s") then
ball:applyForce(0, 50000)
end
if love.keyboard.isDown("d") then
ball:setLinearVelocity(100, ball_y)
end
if love.keyboard.isDown("w") then
ball:setLinearVelocity(0, -100)
end
end
function love.draw()
world:draw()
love.graphics.print("Score: " .. score, 10, 10)
end
windfield physics help for enter and destroy method
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 12
- Joined: Sat Oct 21, 2023 1:33 pm
- ausboss20001
- Prole
- Posts: 4
- Joined: Sat Nov 18, 2023 5:16 pm
Re: windfield physics help for enter and destroy method
whats the issue youre facing
-
- Prole
- Posts: 8
- Joined: Tue Nov 14, 2023 4:01 pm
Re: windfield physics help for enter and destroy method
Correct this 'for' cycle by adding :
Code: Select all
for i = #coins, 1, -1 do
local coin = coins[i]
if coin:enter("ball") then
-- Coin has been collected
coin:destroy()
table.remove(coins, i)
score = score + 1
end
end
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 1 guest