changeimage.script 971 B

1234567891011121314151617181920212223242526272829
  1. -- create script properties with references to three different tile sources
  2. go.property("robot", resource.tile_source("/assets/robot.tilesource"))
  3. go.property("zombie", resource.tile_source("/assets/zombie.tilesource"))
  4. go.property("adventurer", resource.tile_source("/assets/adventurer.tilesource"))
  5. local function update_tilesource(image_id)
  6. -- set the sprite image property to the specified tilesource
  7. go.set("#sprite", "image", image_id)
  8. -- play the run animation
  9. sprite.play_flipbook("#sprite", "run")
  10. end
  11. function init(self)
  12. msg.post(".", "acquire_input_focus")
  13. update_tilesource(self.robot)
  14. end
  15. -- change sprite image when key 1, 2 and 3 are pressed
  16. function on_input(self, action_id, action)
  17. if action.pressed then
  18. if action_id == hash("key_1") then
  19. update_tilesource(self.robot)
  20. elseif action_id == hash("key_2") then
  21. update_tilesource(self.zombie)
  22. elseif action_id == hash("key_3") then
  23. update_tilesource(self.adventurer)
  24. end
  25. end
  26. end