Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --prehash the inputs
- local input_left = hash("left")
- local input_right = hash("right")
- local input_jump = hash("jump")
- local direction = 0
- function init(self)
- msg.post(".", "acquire_input_focus")
- -- maximum speed the instance can be moved
- self.max_speed = 100
- -- velocity of the instance, initially zero
- self.velocity = vmath.vector3()
- end
- --------- handles animation --------
- function animation_direction(self,direction)
- if direction == 1 or direction == 2 then
- msg.post("#sprite","play_animation", {id = hash("walk")})
- end
- if direction == 1 then
- sprite.set_hflip("#sprite", true) --turn to face direction true is oposite from original sprite directio
- else if direction == 2 then
- sprite.set_hflip("#sprite", false) --false resets to original direction
- end end
- end
- ---------- handles input ----------
- function on_input(self, action_id, action)
- if action_id == input_left and action.pressed then
- direction = 1
- animation_direction(self, direction) --- run animation function
- self.velocity = vmath.vector3(action.value * -self.max_speed, 0, 0)
- else if action_id == input_right and action.pressed then
- direction = 2
- animation_direction(self, direction) --- run animation function
- self.velocity = vmath.vector3(action.value * self.max_speed, 0, 0)
- else if action_id == input_jump then
- direction = 3
- print("jump")
- else if action.released then
- msg.post("#sprite","play_animation", {id = hash("idle")})
- self.velocity = vmath.vector3(action.value * 0, 0, 0)
- end
- end
- end
- end
- end
- function update(self, dt)
- -- move the instance
- go.set_position(go.get_position() + dt * self.velocity)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement