arrays - 滚动数组
问题描述
所以我在 GameMaker 中有一个项目,里面有一个聊天框。为此的消息存储在一个数组中。我希望能够滚动浏览此数组,以便查看之前的聊天消息。
这是我目前拥有的:
创建活动
chatLog[0] = "";
chatIndex = 0;
步骤事件
if (chatIndex > 0) {
if (mouse_wheel_down()) {
chatIndex--;
}
}
if (chatIndex < array_length_1d(chatLog) - 1) {
if (mouse_wheel_up()) {
chatIndex++;
}
}
var _maxLines = 5;
for (i = 0; i < _maxLines; i++) {
if (i > (array_length_1d(chatLog) - 1)) { exit; }
var _chatLength = array_length_1d(chatLog) - 1;
draw_text(0, 50 - chatHeight, chatLog[_chatLength - i + chatIndex]);
}
解决方案
首先,为了方便将消息添加到前面/从后面删除(一旦太多),我们假设日志是一个列表,第 0 项是最新消息,
chatLog = ds_list_create();
chatIndex = 0;
for (var i = 1; i <= 15; i++) {
ds_list_insert(chatLog, 0, "message " + string(i));
}
然后,Step Draw 事件可以使用列表中的信息来钳制滚动偏移范围并绘制项目:
var maxLines = 5;
// scrolling:
var dz = (mouse_wheel_up() - mouse_wheel_down()) * 3;
if (dz != 0) {
chatIndex = clamp(chatIndex + dz, 0, ds_list_size(chatLog) - maxLines);
}
// drawing:
var i = chatIndex;
var _x = 40;
var _y = 200;
repeat (maxLines) {
var m = chatLog[|i++];
if (m == undefined) break; // reached the end of the list
draw_text(_x, _y, m);
_y -= string_height(m); // draw the next item above the current one
}
推荐阅读
- html - 如何添加新项目以实现 CSS 集合?
- tableau-api - 如何在 Tableau 中对 2 列求和并在第三列中显示?
- javascript - 在 Firefox 中,如何停止 jQuery 滑块并检查其内容?
- r - 使用 R 将 VCI JSON.GZ 文件转换为 CSV
- http - HTTP 响应状态码 226 的用途是什么?
- python - 如何向使用 Keras 构建的 CNN 模型添加第二个输入参数(第一个是图像)?
- cassandra - Cassandra 具有相同分区键的多个表
- javascript - 需要使用 React 定位表中的特定子单元格
- angular - Angular 4+中是否有可能有“伪路由”
- canvas - 如何使用 Fabric.js 作为从 cdn/script 标签导入的模块?