Object not dynamic after remaking it.

Hey guys,

i got a small problem. I got a small game im making and i am stuck on something, first ill explain the game a bit:

In my game I want to have a random Food type spawn which the player has to drag to the right animal(banana’s go to the gorilla). Now I want to spawn a new food type everytime the right food goes to the right animal. 

I got 5 animals and if they have collision with the right food the score will be updated and the food will de removed.

Now the problems lies in the spawning of the second food, i got it spawning but i cant drag it anymore and its not dynamic when i spawn it again. I noticed it is not possible to add a physics body during a collision check, but where should i put the add.physics body so it adds a physics body event to the second spawned object?

This is my code:

[lua]–hide status var from the beginning
display.setStatusBar( display.HiddenStatusBar)

–start physics
local physics = require(“physics”)
physics.start()

score = 0

_W = display.contentWidth; – get the width of the screen
_H = display.contentHeight; – get the Height of the screen

displayGroupBG = display.newGroup()
displayGroupAnimals = display.newGroup()
displayGroupFood = display.newGroup()
displayGroupGUI = display.newGroup()

local Background = display.newImage (displayGroupBG, “Assets/Background.png”, true)
Background.x = _W/2; Background.y = _H/2;

–init animal png’s

–Animal 1 = Gorilla
–Animal 2 = Giraffe
–Animal 3 = Penguin
–Animal 4 = Snake
–Animal 5 = Tiger

for i=1,5,1 do

if i == 1 then
Animal = display.newImage(displayGroupAnimals, “Assets/Gorilla.png”, true)
Animal.type = “Gorilla”
end

if i == 2 then
Animal = display.newImage(displayGroupAnimals, “Assets/Giraffe.png”, true)
Animal.type = “Giraffe”
end

if i == 3 then
Animal = display.newImage(displayGroupAnimals, “Assets/Penguin.png”, true)
Animal.type = “Penguin”
end

if i == 4 then
Animal = display.newImage(displayGroupAnimals, “Assets/Snake.png”, true)
Animal.type = “Snake”
end

if i == 5 then
Animal = display.newImage(displayGroupAnimals, “Assets/Tiger.png”, true)
Animal.type = “Tiger”
end

if i < 4 then
Animal.x = 75 * i
Animal.y = 400
end

if i > 3 then
Animal.x = 75 * i - 200
Animal.y = 475
end
physics.addBody ( Animal, “static”, { friction=0.5, bounce= 0.3} )
end

local function CreateFood()

– load in food types

RFood = math.random(5)

–RFood 1 = Banana
–RFood 2 = Leaves
–RFood 3 = Fish
–RFood 4 = Mouse
–RFood 5 = Meat

if RFood == 1 then
Food = display.newImage (displayGroupFood, “Assets/Banana.png”, true)
Food.type = “Banana”
end

if RFood == 2 then
Food = display.newImage (displayGroupFood, “Assets/Leaves.png”, true)
Food.type = “Leaves”
end

if RFood == 3 then
Food = display.newImage (displayGroupFood, “Assets/Fish.png”, true)
Food.type = “Fish”
end

if RFood == 4 then
Food = display.newImage (displayGroupFood, “Assets/Mouse.png”, true)
Food.type = “Mouse”
end

if RFood == 5 then
Food = display.newImage (displayGroupFood, “Assets/Meat.png”, true)
Food.type = “Meat”
end

return Food

end

CreateFood()
CreateFood()
CreateFood()

local function FoodCollision(self, event)
if event.phase == “began” then
if event.target.type == “Banana” and event.other.type == “Gorilla” then
print(“Gorilla X Banana”)
Feeding = true
print(Feeding)

end
if event.target.type == “Leaves” and event.other.type == “Giraffe” then
print(“Giraffe X Leaves”)
Feeding = true
print(Feeding)

end
if event.target.type == “Fish” and event.other.type == “Penguin” then
print(“Penguin X Fish”)
Feeding = true
print(Feeding)

end
if event.target.type == “Mouse” and event.other.type == “Snake” then
print(“Snake X Mouse”)
Feeding = true
print(Feeding)

end
if event.target.type == “Meat” and event.other.type == “Tiger” then
print(“Tiger X Meat”)
Feeding = true
print(Feeding)

end

end

if Feeding == true then
score = score + 100
Feeding = false
event.target:removeSelf()
event.target = nil;
CreateFood()
Food.x = _W / 2
Food.y = 25
Food.collision = FoodCollision
Food:addEventListener(“collision”, Food)
physics.addBody ( Food, “dynamic”, { friction=0.5, bounce =0.3} )
print(Feeding)
print(score)
end
end;

local function SetFood()

Food.x = _W / 2
Food.y = 25
Food.collision = FoodCollision
Food:addEventListener(“collision”, Food)
physics.addBody ( Food, “dynamic”, { friction=0.5, bounce =0.3} )

print(“created food”)

end

SetFood()

function Food:touch(event)
if event.phase == “began” then

self.markX = self.x – store x location of object
self.markY = self.y – store y location of object

elseif event.phase == “moved” then

local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY

self.x, self.y = x, y – move objects based on calculations above
end

return true

end

Food:addEventListener (“touch”, Food)
[/lua]

Just suggestions:
-use local Animal not just Animal
-the same for RFood
-if you want to do something physic related on collision then you can but you must perform it with slight delay (timer.performWithDelay)
-you can make Animal.isSensor = true and it will be transparent for physics but will signal collisions

Why not add the code from SetFood() inside the CreateFood() function?  i.e. for every food object, don’t you want them all to be added to the physics environment?

And as piotrz55 suggests, you should use  “local Animal = …” and “local Food = …” otherwise you’re overwriting one global object with the new image.

hey guys, thanks for the replies.

I put the SetFood function with my createFood function but that also means that i have to put it bellow my collision detection wich is fine but then my  if feeding wont know what to do. 

Right now the food stil makes collision with the animal. Tough my Feeding variable is no longer recognized out side the collision function, but to recognize the createFood function the Feeding if has to be below the createFood function which is below my onCollision! how do i get my if Feeding to recognize the Feeding boolean in my onCollision?

this is my code right now(is it possible to hide it, so it isnt a big mess on here?):

[lua]–hide status var from the beginning
display.setStatusBar( display.HiddenStatusBar)

–start physics
local physics = require(“physics”)
physics.start()

score = 0

_W = display.contentWidth; – get the width of the screen
_H = display.contentHeight; – get the Height of the screen

displayGroupBG = display.newGroup()
displayGroupAnimals = display.newGroup()
displayGroupFood = display.newGroup()
displayGroupGUI = display.newGroup()

local Background = display.newImage (displayGroupBG, “Assets/Background.png”, true)
Background.x = _W/2; Background.y = _H/2;

–init animal png’s

–Animal 1 = Gorilla
–Animal 2 = Giraffe
–Animal 3 = Penguin
–Animal 4 = Snake
–Animal 5 = Tiger

for i=1,5,1 do

local Animal

if i == 1 then
Animal = display.newImage(displayGroupAnimals, “Assets/Gorilla.png”, true)
Animal.type = “Gorilla”
end

if i == 2 then
Animal = display.newImage(displayGroupAnimals, “Assets/Giraffe.png”, true)
Animal.type = “Giraffe”
end

if i == 3 then
Animal = display.newImage(displayGroupAnimals, “Assets/Penguin.png”, true)
Animal.type = “Penguin”
end

if i == 4 then
Animal = display.newImage(displayGroupAnimals, “Assets/Snake.png”, true)
Animal.type = “Snake”
end

if i == 5 then
Animal = display.newImage(displayGroupAnimals, “Assets/Tiger.png”, true)
Animal.type = “Tiger”
end

if i < 4 then
Animal.x = 75 * i
Animal.y = 400
end

if i > 3 then
Animal.x = 75 * i - 200
Animal.y = 475
end
physics.addBody ( Animal, “static”, { friction=0.5, bounce= 0.3} )
end

local function FoodCollision(self, event)
if event.phase == “began” then
if event.target.type == “Banana” and event.other.type == “Gorilla” then
print(“Gorilla X Banana”)
Feeding = true
print(Feeding)

end
if event.target.type == “Leaves” and event.other.type == “Giraffe” then
print(“Giraffe X Leaves”)
Feeding = true
print(Feeding)

end
if event.target.type == “Fish” and event.other.type == “Penguin” then
print(“Penguin X Fish”)
Feeding = true
print(Feeding)

end
if event.target.type == “Mouse” and event.other.type == “Snake” then
print(“Snake X Mouse”)
Feeding = true
print(Feeding)

end
if event.target.type == “Meat” and event.other.type == “Tiger” then
print(“Tiger X Meat”)
Feeding = true
print(Feeding)

end

end

end;

local function CreateFood()

– load in food types

local RFood = math.random(5)

–RFood 1 = Banana
–RFood 2 = Leaves
–RFood 3 = Fish
–RFood 4 = Mouse
–RFood 5 = Meat

if RFood == 1 then
Food = display.newImage (displayGroupFood, “Assets/Banana.png”, true)
Food.type = “Banana”
end

if RFood == 2 then
Food = display.newImage (displayGroupFood, “Assets/Leaves.png”, true)
Food.type = “Leaves”
end

if RFood == 3 then
Food = display.newImage (displayGroupFood, “Assets/Fish.png”, true)
Food.type = “Fish”
end

if RFood == 4 then
Food = display.newImage (displayGroupFood, “Assets/Mouse.png”, true)
Food.type = “Mouse”
end

if RFood == 5 then
Food = display.newImage (displayGroupFood, “Assets/Meat.png”, true)
Food.type = “Meat”
end

Food.x = _W / 2
Food.y = 25
Food.collision = FoodCollision
Food:addEventListener(“collision”, Food)
physics.addBody ( Food, “dynamic”, { friction=0.5, bounce =0.3} )

return Food

end

CreateFood()
local function CheckFeeding()

local Feeding

print(“feeding checked”)

if Feeding == true then
print(“feeding checked true”)
timer.performWithDelay( 10, CreateFood )
print(Feeding)
print(score)
score = score + 100
Feeding = false
event.target:removeSelf()
event.target = nil;

else

print(“feeding checked false”)

end

end

CheckFeeding()

function Food:touch(event)
if event.phase == “began” then

self.markX = self.x – store x location of object
self.markY = self.y – store y location of object

elseif event.phase == “moved” then

local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY

self.x, self.y = x, y – move objects based on calculations above
end

return true

end

Food:addEventListener (“touch”, Food)[/lua]

Just suggestions:
-use local Animal not just Animal
-the same for RFood
-if you want to do something physic related on collision then you can but you must perform it with slight delay (timer.performWithDelay)
-you can make Animal.isSensor = true and it will be transparent for physics but will signal collisions

Why not add the code from SetFood() inside the CreateFood() function?  i.e. for every food object, don’t you want them all to be added to the physics environment?

And as piotrz55 suggests, you should use  “local Animal = …” and “local Food = …” otherwise you’re overwriting one global object with the new image.

hey guys, thanks for the replies.

I put the SetFood function with my createFood function but that also means that i have to put it bellow my collision detection wich is fine but then my  if feeding wont know what to do. 

Right now the food stil makes collision with the animal. Tough my Feeding variable is no longer recognized out side the collision function, but to recognize the createFood function the Feeding if has to be below the createFood function which is below my onCollision! how do i get my if Feeding to recognize the Feeding boolean in my onCollision?

this is my code right now(is it possible to hide it, so it isnt a big mess on here?):

[lua]–hide status var from the beginning
display.setStatusBar( display.HiddenStatusBar)

–start physics
local physics = require(“physics”)
physics.start()

score = 0

_W = display.contentWidth; – get the width of the screen
_H = display.contentHeight; – get the Height of the screen

displayGroupBG = display.newGroup()
displayGroupAnimals = display.newGroup()
displayGroupFood = display.newGroup()
displayGroupGUI = display.newGroup()

local Background = display.newImage (displayGroupBG, “Assets/Background.png”, true)
Background.x = _W/2; Background.y = _H/2;

–init animal png’s

–Animal 1 = Gorilla
–Animal 2 = Giraffe
–Animal 3 = Penguin
–Animal 4 = Snake
–Animal 5 = Tiger

for i=1,5,1 do

local Animal

if i == 1 then
Animal = display.newImage(displayGroupAnimals, “Assets/Gorilla.png”, true)
Animal.type = “Gorilla”
end

if i == 2 then
Animal = display.newImage(displayGroupAnimals, “Assets/Giraffe.png”, true)
Animal.type = “Giraffe”
end

if i == 3 then
Animal = display.newImage(displayGroupAnimals, “Assets/Penguin.png”, true)
Animal.type = “Penguin”
end

if i == 4 then
Animal = display.newImage(displayGroupAnimals, “Assets/Snake.png”, true)
Animal.type = “Snake”
end

if i == 5 then
Animal = display.newImage(displayGroupAnimals, “Assets/Tiger.png”, true)
Animal.type = “Tiger”
end

if i < 4 then
Animal.x = 75 * i
Animal.y = 400
end

if i > 3 then
Animal.x = 75 * i - 200
Animal.y = 475
end
physics.addBody ( Animal, “static”, { friction=0.5, bounce= 0.3} )
end

local function FoodCollision(self, event)
if event.phase == “began” then
if event.target.type == “Banana” and event.other.type == “Gorilla” then
print(“Gorilla X Banana”)
Feeding = true
print(Feeding)

end
if event.target.type == “Leaves” and event.other.type == “Giraffe” then
print(“Giraffe X Leaves”)
Feeding = true
print(Feeding)

end
if event.target.type == “Fish” and event.other.type == “Penguin” then
print(“Penguin X Fish”)
Feeding = true
print(Feeding)

end
if event.target.type == “Mouse” and event.other.type == “Snake” then
print(“Snake X Mouse”)
Feeding = true
print(Feeding)

end
if event.target.type == “Meat” and event.other.type == “Tiger” then
print(“Tiger X Meat”)
Feeding = true
print(Feeding)

end

end

end;

local function CreateFood()

– load in food types

local RFood = math.random(5)

–RFood 1 = Banana
–RFood 2 = Leaves
–RFood 3 = Fish
–RFood 4 = Mouse
–RFood 5 = Meat

if RFood == 1 then
Food = display.newImage (displayGroupFood, “Assets/Banana.png”, true)
Food.type = “Banana”
end

if RFood == 2 then
Food = display.newImage (displayGroupFood, “Assets/Leaves.png”, true)
Food.type = “Leaves”
end

if RFood == 3 then
Food = display.newImage (displayGroupFood, “Assets/Fish.png”, true)
Food.type = “Fish”
end

if RFood == 4 then
Food = display.newImage (displayGroupFood, “Assets/Mouse.png”, true)
Food.type = “Mouse”
end

if RFood == 5 then
Food = display.newImage (displayGroupFood, “Assets/Meat.png”, true)
Food.type = “Meat”
end

Food.x = _W / 2
Food.y = 25
Food.collision = FoodCollision
Food:addEventListener(“collision”, Food)
physics.addBody ( Food, “dynamic”, { friction=0.5, bounce =0.3} )

return Food

end

CreateFood()
local function CheckFeeding()

local Feeding

print(“feeding checked”)

if Feeding == true then
print(“feeding checked true”)
timer.performWithDelay( 10, CreateFood )
print(Feeding)
print(score)
score = score + 100
Feeding = false
event.target:removeSelf()
event.target = nil;

else

print(“feeding checked false”)

end

end

CheckFeeding()

function Food:touch(event)
if event.phase == “began” then

self.markX = self.x – store x location of object
self.markY = self.y – store y location of object

elseif event.phase == “moved” then

local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY

self.x, self.y = x, y – move objects based on calculations above
end

return true

end

Food:addEventListener (“touch”, Food)[/lua]