My Object is behind the backgroud
Code For Skip:
function init(self)
msg.post(’.’, ‘acquire_input_focus’)
self.direction = vmath.vector3() – function returns {x = 0, y = 0, z = 0}
self.speed = 8
self.ship_half_width = go.get(’#ship’, “size.x”) / 2
self.ship_half_height = go.get(’#ship’, “size.y”) / 2
self.screen_top = 0 + self.ship_half_height
self.screen_bottom = 720 - self.ship_half_height
self.screen_left = 0 + self.ship_half_width
self.screen_right = 1280 - self.ship_half_width
end
function final(self)
– Add finalization code here
– Remove this function if not needed
end
function update(self, dt)
– check if there is any movement from the player
if self.direction ~= vmath.vector3() then
local current_position = go.get_position() – get current player position
local new_position = current_position + self.direction * self.speed – set the new position
– if moving in the left direction
if self.direction.x < 0 then
– check if player is at the left limit of the screen
if new_position.x < self.screen_left then
new_position.x = self.screen_left – if so, put him at the edge
end
– change animation of sprite to flight in the left direction
msg.post(’#ship’, ‘play_animation’, {id = hash(‘flight_left’)})
– if moving in the right direction
elseif self.direction.x > 0 then
– check if player is at the right limit of the screen
if new_position.x > self.screen_right then
new_position.x = self.screen_right – if so, put him at the edge
end
– change animation of sprite to flight in the right direction
msg.post(’#ship’, ‘play_animation’, {id = hash(‘flight_right’)})
end
if self.direction.y < 0 then
msg.post(’#jet’, ‘play_animation’, {id = hash(‘no_jet’)})
msg.post(’#front_jet_left’, ‘play_animation’, {id = hash(‘front_jet’)})
msg.post(’#front_jet_right’, ‘play_animation’, {id = hash(‘front_jet’)})
– check if player is at the bottom limit of the screen
if new_position.y < self.screen_top then
new_position.y = self.screen_top – if so, put him at the edge
end
elseif self.direction.y > 0 then
msg.post(’#jet’, ‘play_animation’, {id = hash(‘jet’)})
msg.post(’#front_jet_left’, ‘play_animation’, {id = hash(‘front_jet_none’)})
msg.post(’#front_jet_right’, ‘play_animation’, {id = hash(‘front_jet_none’)})
– check if player is at the top limit of the screen
if new_position.y > self.screen_bottom then
new_position.y = self.screen_bottom – if so, put him at the edge
end
end
– change the player to the new position in the game
go.set_position(new_position)
– Erase the direction of the last movement
self.direction = vmath.vector3()
else – if NOT moving for for the left OR the right
– change animation of sprite to flight forward
msg.post(’#ship’, ‘play_animation’, {id = hash(‘flight’)})
msg.post(’#jet’, ‘play_animation’, {id = hash(‘no_jet’)})
msg.post(’#front_jet_left’, ‘play_animation’, {id = hash(‘front_jet_none’)})
msg.post(’#front_jet_right’, ‘play_animation’, {id = hash(‘front_jet_none’)})
end
end
function on_message(self, message_id, message, sender)
– Add message-handling code here
– Remove this function if not needed
end
function on_input(self, action_id, action)
if action_id == hash(‘move_up’) then
self.direction.y = 1
elseif action_id == hash(‘move_down’) then
self.direction.y = -1
end
if action_id == hash(‘move_left’) then
self.direction.x = -1
elseif action_id == hash(‘move_right’) then
self.direction.x = 1
end
end
function on_reload(self)
– Add reload-handling code here
– Remove this function if not needed
end
Code for backgroud game:
function init(self)
self.speed = 100
self.return_position = 1016
self.backgrounds = {’#primary_background’,’#secondary_background’}
end
function update(self, dt)
for _, background in ipairs(self.backgrounds) do
local current_position = go.get_position(background)
current_position.y = current_position.y - self.speed * dt
if current_position.y <= self.return_position * -1 then
if background == self.backgrounds[1] then
current_position.y = go.get_position(self.backgrounds[2]).y + self.return_position
else
current_position.y = go.get_position(self.backgrounds[1]).y + self.return_position
end
end
go.set_position(current_position, background)
end
end