瀏覽代碼

Added change image example for sprite

Björn Ritzl 5 月之前
父節點
當前提交
f6678c339a

+ 18 - 0
sprite/changeimage/all.texture_profiles

@@ -0,0 +1,18 @@
+path_settings {
+  path: "**"
+  profile: "Default"
+}
+profiles {
+  name: "Default"
+  platforms {
+    os: OS_ID_GENERIC
+    formats {
+      format: TEXTURE_FORMAT_RGBA
+      compression_level: BEST
+      compression_type: COMPRESSION_TYPE_DEFAULT
+    }
+    mipmaps: false
+    max_texture_size: 0
+    premultiply_alpha: true
+  }
+}

二進制
sprite/changeimage/assets/SourceSansPro-Semibold.ttf


+ 11 - 0
sprite/changeimage/assets/adventurer.tilesource

@@ -0,0 +1,11 @@
+image: "/assets/images/character_femaleAdventurer_sheet.png"
+tile_width: 96
+tile_height: 128
+animations {
+  id: "run"
+  start_tile: 37
+  end_tile: 44
+  playback: PLAYBACK_LOOP_FORWARD
+  fps: 10
+}
+extrude_borders: 2

二進制
sprite/changeimage/assets/images/character_femaleAdventurer_sheet.png


二進制
sprite/changeimage/assets/images/character_robot_sheet.png


二進制
sprite/changeimage/assets/images/character_zombie_sheet.png


+ 11 - 0
sprite/changeimage/assets/robot.tilesource

@@ -0,0 +1,11 @@
+image: "/assets/images/character_robot_sheet.png"
+tile_width: 96
+tile_height: 128
+animations {
+  id: "run"
+  start_tile: 37
+  end_tile: 44
+  playback: PLAYBACK_LOOP_FORWARD
+  fps: 10
+}
+extrude_borders: 2

+ 4 - 0
sprite/changeimage/assets/text32.font

@@ -0,0 +1,4 @@
+font: "/assets/SourceSansPro-Semibold.ttf"
+material: "/builtins/fonts/font.material"
+size: 32
+characters: " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"

+ 11 - 0
sprite/changeimage/assets/zombie.tilesource

@@ -0,0 +1,11 @@
+image: "/assets/images/character_zombie_sheet.png"
+tile_width: 96
+tile_height: 128
+animations {
+  id: "run"
+  start_tile: 37
+  end_tile: 44
+  playback: PLAYBACK_LOOP_FORWARD
+  fps: 10
+}
+extrude_borders: 2

+ 20 - 0
sprite/changeimage/example.md

@@ -0,0 +1,20 @@
+---
+tags: sprite
+title: Change sprite image
+brief: This example shows how to change the image of a sprite
+scripts: changeimage.script
+---
+
+The example shows a game object with a sprite and a script with three script properties to reference different tilesource images. The script lets the user change which image to use on the sprite.
+
+It is also possible to use a script property to reference an atlas instead of a tilesource:
+
+```lua
+go.property("hero", resource.atlas("/assets/hero.atlas"))
+
+function init(self)
+	go.set("#sprite", "image", self.hero)
+end
+```
+
+![tilesource](tilesource.png)

+ 45 - 0
sprite/changeimage/example/changeimage.collection

@@ -0,0 +1,45 @@
+name: "tint"
+scale_along_z: 0
+embedded_instances {
+  id: "go"
+  data: "components {\n"
+  "  id: \"changeatlas\"\n"
+  "  component: \"/example/changeimage.script\"\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"sprite\"\n"
+  "  type: \"sprite\"\n"
+  "  data: \"default_animation: \\\"run\\\"\\n"
+  "material: \\\"/builtins/materials/sprite.material\\\"\\n"
+  "textures {\\n"
+  "  sampler: \\\"texture_sampler\\\"\\n"
+  "  texture: \\\"/assets/zombie.tilesource\\\"\\n"
+  "}\\n"
+  "\"\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"label\"\n"
+  "  type: \"label\"\n"
+  "  data: \"size {\\n"
+  "  x: 128.0\\n"
+  "  y: 32.0\\n"
+  "}\\n"
+  "color {\\n"
+  "  x: 0.0\\n"
+  "  y: 0.0\\n"
+  "  z: 0.0\\n"
+  "}\\n"
+  "text: \\\"1 = Robot 2 = Zombie 3 = Adventurer\\\"\\n"
+  "font: \\\"/assets/text32.font\\\"\\n"
+  "material: \\\"/builtins/fonts/label-df.material\\\"\\n"
+  "\"\n"
+  "  position {\n"
+  "    y: -192.0\n"
+  "  }\n"
+  "}\n"
+  ""
+  position {
+    x: 360.0
+    y: 360.0
+  }
+}

+ 29 - 0
sprite/changeimage/example/changeimage.script

@@ -0,0 +1,29 @@
+-- create script properties with references to three different tile sources
+go.property("robot", resource.tile_source("/assets/robot.tilesource"))
+go.property("zombie", resource.tile_source("/assets/zombie.tilesource"))
+go.property("adventurer", resource.tile_source("/assets/adventurer.tilesource"))
+
+local function update_tilesource(image_id)
+	-- set the sprite image property to the specified tilesource
+	go.set("#sprite", "image", image_id)
+	-- play the run animation
+	sprite.play_flipbook("#sprite", "run")
+end
+
+function init(self)
+	msg.post(".", "acquire_input_focus")
+	update_tilesource(self.robot)
+end
+
+-- change sprite image when key 1, 2 and 3 are pressed
+function on_input(self, action_id, action)
+	if action.pressed then
+		if action_id == hash("key_1") then
+			update_tilesource(self.robot)
+		elseif action_id == hash("key_2") then
+			update_tilesource(self.zombie)
+		elseif action_id == hash("key_3") then
+			update_tilesource(self.adventurer)
+		end
+	end
+end

+ 65 - 0
sprite/changeimage/game.project

@@ -0,0 +1,65 @@
+[project]
+title = Defold-examples
+version = 0.1
+
+[bootstrap]
+main_collection = /example/changeimage.collectionc
+
+[input]
+game_binding = /input/game.input_bindingc
+repeat_interval = 0.05
+
+[display]
+width = 720
+height = 720
+high_dpi = 1
+
+[physics]
+scale = 0.02
+gravity_y = -500.0
+
+[script]
+shared_state = 1
+
+[collection_proxy]
+max_count = 256
+
+[label]
+subpixels = 1
+
+[sprite]
+subpixels = 1
+max_count = 32765
+
+[windows]
+iap_provider = 
+
+[android]
+package = com.defold.examples
+
+[ios]
+bundle_identifier = com.defold.examples
+
+[osx]
+bundle_identifier = com.defold.examples
+
+[html5]
+show_fullscreen_button = 0
+show_made_with_defold = 0
+scale_mode = no_scale
+heap_size = 64
+
+[graphics]
+texture_profiles = /all.texture_profiles
+
+[collection]
+max_instances = 32765
+
+[particle_fx]
+max_emitter_count = 1024
+
+[render]
+clear_color_blue = 1.0
+clear_color_green = 1.0
+clear_color_red = 1.0
+

+ 24 - 0
sprite/changeimage/input/game.input_binding

@@ -0,0 +1,24 @@
+key_trigger {
+  input: KEY_1
+  action: "key_1"
+}
+key_trigger {
+  input: KEY_2
+  action: "key_2"
+}
+key_trigger {
+  input: KEY_3
+  action: "key_3"
+}
+mouse_trigger {
+  input: MOUSE_BUTTON_LEFT
+  action: "touch"
+}
+touch_trigger {
+  input: TOUCH_MULTI
+  action: "multitouch"
+}
+text_trigger {
+  input: TEXT
+  action: "type"
+}

二進制
sprite/changeimage/tilesource.png