Skip to content

Haxe SDK

我们欢迎您将本 SDK 与任何 Haxe 游戏引擎一同使用, 比如: OpenFL, Kha, HaxeFlixel, Heaps, HaxePunk 等.

安装

从 haxelib 安装 colyseus:

haxelib install colyseus

用法

连接至服务器:

import io.colyseus.Client;
import io.colyseus.Room;

var client = new Client('ws://localhost:2567');

加入房间:

看看如何使用 State Handling 生成您的 RoomState

client.joinOrCreate("room_name", [], RoomState, function(err, room) {
    if (err != null) {
        trace("JOIN ERROR: " + err);
        return;
    }

    room.state.entities.onAdd(function(entity, key) {
        trace("entity added at " + key + " => " + entity);

        entity.onChange(function (changes) {
            trace("entity has been changed");
        });
    })

    room.state.entities.onChange(function(entity, key) {
        trace("entity changed at " + key + " => " + entity);
    })

    room.state.entities.onRemove(function(entity, key) {
        trace("entity removed at " + key + " => " + entity);
    })
});

其他房间事件

房间 state 更新:

room.onStateChange += function(state) {
  // 'state' 变量即是最新的完整的同步状态
}

从服务器广播的或者直接发给该客户端的消息:

room.onMessage("type", function (message) {
  trace(client.id + " received on " + room.name + ": " + message);
});

发生服务器错误:

room.onError += function() {
  trace(client.id + " couldn't join " + room.name);
}

客户端离开房间:

room.onLeave += function() {
  trace(client.id + " left " + room.name);
}

运行演示项目

这个 example 项目可以被编译为 html5, neko, cpp, ios 等平台应用.

它使用了 colyseus-examples 项目的 state_handler 房间, 您可以在 这里 找到.

编译演示项目为 html5

git clone https://github.com/colyseus/colyseus-hx.git
cd colyseus-hx/example/openfl
lime build project.xml html5

您可以 于此 运行实时在线演示项目.

ios 目标警告

如果想编译 iOS 应用, 您可能需要手动应用这个补丁: HaxeFoundation/hxcpp@5f63d23

详情请见: http://community.openfl.org/t/solved-system-not-available-on-ios-with-xcode-9-0/9683?source_topic_id=10046

Back to top