Defold学习笔记002

2018-08-10  本文已影响0人  ThzStudio
1.拖拽(详细工程https://github.com/britzl/publicexamples/tree/master/examples/drag_to_scroll
function update(self, dt)
    --2 核心代码,update衰减力,并将力作用于位置
    if not self.drag and self.fling_enabled then
        local pos = go.get_position() + self.fling
        go.set_position(limit(self, pos))
        self.fling = vmath.lerp(0.1, self.fling, vmath.vector3()) 
    end
end

function on_input(self, action_id, action)
    if action.pressed then
        self.drag = true
        self.pressed_pos = vmath.vector3(action.x, action.y, 0)
        self.pressed_time = socket.gettime()
        self.camera_pos = go.get_position()
    elseif action.released then
        self.drag = false
        --1 核心代码,计算得到一个self.fling,这是一个有方向的力
        if self.fling_enabled then
            local released_time = socket.gettime()
            local released_pos = vmath.vector3(action.x, action.y, 0)
            local delta_time = released_time - self.pressed_time
            local distance = vmath.length(released_pos - self.pressed_pos)
            local velocity = distance / delta_time
            if velocity >= self.fling_distance_threshold then
                local direction = vmath.normalize(self.pressed_pos - released_pos)
                self.fling = direction * velocity * 0.02
            end
        end
    end
    if self.drag then
        local mouse_pos = vmath.vector3(action.x, action.y, 0)
        local pos = self.camera_pos + self.pressed_pos - vmath.vector3(action.x, action.y, 0)
        go.set_position(limit(self, pos))
    end
end
2.Tile Map
Tile Map

Tile map比较简单,编辑的时候快捷键space选择图片和 shift-e橡皮擦

上一篇下一篇

猜你喜欢

热点阅读