我对Lua很新。这就是我试过的:
local function tapListener( event )
-- Code executed when the button is tapped
if crate.gravityScale == 1 then crate.gravityScale=-1
if crate.gravityScale == -1 then crate.gravityScale=1
-- "event.target" is the tapped object
return true
end
local myButton = display.newRect( 540, 960, 1080, 1920 )
myButton:setFillColor( 0, 0.01 )
myButton:addEventListener( "tap", tapListener )
-- Add a "tap" listener to the object
这样做的正确方法是什么?我不能真正理解如何使用API引用来实现它。
.......
编辑8/19/17: 我不喜欢它如何随着重力移动所以我将它改为setLinearVelocity但现在它不会改变方向。这是我试过的:
speed = 400
-- make a crate (off-screen), position it, and rotate slightly
local crate = display.newImageRect( "crate.png", 90, 90 )
crate.x, crate.y = 540, 1750
crate.rotation = 0
physics.addBody( crate, "dyanamic" )
crate:setLinearVelocity(speed, 0)
local function tapListener( event )
speed = speed == 400 and -400 or 400
return true
end
local myButton = display.newRect( 540, 960, 1080, 1920 )
myButton:setFillColor( 0, 0.01 )
myButton:addEventListener( "tap", tapListener )
我做错了什么?