1-- Moving sprites example. 2-- This sample demonstrates: 3-- - Adding Sprite elements to the UI 4-- - Storing custom data (sprite velocity) inside UI elements 5-- - Handling frame update events in which the sprites are moved 6 7require "LuaScripts/Utilities/Sample" 8 9local numSprites = 100 10local sprites = {} 11 12-- Custom variable identifier for storing sprite velocity within the UI element 13local VAR_VELOCITY = StringHash("Velocity") 14 15function Start() 16 -- Execute the common startup for samples 17 SampleStart() 18 19 -- Create the sprites to the user interface 20 CreateSprites() 21 22 -- Set the mouse mode to use in the sample 23 SampleInitMouseMode(MM_FREE) 24 25 -- Hook up to the frame update events 26 SubscribeToEvents() 27end 28 29function CreateSprites() 30 local decalTex = cache:GetResource("Texture2D", "Textures/UrhoDecal.dds") 31 32 local width = graphics.width 33 local height = graphics.height 34 35 for i = 1, numSprites do 36 -- Create a new sprite, set it to use the texture 37 local sprite = Sprite:new() 38 sprite.texture = decalTex 39 sprite:SetFullImageRect() 40 41 -- The UI root element is as big as the rendering window, set random position within it 42 sprite.position = Vector2(Random(width), Random(height)) 43 44 -- Set sprite size & hotspot in its center 45 sprite:SetSize(128, 128) 46 sprite.hotSpot = IntVector2(64, 64) 47 48 -- Set random rotation in degrees and random scale 49 sprite.rotation = Random(360.0) 50 sprite.scale = Vector2(1.0, 1.0) * (Random(1.0) + 0.5) 51 52 -- Set random color and additive blending mode 53 sprite:SetColor(Color(Random(0.5) + 0.5, Random(0.5) + 0.5, Random(0.5) + 0.5, 1.0)) 54 sprite.blendMode = BLEND_ADD 55 56 -- Add as a child of the root UI element 57 ui.root:AddChild(sprite) 58 59 -- Store sprite's velocity as a custom variable 60 sprite:SetVar(VAR_VELOCITY, Variant(Vector2(Random(200.0) - 100.0, Random(200.0) - 100.0))) 61 62 table.insert(sprites, sprite) 63 end 64end 65 66function SubscribeToEvents() 67 -- Subscribe HandleUpdate() function for processing update events 68 SubscribeToEvent("Update", "HandleUpdate") 69end 70 71function MoveSprites(timeStep) 72 local width = graphics.width 73 local height = graphics.height 74 75 for i = 1, numSprites do 76 local sprite = sprites[i] 77 sprite.rotation = sprite.rotation + timeStep * 30 78 79 local newPos = sprite.position 80 newPos = newPos + sprite:GetVar(VAR_VELOCITY):GetVector2() * timeStep 81 82 if newPos.x >= width then 83 newPos.x = newPos.x - width 84 elseif newPos.x < 0 then 85 newPos.x = newPos.x + width 86 end 87 if newPos.y >= height then 88 newPos.y = newPos.y - height 89 elseif newPos.y < 0 then 90 newPos.y = newPos.y + height 91 end 92 sprite.position = newPos 93 end 94end 95 96function HandleUpdate(eventType, eventData) 97 local timeStep = eventData["TimeStep"]:GetFloat() 98 99 MoveSprites(timeStep) 100end 101 102-- Create XML patch instructions for screen joystick layout specific to this sample app 103function GetScreenJoystickPatchString() 104 return 105 "<patch>" .. 106 " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" .. 107 " <attribute name=\"Is Visible\" value=\"false\" />" .. 108 " </add>" .. 109 "</patch>" 110end 111