Hi,
I have a method that creates a simple object (a food), I call this function two times and give it two positions and it creates those two foods for me at those places, adds them to physic and I have no problem touching and dragging them.
Thing is, at some point I want to generate some more of these foods, so I call this “makeOneFoodObject” code and give it position, but guess what, it creates the display object, shows it’s texture into it but it doesn’t have any physic body attached to it. And I confirm this because I put display in hybrid mode and I see physic objects.
It’s strange because I’m creating the food objects same way that I do in my main function.
[lua]local function onPurpleFoodCollision(self, event)
if ( event.phase == “began” ) then
if (event.other.myName == “PurpleMonster”) then
print( self.myName … ": collision ended with " … event.other.myName )
makeOneFoodObject(150, 150);
elseif (event.other.myName == “YellowMonster”) then
makeOneFoodObject(150, 150);
end
elseif ( event.phase == “ended” ) then
–print( self.myName … ": collision ended with " … event.other.myName )
end
end
function makeOneFoodObject(x,y)
– foodElement = { friction=0.4, bounce=0.2, filter=borderCollisionFilter }
– local purpleFoodCollisionFilter = { categoryBits = 2, maskBits = 3 } – collides with (2 & 1) only
local purpleFoodCollisionFilter = { categoryBits = 2, maskBits = 1 } – collides with (1) only
local purpleFoodPhysicSettings = { density=0.2, friction=0, bounce=0.95, radius=20.0, filter=purpleFoodCollisionFilter }
– local yellowFoodCollisionFilter = { categoryBits = 4, maskBits = 5 } – collides with (4 & 1) only
– local yellowFoodPhysicSettings = { density=0.2, friction=0, bounce=0.95, radius=23.0, filter=yellowFoodCollisionFilter }
local foodObject = display.newImageRect(“food_purple.png”, 25, 33);
foodObject:setReferencePoint(display.TopLeftReferencePoint);
foodObject.myName = “PurpleFoodObject”;
– foodObject.x = display.contentCenterX;
– foodObject.y = display.contentCenterY;
foodObject.x = x;
foodObject.y = y;
foodObject:setReferencePoint(display.CenterReferencePoint); --we need to revert reference point to center physic will move it by center.
foodObject:addEventListener(“touch”, localDrag);
foodObject.collision = onPurpleFoodCollision;
foodObject:addEventListener(“collision”, monster_yellow)
physics.addBody( foodObject, “dynamic”, purpleFoodPhysicSettings)
end
function main()
initializeGame();
makeOneFoodObject(150, 150);
makeOneFoodObject(250, 150);
makeOneFoodObject(250, 180);
end[/lua] [import]uid: 206803 topic_id: 34449 reply_id: 334449[/import]