通过使用这个代码,我可以移动Y轴上的“磁铁”,但是如何将“场”链接到“磁铁”,这样当我移动“磁铁”时,“场”随之移动?
magX = 300
magY = 100
field = display.newImageRect( "field.png", 330, 330 ) ; field.alpha = 0.2
field.name = "field"
field.x = magX; field.y = magY
physics.addBody( field, "static", { isSensor=true, radius=180, filter=fieldFilter } )
magnet = display.newImageRect( "magnet.png", 128, 128 )
magnet.name = "magnet"
magnet.x = magX ; magnet.y = magY
physics.addBody( magnet, "static", { bounce=0, radius=40, filter=magnetFilter } )
function magnet:touch( event )
Runtime:removeEventListener( "touch", screenTouch )
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 = moverX --(event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y
if self.y > 500 then self.y = 500 end
if self.y < 100 then self.y = 100 end
magX = self.x
magY = self.y
else
Runtime:addEventListener( "touch", screenTouch )
end
return true
end
magnet:addEventListener( "touch", magnet )
答案 0 :(得分:0)
通过将 transition.to(field,{time = 1,x = self.x,y = self.y})添加到 elseif event.phase ==“移动来修复“那么
function magnet:touch( event )
Runtime:removeEventListener( "touch", screenTouch )
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 = moverX --(event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y -- move object based on calculations above
--print("touch")
if self.y > 500 then self.y = 500 end
if self.y < 100 then self.y = 100 end
magX = self.x
magY = self.y
transition.to( field, { time=1, x=self.x, y=self.y } )
else
Runtime:addEventListener( "touch", screenTouch )
end
return true
end