Custom Chat
Note
This feature will not function unless we have enabled it for your game. If you would like to have this feature enabled, please email us at [email protected]
Custom chat API
This client API allows your game to interact with the Kongregate chat client by opening up a custom tab which you have control over. Please note that this API is optional and not necessary to have normal Kongregate chat for your game. It also does not provide chat services - it is only a way of displaying content in a chat box, though that can be used for your own chat if you hook it up to your own chat system.
Overview
The chat API allows you to display a tab in Kongregate chat. This tab is split into two components. The top half, or the "canvas" can be drawn on with various techniques including text, images, and support for the AS3 drawing API. The bottom half, called the "dialogue", is used for displaying chat style messages, whispers, etc.
The canvas
The canvas is made up of different kinds of display objects. These are basically named movie clips which can be created with the contents of an image, text, or custom art using the AS3 drawing API. The canvas supports a wide range of positioning and layout options which allow you to customize the canvas as you see fit. You also have control over how large the canvas is, and can query the API for its size.
The dialogue
The dialogue should look familiar to anyone who has used Kongregate. It allows users to type in chat input, as well as receive messages from other users, along with general information such as challenge announcements.
The chat API allows you to display custom messages from users in this dialogue. For example, it can be used to create a "match chat" between two players. When the game receives a match chat message from your opponent, it would use the chat API to display that message in the custom tab. It would also add an event listener to receive copies of any messages the user puts into the chat box.
Accessing the API
Once you have the Kongregate API loaded in your game, you can access the chat functionality by referencing the chat
member of your Kongregate API object. The following is a list of relevant API calls for managing the custom chat tab:
Positioning canvas objects
Before you can display a canvas object, you must know how to position it properly. Position objects can have various properties describing where the element should be displayed. The object can have many different properties, not all of which are supported by every element type:
Name | Type | Description |
---|---|---|
x | Integer | X offset of the object |
y | Integer | Y offset of the object |
w | Integer | Width of the object |
h | Integer | Height of the object |
parent | String | Name of the parent object, if any |
align | String | Alignment of the object relative to its parent (if any) Legal values are tl ,t ,tr ,r ,br ,b ,bl ,l , c (top left, top, top right, right, bottom right, bottom, bottom left, left, and center, respectively) |
Displaying canvas images
You can load an image from an external source into the canvas by using the displayCanvasImage
function. It takes a name
, url
, and position
object:
Name | Type | Description |
---|---|---|
name | String | Name of the canvas object |
url | String | Link to the image to display |
position | Object | Position of the object, as described above |
Example: Load an image and display it at 100,100 on the canvas
var url:String = "http://www.fake.com/picture.jpg";
var position:Object = { x:100, y:100 };
kongregate.chat.displayCanvasImage("Image", url, position);
var url = "http://www.fake.com/picture.jpg";
var position = { x:100, y:100 };
kongregate.chat.displayCanvasImage("Image", url, position);
Displaying canvas text
You can display text on the canvas with the displayCanvasText
function. It accepts the following arguments:
Name | Type | Description |
---|---|---|
name | String | Name of the canvas object |
text | String | The text to display |
position | Object | The position object, as described above |
options | Object | Any fields from an AS3 TextFormat object |
Example: Display the text Testing123
at 0,0
using the Verdana
font at size 12.
var options:Object = { font:"Verdana", size:12, color:0x7FB7FF };
kongregate.chat.displayCanvasText("Text", "Testing123", position, options);
var options = { font:"Verdana", size:12, color:0x7FB7FF };
kongregate.chat.displayCanvastext("Text", "Testing123", position, options);
Drawing custom shapes
You may use the drawCanvasObject
function to use any of the methods in the AS3 Drawing API in order to create a custom graphic shape. This function takes a name
, and a list of drawing operations and arguments:
Name | Type | Description |
---|---|---|
name | String | The name of the canvas object |
operations | Array | A nested array of drawing operations to perform, along with their arguments. |
The drawing operations must come from the following list:
beginFill
beginGradientFill
clear
curveTo
endFill
lineTo
lineStyle
moveTo
drawRect
drawRoundRect
drawCircle
drawEllipse
Example: Drawing a box using the drawCanvasObject
function
var commands:Array = [
["beginFill", 0x333333, 100],
["moveTo", 25, 25],
["lineTo", 75, 25],
["lineTo", 75, 75],
["lineTo", 25, 75],
["lineTo", 25, 25],
["endFill"]
];
kongregate.chat.drawCanvasObject("box", commands);
var commands = [
["beginFill", 0x333333, 100],
["moveTo", 25, 25],
["lineTo", 75, 25],
["lineTo", 75, 75],
["lineTo", 25, 75],
["lineTo", 25, 25],
["endFill"]
];
kongregate.chat.drawCanvasObject("box", commands);
Note
Unlike other display operations, calling
drawCanvasObject
using a name which already exists will not replace it, but instead will draw onto the existing object.
Removing a canvas object
You can remove any canvas object with the removeCanvasObject
function, which accepts the name
of the object to remove:
Name | Type | Description |
---|---|---|
name | String | The name of the canvas object to remove |
Example: Remove the object named "MyObject"
kongregate.chat.removeCanvasObject("MyObject");
kongregate.chat.removeCanvasObject("MyObject");
Canvas size
The size of the canvas can be retrieved using the getCanvasSize
function. It returns an object which has width
and height
elements. This function is only valid after the tab_visible
event has been fired for your tab.
var size:Object = kongregate.chat.getCanvasSize();
trace("Width: " + size.width + ", Height:" + size.height);
var size = kongregate.chat.getCanvasSize();
console.log("Width: " + size.width + ", Height:" + size.height);
Updated over 4 years ago