Now that we have a list of touches that can be examined at will, we need to add logic to compare those touches with the desired target locations.
Save touches.lua
if needed, and open the file game.lua
from the project folder. Take a look through the scene:willEnterScene(event)
function; this is where the list of game targets will be created, that we will use in the main loop.
Start at the top of game.lua
.
Load the function that will carry a for
loop over all active touch points:
local level = require "level"
local touches = require "touches"
local storyboard = require "storyboard"
Find the scene:clock(event)
function which is in charge of updating the game each time its main loop runs; it is currently empty.
function scene:clock( event ) local orbits = {} local orphans = 0 end
function scene:clock( event ) local orbits = {} local orphans = 0 for id, location in touches() do local nearest, proximity for _, target in pairs(self.Targets) do local distance = math.pythagorean(location.x - target.x, location.y - target.y) if not nearest or distance < proximity then nearest, proximity = target, distance end end end end
if not nearest or distance < proximity then nearest, proximity = target, distance end end if proximity <= self.TARGET_RADIUS and ( not orbits[nearest] or proximity < orbits[nearest]) then orbits[nearest] = proximity end end
if proximity <= self.TARGET_RADIUS and ( not orbits[nearest] or proximity < orbits[nearest]) then orbits[nearest] = proximity else orphans = orphans - 1 end
In the same function, we'll adjust the player's score. Because accuracy in this game is something that has to be sustained, the total accuracy of touches at any given moment is used to speed up or slow down the rate at which the score increases. If the score stops increasing completely, the level is over.
orphans = orphans - 1
end
end
local pressure = orphans * self.TARGET_RADIUS * -0.5
end
local pressure = orphans * self.TARGET_RADIUS * -0.5 for target, proximity in pairs(orbits) do pressure = pressure + (self.TARGET_RADIUS - proximity) end end
pressure = pressure + (self.TARGET_RADIUS - proximity)
end
self.Velocity = math.min(self.Velocity + pressure * event.delta, self.TERMINAL_VELOCITY)
end
self.Velocity = math.min(self.Velocity + pressure * event.elapsed, self.TERMINAL_VELOCITY) if self.Velocity <= 0 then self:dispatchEvent{name = 'Level'; action = 'failed'} end end
if self.Velocity <= 0 then self:dispatchEvent{name = 'Level'; action = 'failed'} else self.ScoreCurrent = self.ScoreCurrent + self.Velocity * event.delta end end
We set up the main game loop to award points for each touch that lies close enough to a target point (keep in mind that the target points are in motion). Because this will happen in every frame, we need to adjust the value being gained according to the amount of game time that has actually passed.
3.16.81.94