My Object (spaceship) is behind the background

#1

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

0 Likes

#2

Hello!

The problem of having your ship be behind the background looks like a problem with the z-axis and can fixed by a few things.

Z-axis for game object determines the order they are placed over each other during runtime, with the higher number being on top.

On both the ship and background, you are using go.get_position(), this will copy over the vector3 position (x,y,z) from the game object the script is in. I don’t see anywhere in your code that changes this z-axis, so you can either change the z-axis in the editor window under ‘properties’ when you have the game object selected.

Alternatively, you can fix it in the script by doing the following. Before you do go.set_position(current_position, background) in your background script, set the z-axis to -1 by doing current_position.z = -1. In the ship script, do go.set_position(new_position), set the z-axis to 1 by doing new_position.z = 1. That should fix your problem.

Here’s a good example for the z-axis stuff : https://defold.com/examples/basics/z_order/

I also notice you are using constant values for the screen size and height, you can fetch them from the size of the window using:
sys.get_config(“display.width”, “0”)
sys.get_config(“display.height”, “0”)
This will be helpful if the player decides to try and resize the screen.
Here’s a good write up that explains how to handle screen resizing: https://defold.com/manuals/adapting-graphics-to-screen-size/

Best of luck, feel free to post here again if that solution doesn’t work.
Happy Defolding!

4 Likes