Hello,
I’m a newbie in Defold but I really like the engine. I’ve been programming in other languages like Javascript and PHP. And now started playing with games.
So I am having problems with a top down shooter game I’m doing. The bullets the character fires stays at the place it was created. No animation is happening.
I’m using the Oneroom project as my follow example. So I’m using the same example. Don’t know where I’m going wrong.
When I fire a bullet it just stays there like this:
So I know the factory creating of the bullet.go is working, but what about the animation. So here we go.
- First when someone clicks the mouse it calls this function
local function spawn_bullet(angle, properties)
factory.create("#bulletfactory", go.get_world_position() + vmath.rotate(angle, BULLET_OFFSET), angle, properties)
end
- The bullet.go is inside the factory and has this on it.
components {
id: "script"
component: "/main/bullet.script"
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
}
embedded_components {
id: "sprite"
type: "sprite"
data: "tile_set: \"/main/laserHero.atlas\"\n"
"default_animation: \"fxShot\"\n"
"material: \"/builtins/materials/sprite.material\"\n"
"blend_mode: BLEND_MODE_ALPHA\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
}
- And the bullet.script inside the bullet.go has this code
go.property("speed", 200)
go.property("type", hash("fxShot"))
go.property("enemy_bullet", false)
go.property("damage", 1)
go.property("distance", 500)
go.property("destroy_walls", false)
go.property("explosion", hash("explosion_small"))
go.property("particles", hash(""))
local COLLISION_RESPONSE = hash("collision_response")
function init(self)
msg.post("#sprite", "play_animation", { id = self.type })
self.initial_position = go.get_position()
end
function final(self)
end
function update(self, dt)
local pos = go.get_position()
print("pos1: " .. pos)
local d = vmath.rotate(go.get_rotation(), vmath.vector3(0, 1, 0))
print("dt: " .. dt)
pos = pos + d * self.speed * dt
go.set_position(pos)
print("pos2: " .. pos)
--local distance_traveled = vmath.length(pos - self.initial_position)
end
function on_message(self, message_id, message, sender)
if message_id == COLLISION_RESPONSE then
end
end
function on_input(self, action_id, action)
-- Add input-handling code here
-- Remove this function if not needed
end
I am guessing the part where the bullet is supposed to move is this one:
pos = pos + d * self.speed * dt
go.set_position(pos)
But I don’t know what to change or where I went wrong.
If you want to know what happen when I click