我是Corona SDK的新手,我只是看一下示例项目,看看事情是如何运作的。我正在看TAB示例,但是我遇到了问题。
我有一个这样的页面(这是第2页)
local composer = require( "composer" )
local scene = composer.newScene()
function scene:create( event )
local sceneGroup = self.view
-- Called when the scene's view does not exist.
--
-- INSERT code here to initialize the scene
-- e.g. add display objects to 'sceneGroup', add touch listeners, etc.
-- create a white background to fill screen (things go in here like pictures etc)
local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
bg.anchorX = 0
bg.anchorY = 0
bg:setFillColor( 0 ) -- white
-- this will create the thing that you drag (the function is after)
local tracker = display.newRect( 568, 340, 50, 50 )
tracker:setFillColor( 1 )
-- all objects must be added to group (e.g. self.view)
sceneGroup:insert( bg )
sceneGroup:insert( tracker )
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == "will" then
-- Called when the scene is still off screen and is about to move on screen
elseif phase == "did" then
-- Called when the scene is now on screen
--
-- INSERT code here to make the scene come alive
-- e.g. start timers, begin animation, play audio, etc.
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == "will" then
-- Called when the scene is on screen and is about to move off screen
--
-- INSERT code here to pause the scene
-- e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == "did" then
-- Called when the scene is now off screen
end
end
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's "view" (sceneGroup)
--
-- INSERT code here to cleanup the scene
-- e.g. remove display objects, remove touch listeners, save state, etc.
end
function tracker:touch( event )
if event.phase == "began" then
self.markX = self.x --stores x location
self.markY = self.y --stores y location
elseif event.phase == "moved" then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y -- moves the object from things above
end
end
---------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
tracker:addEventListneer( "touch", tracker)
-----------------------------------------------------------------------------------------
return scene
无论如何要简化我所做的改变:
local tracker = display.newRect( 568, 340, 50, 50 )
tracker:setFillColor( 1 )
这将创建一个我想要制作的新盒子,这样你就可以将它拖到屏幕上了(我已经使用过这个功能):
function tracker:touch( event )
if event.phase == "began" then
self.markX = self.x --stores x location
self.markY = self.y --stores y location
elseif event.phase == "moved" then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y -- moves the object from things above
end
end
总的来说,它会创建一个框,我正在尝试添加一个函数,以便您可以将其拖动到屏幕上。但是它不起作用并且给我一个错误,说明tracker
在函数行function tracker:touch( event )
的那个开头是错误的?任何帮助,因为我认为这是在错误的地方。
P.S我也有一个tracker:addEventListneer( "touch", tracker)
听众。
-Thanks
答案 0 :(得分:0)
您正在local tracker
函数中创建scene:create
作为局部变量。这意味着此变量仅在所述函数的范围内可用。
您需要在tracker:touch(event)
函数中移动scene:create
函数才能使跟踪器可以访问。
您需要将监听器移动到scene:create
函数的底部。
答案 1 :(得分:0)
没关系。我修好了。我应该经常检查我的拼写xD(我拼错了tracker:addEventListener( "touch", tracker)
错误)。这样的菜鸟错误。谢谢你的帮助。