I'm developing a game for quite long now and It's start to get very messy.. Especially with the "beginContact" function..
I want to know if there is any better method to do this :
I have many enemy running around in my level, all the enemy are in "enemyArray[]", and when a enemy touch the wall i want to change his variable of "direction" like this :
Code: Select all
enemyArray[i].direction = -1
I did this but it looks pretty rough and not efficent at all:
Code: Select all
if b:getCategory()==11 and a:getCategory()==3 then
local x,y =b:getBody():getLinearVelocity()
local ind = 0
for i=1,100 do
if enemyArray[i] == nil then else
if enemyArray[i].fixture ==b then
ind = i
end
end
end
enemyArray[ind].direction = -enemyArray[ind].direction
if x>0 then b:getBody():setLinearVelocity(-400,0)
elseif x<0 then b:getBody():setLinearVelocity(400,0)
end
end
Also I still can't figure how to do "If enemyArray != nil then..."
I tryed "!=" ,"not" and "~=" but anything seems to work except doing nothing if is nil and put everything in the "else" part..