我想让精灵图像与硬币发生碰撞。 硬币是一个图像,它总是因为这段代码而移动:
local tPrevious = system.getTimer();
local function move(event)
local tDelta = event.time - tPrevious;
tPrevious = event.time;
local xOffset = (0.3 * tDelta );
grass.x = grass.x - xOffset;
grass2.x = grass2.x - xOffset;
coin.x = coin.x - xOffset;
if (grass.x + grass.contentWidth) < 0 then
grass:translate( 480 * 2, 0);
end
if (grass2.x + grass2.contentWidth) < 0 then
grass2:translate( 480 * 2, 0);
end
if (coin.x + coin.contentWidth) < 0 then
coin:translate( 480 * 2, 0);
coinRect.x = coin.x
end
local i;
end
有谁知道我如何与硬币图像发生碰撞?
提前致谢。
答案 0 :(得分:0)
将碰撞事件监听器添加到硬币中,
local function onLocalCollision( self, event )
if ( event.phase == "began" ) then
print("on collision began")
elseif ( event.phase == "ended" ) then
print( "on collision ended")
end
end
coin:addEventListener( "collision", onLocalCollision)
如果你想与硬币和精灵对象发生特定的碰撞,你应该使用碰撞过滤器。