2014-12-26 2 views
0

Я совершенно не знаком с Corona SDK, и я просто смотрю примерные проекты, чтобы увидеть, как все работает. Я смотрю пример TAB, но у меня есть проблема.Невозможно получить функции для работы в Corona SDK (с TabButton)

У меня есть страница, как так (это стр.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).

-Спасибо

ответ

0

Вы создаете local tracker в качестве локальной переменной внутри функции scene:create. Это означает, что эта переменная будет доступна только в пределах объема указанной функции.

Вам необходимо переместить функцию tracker:touch(event) внутри функции scene:create, чтобы трекер мог быть доступен.

И вам нужно переместить слушателя в нижней части функции scene:create.

+0

О, хорошо, спасибо. Тем не менее, это не сработало, потому что я поставил 'tracker: addEventListneer (« touch », tracker)' внутри 'scene: create', но он все еще дает мне ошибку об этом? (Я также добавил 'return true' в нижней части моей' scene: create'). –

+0

Необходимо связать ошибку. Все ошибки объясняют, в чем проблема, ссылка, и мы можем рассказать о проблеме. –

0

Ничего. Я исправил это. Я всегда должен проверять свое правописание xD (я неправильно записал tracker:addEventListener("touch", tracker)). Такая ошибка noob. Спасибо за помощь, ребята!

Смежные вопросы