1
votes

I am making a game with corona sdk in which stars fall down the screen randomly and until the user loses, see the code:

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

Now for my game to work I need to be able to draw lines between each star. That is to say that I need the player to touch each star and when he does then a line is created between the star he touched and the one he touched before that. I know how to draw a line but am not sure how to do it between two objects created with a function. Does anyone know how to do this? Thanks in advance!

1

1 Answers

1
votes

Firstly, keep in mind StackOverflow is a place where you get answers to questions about an actual problem you have faced. It is expected that you include details about what you have tried and exactly what you are trying to do.

This question is rather broad as it is pretty much asking for a final solution. In the future try to break down the problem you are trying solve. First how do I know if a star is touched? How do I save information about this star? How do I check if two stars has been touched? How do I draw a line?

All of above questions are very basic and should be easily solved.

Since you said you simply aren't sure how to do it I will provide you with inspiration to solve it rather then a solved answer.


First create an array to hold the stars

local touchedStarArray = {}

Then add a touch listener to each star object that you create ex.

(Read more about touch listeners here: 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)