【问题标题】:How to create a line between two objects on touch - corona如何在触摸时在两个对象之间创建一条线 - 电晕
【发布时间】:2014-10-04 03:14:38
【问题描述】:

我正在使用 corona sdk 制作一个游戏,其中星星随机从屏幕上掉下来,直到用户输掉,请看代码:

local composer = require( "composer" )
local scene = composer.newScene()
local createstar = {}
local stars = {}
local timer
local b
local update = {}
local wait = {}
local ie = - 150



function scene:create( event )
    local sceneGroup = self.view


end

function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase

    if phase == "will" then

    elseif phase == "did" then

        stars = display.newGroup()



function wait( event )

        timer = timer.performWithDelay( 100, createstar, 0)
        Runtime:addEventListener('enterFrame', update)

end


            timer.performWithDelay( 200, wait)




         function createstar()
           ie = ie - 300
           astar = display.newImage('star.png', math.random( 1, 10) * 33, ie)

           stars:insert(astar)

           sceneGroup:insert(stars)


    end 


    function update(e)

     if(stars ~= nil)then
        for i = 1, stars.numChildren do

            stars[i].y = stars[i].y + 3
        end
     end
    end


    end 
end

function scene:hide( event )
    local sceneGroup = self.view
    local phase = event.phase

    if event.phase == "will" then

    elseif phase == "did" then

    end 
end


function scene:destroy( event )
    local sceneGroup = self.view
end


scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

return scene

现在要让我的游戏正常运行,我需要能够在每颗星之间画线。也就是说,我需要玩家触摸每颗星星,当他这样做时,在他触摸的星星和他之前触摸的星星之间会产生一条线。我知道如何画线,但不确定如何在使用函数创建的两个对象之间画线。有谁知道如何做到这一点?提前致谢!

【问题讨论】:

    标签: lua touch line coronasdk draw


    【解决方案1】:

    首先,请记住,StackOverflow 是一个让您获得有关您所面临的实际问题的问题的答案的地方。您应该包括有关您尝试过的内容以及您正在尝试做的事情的详细信息。

    这个问题相当广泛,因为它几乎要求最终解决方案。将来尝试分解您正在尝试解决的问题。首先,我如何知道一颗星星是否被触摸?如何保存有关这颗星的信息?如何检查是否已触摸两颗星?如何画线?

    以上所有问题都很基础,应该很容易解决。

    既然你说你根本不知道该怎么做,我会为你提供解决它的灵感,而不是一个解决的答案。


    首先创建一个数组来保存星星

    local touchedStarArray = {}
    

    然后为您创建的每个星形对象添加一个触摸监听器。

    (在此处了解有关触摸监听器的更多信息:CoronaDocs: addEventListener

    local star = display.newImage( "star.png" )
    
    function star:touch( event )
        if event.phase == "began" then
    
            -- Insert touched star into array
            table.insert(touchedStarArray, self)
    
            -- Check if array holds 2 stars yet
            if table.getn(touchedStarArray) >= 2 then
    
               -- if it does then draw a line between the 2 stars
               local line = display.newLine( touchedStarArray[1].x, touchedStarArray[1].y, touchedStarArray[2].x, touchedStarArray[2].y)
    
               -- and empty array
               touchedStarArray = {}
            end
        end
    end
    
    star:addEventListener( "touch", star)
    

    【讨论】:

    • 首先非常感谢您对我的帮助!我意识到我做错了,并且会尝试不再这样做。我试过你说的,但我得到一个错误:Bad argument #1 to 'newLine' (number expected, got nil)
    • 不要担心帮助,我这样做是因为我喜欢它!顺便说一句,我试过你的游戏:) 竖起大拇指!该错误是由未设置 .x 或 .y 的星号引起的。也可能是该对象未添加到数组中。上面的代码不能直接工作 - 它只是鼓舞人心的。
    • 还是谢谢你,我不确定没有你我能不能创造出这个游戏!我会为我的新错误创建一个新问题...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    相关资源
    最近更新 更多