Alright
Controller.script
go.property("speed", 4)
local grid = 460
local platform_heights = { 100, 200, 350 }
local coins = 3
function init(self)
msg.post("ground/controller#script", "set_speed", { speed = self.speed })
self.gridw = 0
self.spawns = {}
self.spawn_ids = {}
self.score = 0
self.phase = false
self.startime = socket.gettime()
end
function update(self, dt)
self.gridw = self.gridw + self.speed
if self.gridw >= grid then
self.gridw = 0
if math.random() > 0.2 then
local h = platform_heights[math.random(#platform_heights)]
local f = "#platform_factory"
local coins = coins
if math.random() > 0.5 then
f = "#platform_long_factory"
coins = coins * 2
end
local p = factory.create(f, vmath.vector3(1600, h, 0), nil, {}, 0.6)
table.insert(self.spawn_ids, p)
msg.post(p, "set_speed", { speed = self.speed })
msg.post(p, "create_coins", { coins = coins })
table.insert(self.spawns, p)
end
end
phase(self)
distance(self)
end
function phase(self)
if self.phase == true then
for i, p in pairs(self.spawn_ids) do
msg.post(msg.url(nil, p, "danger_edges"), "disable")
msg.post(msg.url(nil, p, "collisionobject"), "disable")
end
timer.delay(4, false, function(self, id)
self.phase = false
end)
end
end
function distance(self)
self.currentime = socket.gettime()
self.distance = (self.currentime - self.startime) * self.speed
msg.post("hud", "distance", {distance = self.distance})
end
function on_message(self, message_id, message, sender)
if message_id == hash("reset") then
msg.post("hero#script", "reset")
-- Delete all platforms
for i,p in ipairs(self.spawns) do
go.delete(p)
end
self.spawns = {}
elseif message_id == hash("delete_spawn") then
for i,p in ipairs(self.spawns) do
if p == message.id then
table.remove(self.spawns, i)
go.delete(p)
end
end
elseif message_id == hash("score_coin") then
self.score = self.score + 100
msg.post("hud", "set_score", { score = self.score })
end
end
hud.script
function init(self)
self.hits = 3
self.distance = 0
end
function on_message(self, message_id, message, sender)
if message_id == hash("set_score") then
local s = gui.get_node("score")
gui.set_text(s, message.score)
elseif message_id == hash("teleport") then
gui.set_text(gui.get_node("distance"), self.distance + 10)
elseif message_id == hash("distance") then
gui.set_text(gui.get_node("distance"), math.floor(message.distance))
self.distance = math.floor(message.distance)
end
end
player.script
local function teleport(self)
if self.action == true then
self.position = vmath.vector3(500, self.position.y, self.position.z)
go.set_position(self.position)
self.action = false
msg.post("hud", "teleport")
timer.delay(4, false, function(self, id)
go.animate(".", "position.x", go.PLAYBACK_ONCE_FORWARD, 298.735, go.EASING_LINEAR, 1)
end)
end
end
function init(self)
msg.post(".", "acquire_input_focus")
self.position = go.get_position()
local orgp = go.get_position()
msg.post("#", "reset")
self.action_count = {}
self.startime = socket.gettime()
end
function update(self, dt)
teleport(self)
end
What am I trying to do?
Add to the distance tracker 10 more meters when teleported as shown in hud.script, the message that is sent to the hud is via the player.script
What doesn’t work(sorry I forgot to mentioned that my mistake)?
It doesn’t execute the line that adds the 10 more meters to the distance tracker(probably because its in an elseif)