[lua]
_W = display.contentWidth;
_H = display.contentHeight;
db = true;
local physics = require(“physics”);
physics.start();
physics.setGravity(0,0);
box = display.newImage(“background.jpg”, 10, _H/2);
box.xScale = .2; box.yScale = .2;
box.ID = “Box”;
physics.addBody(box, “static”);
function spawn()
circle = display.newCircle(10, 10, 20);
circle.ID = “Circle”;
circle:setFillColor(math.random(0,1), 0 ,0);
physics.addBody(circle);
circle.gravityScale = 0;
circle.Rotation = true;
circle.isSensor = true;
end
spawn();
function move()
if (db == true) then
transition.to(circle,{time = 2000, x = math.random(10, 300), y = math.random(10, 500)
, onComplete = move});
end
end
move();
function moveIt(mouse)
if (mouse.phase == “began”)then
circle.oldX = circle.x;
circle.oldY = circle.y;
display.getCurrentStage():setFocus(circle);
circle.hasFocus = true;
elseif (circle.hasFocus) then
if (mouse.phase == “moved”) then
circle.x = (mouse.x - mouse.xStart) + circle.oldX;
circle.y = (mouse.y - mouse.yStart) + circle.oldY;
db = false;
elseif (mouse.phase == “ended”) then
db = true;
move();
display.getCurrentStage():setFocus(nil);
circle.hasFocus = false;
end
end
end
circle:addEventListener(“touch”, moveIt);
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
if (self.ID == “Circle” and event.other.ID == “Box”) then
print(“aya”);
end
print( self.ID … ": collision began with " … event.other.ID );
end
end
-----------------------------------------------------------------------------------------
circle.collision = onLocalCollision;
circle:addEventListener( “collision”, circle);
[/lua]
The part that isn’t working is the collision detection between the circle and image. (Even though it’s saved as “background.jpg” it only takes a little bit of the screen). The collision is being detected when I drag the circle off of the screen and then when it enters the screen. (it prints “Circle: collision began with Box” when I drag it back on the screen). Why is it doing this and not when it collides with box? Also, it doesn’t print aya when the collision begins if that helps.