Hello everyone! It's me again! Let go to the main problem. I want to make some trees for my game. At first I could only make one tree grow straight, nothing. Then I plan to add leaves. After a while of thinking, I came up with this way. In the tree initialization function, I add the input parameter as leafNum. I divided the length of the tree into 16 parts and assigned the variable averageHeight. If leafNum == 2 then I will query 2 points (I use the coordinates self.x + self.width, averageHeight * 10 to make the tree and leaves look more balanced, this is the only part of the effect, let's comeback), similar to leafNum == 3, == 4, ... (in these cases I'll add the following code) I use the Bump library to check for collisions. I use a rectangular query. If within 2 places I query that, if there is no leaves and the number of leaves has not exceeded the limit then add more leaves (I also plan to make more time between sprays but I have only tested to see if what I have done is successful or not so I have not written down). I thought I would succeed but the problem started from here. Please take a look at this link to know my problem (
https://github.com/kikito/bump.lua#quer ... -rectangle).
Code: Select all
function Plant:update(dt)
--[[local function checkOneFilter(item, other)
if other.isLeaf then return 'cross' end
end]]--
local items, len = world:queryRect(self.width + self.x, self.averageHeight * 10, 30, 30)
local count = world:countItems()
for i = 1, count do
local other = items[i].other
if other.isLeaf == false and self.totalLeaf < self.leafNum then
table.insert(listOfLeaf, Leaf(self.x + self.width, self.averageHeight * 10, 7.5, 4, 30, 16))
end
print("a")
end
........
At first, I followed the instructions from the link I just wrote above. I let loop i run from 1 to len (len is the length of items, items is a table). But when I do that, it goes like this, because the rectangle I query does not collide with any object, the items become an empty table, and in my opinion so len will be equal to 0, the loop do not run. I tried inserting print ("a") and it was true. Then I use another method, which is also in the other link, scroll down a bit and you'll see. I declare the local variable count as the number of all objects in the world and run the loop i from 1 to there. Then the error kept happening, it said Error: plant.lua: 30: attempt to index a nil value (in items
.other) (I will attach my file below for your convenience). I used a guard (if items .other ~ = nil then ....) but it still says plant.lua: 30: attempt to index a nil value. I was bewildered not knowing what to do. I've made a little progress in making games, this project is better than my bad projects before, I don't want to have to stop here. Hope you could take some of your valuable time to help me. How to fix that? Is there a better way to create my leaves? Hope you can help me as soon as possible, I'm too eager to continue my work. Thank you very much. If you need any further information on the issue I'm having, just ask me and I'll give you more. This is my .love file. Specific problems in plant.lua.