Manipulating a Running Scene
Loading a scene created in the visual editor creates a visual overlay graphically, but intentionally does nothing else. The objects in view match the scene editor's camera region and the graphics loaded match as well. However, to actually adjust and manipulate a game, add logic, and other ideas, we need computer code.
Because making an application do what we want in code is a complicated topic, we will not go into detail here how everything works and instead will focus on the features that are related to getting items from a scene. Users that want more instruction on coding up parts of an application should reference the various tutorials on the topic on the Quorum site. Here is a template for moving a character in a game:
use Libraries.Game.Game use Libraries.Game.Scenes.Scene use Libraries.System.File use Libraries.Interface.Events.KeyboardListener use Libraries.Interface.Events.KeyboardEvent use Libraries.Interface.Item2D use Libraries.Compute.Vector2 use Libraries.Interface.Events.CollisionListener2D use Libraries.Interface.Events.CollisionEvent2D use Libraries.Sound.Speech /* This is a default class for creating a game in Quorum. Note that the Main action begins by calling StartGame(), which loads a window a game can be displayed in. The action CreateGame is where the game should load any assets, like images to be displayed, frames of animation, or sounds to be played. */ class Main is Game, KeyboardListener, CollisionListener2D Speech speech Item2D player = undefined action Main StartGame() end action CreateGame // This code is provided by the Scene template. It automatically loads your scene into the game. File file file:SetPath("Scenes/Scene2.qs") LoadScene(file) // End of templated CreateGame code. player = FindItem2DByName("1: Boy") if player not= undefined player:CanRotate(false) AddKeyboardListener(me) AddCollisionListener(me) EnablePhysics2D(true) end end action Update(number seconds) end action PressedKey(KeyboardEvent event) if player = undefined return now end Vector2 vector if event:keyCode = event:LEFT vector:Set(-150, 0) player:SetLinearVelocity(vector) elseif event:keyCode = event:RIGHT vector:Set(150, 0) player:SetLinearVelocity(vector) elseif event:keyCode = event:UP vector:Set(0, 150) player:SetLinearVelocity(vector) elseif event:keyCode = event:DOWN vector:Set(0, -150) player:SetLinearVelocity(vector) end end action ReleasedKey(KeyboardEvent event) if player = undefined return now end if event:keyCode = event:LEFT or event:keyCode = event:RIGHT or event:keyCode = event:UP or event:keyCode = event:DOWN Vector2 vector vector:Set(0, 0) player:SetLinearVelocity(vector) end end action BeginCollision(CollisionEvent2D event) if player = undefined return now end Item2D other = undefined if event:GetItemA() = player other = event:GetItemB() elseif event:GetItemB() = player other = event:GetItemA() end speech:Say(other:GetName()) end end
The most crucial point to notice is that when items are given a name, they can be located in a game using the following action:
Item2D player = FindItem2DByName("1: Girl")
This action provides access to items created and loaded in a scene, allowing us to manipulate them with the keyboard, mouse, or however else makes sense for the product we are creating. Note that potentially, we could give all objects in our scene the same name. In such a case, this action would return the first object it finds with the given name, but not all of them. In such a case, we can use other actions to find all items in a scene by looking through the documentation for the Game class or obtaining information from a Layer.
In the next tutorial, we will discuss Changing the Scene Palette., which describes changing the scene Palette in Quorum Studio..