ActionScript Cloud Code


After making the Set_Pos and Get_Pos Events in the portal, it's time to call them from your game. In this tutorial, you'll learn how to use a persistent variable saved against the player to change their sprite location.

Setting up your Set and Get Functions

Testing the Set and Get Functions

Example ActionScript Cloud code can be downloaded here

Setting up your Set and Get Functions

You'll notice that from here on the tutorials will automatically connect to the Portal. You can set your URL and API Secret before the game launches.

        private function ConnectToPortal():void

Start by creating a function that will save the player's location. This function will be logging an Event request. Similar to Authenticating and Registering, this function will build a request:

        public function SetPos():void

                    var location:Object = {};

                    location.X = Player.x;
                    location.Y = Player.y;

                    requestBuilder.createLogEventRequest().setEventKey("Set_Pos").setJSONEventAttribute("POS", location).send(SetPosResponse);

                    logger("Saving Position...");

Optionally, you can have a Response handler which logs out when the Event is successfully requested:

        public function SetPosResponse(response:LogEventResponse):void
                    logger("Saving X pos = " + Player.x.toString());
                    logger("Saving Y pos = " + Player.y.toString());

Create a function that will retrieve the position of the player by calling the Get_Pos Event. You'll need to build a request that is similar to the previously created SetPos function, but this time you'll won't need an Attribute. It's important to set a response handler function as a parameter for the send() method:

            public function GetPos():void
                    logger("Retrieving Position...");


In the response handler for the Get_Pos Event you'll need to break down the response and retrieve the values that are passed in by Script Data:

In this example, you can access the X value simply by using script.POS.X. After setting the two variables of type NUMBER as X and Y, simply set your player's location to the retrieved values.

        public function PosReturn(response:LogEventResponse):void
                    var script:Object = response.getScriptData();

                    var PlayerXPos:Number = script.POS.X;
                    var PlayerYPos:Number = script.POS.Y;

                    Player.x = PlayerXPos;
                    Player.y = PlayerYPos;

                    logger("Loading X pos = " + PlayerXPos.toString());
                    logger("Loading Y pos = " + PlayerYPos.toString());

Testing your Set and Get Functions

Set up your game to Set and Get your Player's position. Here we've made a simple game where you can save and load the sprite's location. Make sure you authenticate your player because the location will be saved against their database. Having no Player authenticated means you cannot call the Events on the Portal, even if you are connected to that Portal.


Did this page help you? Please enter your feedback below. For questions about using this part of the platform, please contact support here