我正在 Lua 中开发一个简单的应用程序,以便更好地了解 Corona SDK:一个红球在屏幕上弹跳并在用户触摸它时切换方向。但是,每当我在 Corona Simulator 中单击球时,都会多次调用触摸事件。这是我的代码:
local xdirection,ydirection = 1,1
local xpos,ypos = display.contentWidth*0.5,display.contentHeight*0.5
local circle = display.newCircle( xpos, ypos, 20 );
circle:setFillColor(255,0,0,255);
local x_speed = 5
local y_speed = 5
local function animate(event)
xpos = xpos + ( x_speed * xdirection );
ypos = ypos + ( y_speed * ydirection );
if (xpos > display.contentWidth - 20 or xpos < 20) then
xdirection = xdirection * -1;
end
if (ypos > display.contentHeight - 20 or ypos < 20) then
ydirection = ydirection * -1;
end
circle:translate( xpos - circle.x, ypos - circle.y)
end
local function switch(event)
xdirection = xdirection * -1;
ydirection = ydirection * -1;
print "Switched!"
end
Runtime:addEventListener( "enterFrame", animate );
circle:addEventListener("touch",switch);
每次我在模拟器中点击球,“切换!” 多次打印。有什么想法吗?