Lua 如何在触摸屏上创建两个对象之间的直线
我正在使用corona sdk制作一个游戏,其中星星随机从屏幕上落下,直到用户失败,请参见代码:Lua 如何在触摸屏上创建两个对象之间的直线,lua,touch,line,coronasdk,draw,Lua,Touch,Line,Coronasdk,Draw,我正在使用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:
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
现在,为了让我的比赛发挥作用,我需要能够在每个明星之间划出界限。也就是说,我需要球员触摸每一颗星,当他触摸到每一颗星时,就会在他触摸到的那颗星和之前触摸到的那颗星之间形成一条线。我知道如何画一条线,但不知道如何在用函数创建的两个对象之间画线。有人知道怎么做吗?提前谢谢 首先,请记住,StackOverflow是一个让你得到关于你所面临的实际问题的答案的地方。希望你能详细说明你尝试过什么,以及你到底想做什么 这个问题相当广泛,因为它几乎要求最终解决。将来,试着把你要解决的问题分解开来。首先,我如何知道一颗星星是否被触碰过?如何保存有关该明星的信息?我如何检查两颗星是否被碰过?我怎么画一条线 以上所有问题都是非常基本的,应该很容易解决 既然你说你根本不知道怎么做,我会给你灵感去解决它,而不是一个解决了的答案
首先创建一个阵列来容纳星星
local touchedStarArray = {}
然后为您创建的每个星形对象添加一个触摸侦听器
(请在此处阅读有关触控侦听器的更多信息:)
首先非常感谢你对我的帮助!我意识到我做错了什么,我会努力不再这样做。我试过你说的,但我犯了一个错误:错误的论点#1到“新线”(预期数字,得到零)不要担心帮助,我这样做是因为我喜欢它!我试过你的游戏顺便说一句:)竖起大拇指!该错误是由于星号未设置.x或.y而导致的。也可能是对象未添加到数组中。上面的代码不是直接工作的工具-它只是鼓舞人心的。无论如何,谢谢你,我不确定如果没有你我是否可以创建这个游戏!我将为我的新错误创建一个新问题。。。
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)