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 require computer code.

Because making an application do what is wanted in code is a complicated topic, this tutorial does not go into detail here how everything works and instead will focus on the features that are related to getting items from a scene, to be used for movement or other actions. Users who want more instruction on coding 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


/*
    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/Scene.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

        //these variables get the object's x and y coordinares
        number x = player:GetX()
        number y = player:GetY()

        //this will be used to move the object 20 pixels
        number amount = 20

        if event:keyCode = event:LEFT
            player:SetY(y-amount)
        elseif event:keyCode = event:RIGHT
            player:SetY(y+amount)
        elseif event:keyCode = event:UP
            player:SetX(x+amount)
        elseif event:keyCode = event:DOWN
            player:SetX(x-amount)
        end
    end

    action ReleasedKey(KeyboardEvent event)
        if player = undefined
            return now
        end
    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: Boy")

This action provides access to items created and loaded in a scene, allowing manipulations by keyboard, mouse, or however else makes sense for the product we are creating. In the above example, the Boy object is found and manipulated with keypresses. Note that potentially, all objects in the scene could have 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, other actions in the Game class can be used.

Next Tutorial

In the next tutorial, we will discuss Changing the Scene Palette, which describes changing the scene Palette in Quorum Studio.