diff options
| author | Tom Black <[email protected]> | 2018-09-04 00:45:00 -0700 |
|---|---|---|
| committer | Tom Black <[email protected]> | 2018-09-12 14:40:27 -0700 |
| commit | 632e007e711ba8e4e7d9b3a39cd64e32ca58afd1 (patch) | |
| tree | d28aeacc312c2e736ceb2028f21c234738cbfbf5 /test | |
| parent | 36754d58e1e17c11cc243f896d63cd9e1daf95ed (diff) | |
| download | ruby2d-632e007e711ba8e4e7d9b3a39cd64e32ca58afd1.tar.gz ruby2d-632e007e711ba8e4e7d9b3a39cd64e32ca58afd1.zip | |
Sprite enhancements
Add ability to flip the sprite, run a block after an animation, properly change the width and height, and other fixes.
Diffstat (limited to 'test')
| -rw-r--r-- | test/sprite.rb | 62 | ||||
| -rw-r--r-- | test/testcard.rb | 17 |
2 files changed, 64 insertions, 15 deletions
diff --git a/test/sprite.rb b/test/sprite.rb index 713375a..e62488b 100644 --- a/test/sprite.rb +++ b/test/sprite.rb @@ -6,17 +6,28 @@ else media = "media" end -set title: "Ruby 2D — Sprite", width: 350, height: 150 +set title: "Ruby 2D — Sprite", width: 400, height: 300 +coin1 = Sprite.new( + "#{media}/coin.png", + clip_width: 84, + time: 300, + loop: true +) + +coin1.play -coin = Sprite.new( +coin2 = Sprite.new( "#{media}/coin.png", + y: 90, + width: 42, + height: 42, clip_width: 84, time: 300, loop: true ) -coin.play +coin2.play boom = Sprite.new( "#{media}/boom.png", @@ -28,6 +39,8 @@ boom = Sprite.new( hero = Sprite.new( "#{media}/hero.png", x: 261, + width: 78, + height: 99, clip_width: 78, time: 250, animations: { @@ -39,7 +52,7 @@ hero = Sprite.new( atlas = Sprite.new( "#{media}/texture_atlas.png", - x: 10, y: 100, + x: 50, y: 90, animations: { count: [ { @@ -79,21 +92,58 @@ on :key_down do |e| case e.key when 'p' - coin.play + coin1.play + coin2.play boom.play atlas.play :count + when 'b' + boom.play nil, nil, nil do + puts "Boom animation finished!" + end when 's' - coin.stop + coin1.stop + coin2.stop hero.stop atlas.stop + when 'left' + hero.play :walk, :loop, :flip_h when 'right' hero.play :walk, :loop when 'up' hero.play :climb, :loop when 'down' + hero.play :climb, :loop, :flip_v + when 'h' + hero.play :climb, :loop, :flip_hv + when 'c' hero.play :cheer end end +on :key_held do |e| + case e.key + when 'a' + hero.play :walk, :loop, :flip_h + hero.x -= 1 + when 'd' + hero.play :walk, :loop + hero.x += 1 + when 'w' + hero.play :climb, :loop + hero.y -= 1 + when 's' + hero.play :climb, :loop, :flip_v + hero.y += 1 + when 'z' + hero.width = get(:mouse_x) + hero.height = get(:mouse_y) + end +end + +on :key_up do |e| + if ['w', 'a', 's', 'd'].include? e.key + hero.stop + end +end show diff --git a/test/testcard.rb b/test/testcard.rb index 4e629bc..7ae153f 100644 --- a/test/testcard.rb +++ b/test/testcard.rb @@ -221,13 +221,14 @@ Text.new(x: 144, y: 202, text: "B", font: font, color: [0.0, 0.0, 1.0, 1.0]) fps = Text.new(x: 10, y: 470, text: "", font: font) # Sprites -s1 = Sprite.new(450, 200, "#{media}/sprite_sheet.png") -s1.add(forwards: [ - [ 0, 0, 50, 50, 30], - [ 50, 0, 50, 50, 40], - [100, 0, 50, 50, 50], - [150, 0, 50, 50, 60] -]) +spr = Sprite.new( + "#{media}/sprite_sheet.png", + x: 450, y: 200, + clip_width: 50, + time: 500, + loop: true +) +spr.play # Pointer for mouse pointer = Square.new(size: 10) @@ -293,8 +294,6 @@ update do pointer_outline.color = [0, 1, 0, 0] end - s1.animate(:forwards) - if (get :frames) % 20 == 0 fps.text = "FPS: #{(get :fps).round(3)}" end |
