# Class: Backpack

Backpack()

背包信息

# Properties

# Readonly items

items: Map‹number, BackpackItem

背包物品集合

description 该集合的 key 为背包中的位置(从 0 开始),value 为该位置的物品信息。注意:指定的位置上可能为空。

Returns: Map‹number, BackpackItem

# Accessors

# description

get description(): string

背包描述信息

# 返回值:

string


# id

get id(): number

背包 ID

# 返回值:

number


# initialCount

get initialCount(): number

背包初始容量

# 返回值:

number


# maxCount

get maxCount(): number

背包最大容量

# 返回值:

number


# name

get name(): string

背包名称

# 返回值:

string

# 事件监听

# offItemCountChange

offItemCountChange(handler?: BackpackItemCountChangeEventHandler): void

取消注册 (当前背包)物品数量更新 事件处理函数

example

// 保存事件处理函数的引用 便于稍后取消注册
const handler: backpackSystem.BackpackItemCountChangeEventHandler = (event) => {
  // ...
};
backpack.onItemCountChange(handler);
backpack.offItemCountChange(handler);
1
2
3
4
5
6

# 参数:

属性 类型
handler? BackpackItemCountChangeEventHandler

# 返回值:

void


# offItemSwap

offItemSwap(handler?: BackpackItemSwapEventHandler): void

取消注册 (当前背包)物品交换 事件处理函数

example

const handler: backpackSystem.BackpackItemSwapEventHandler = (event) => {
  // ...
};
backpack.onItemSwap(handler);
backpack.offItemSwap(handler);
1
2
3
4
5

# 参数:

属性 类型
handler? BackpackItemSwapEventHandler

# 返回值:

void


# offRefresh

offRefresh(handler?: BackpackRefreshEventHandler): void

取消注册 当前背包(全量)刷新 事件处理函数

example

const handler: backpackSystem.BackpackRefreshEventHandler = (event) => {
  // ...
};
backpack.onRefresh(handler);
backpack.offRefresh(handler);
1
2
3
4
5

# 参数:

属性 类型
handler? BackpackRefreshEventHandler

# 返回值:

void


# onItemCountChange

onItemCountChange(handler: BackpackItemCountChangeEventHandler): void

注册 (当前背包)物品数量更新 事件处理函数

example

backpack.onItemCountChange((event) => {
  switch (event.type) {
    case backpackSystem.BackpackItemCountChangeEventType.Increase:
      console.log(event.itemId, event.itemUid, event.position);
      console.log('物品数量增加前后的值:', event.previousValue, event.currentValue);
      console.log('是否为新增的物品:', event.previousValue.equals(0));
      break;
    case backpackSystem.BackpackItemCountChangeEventType.Decrease:
      console.log(event.itemId, event.itemUid, event.position);
      console.log('物品数量减少前后的值:', event.previousValue, event.currentValue);
      console.log('是否为删除但物品:', event.currentValue.equals(0));
      break;
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 参数:

属性 类型
handler BackpackItemCountChangeEventHandler

# 返回值:

void


# onItemSwap

onItemSwap(handler: BackpackItemSwapEventHandler): void

注册 (当前背包)物品交换 事件处理函数

example

backpack.onItemSwap((event) => {
  console.log('对换的两个位置:', event.swappedPositions[0], swappedPositions[1]);
});
1
2
3

# 参数:

属性 类型
handler BackpackItemSwapEventHandler

# 返回值:

void


# onRefresh

onRefresh(handler: BackpackRefreshEventHandler): void

注册 当前背包(全量)刷新 事件处理函数

example

import { backpackSystem } from '@timi/mogs-sdk';

backpack.onRefresh((event) => {
  for (let position = 0; position < backpack.maxCount; position += 1) {
    const item = backpack.items.get(position);
    if (item) {
      // 重新绘制位置为 position 的物品的 UI
    }
  }
});
1
2
3
4
5
6
7
8
9
10

# 参数:

属性 类型
handler BackpackRefreshEventHandler

# 返回值:

void


# 操作触发

# swap

swap(params: Omit‹SwapBackpackItemsParams, "backpackId"›): Promise‹SwapBackpackItemsRes

(背包内)两个格子的物品交换位置

description 注意:两个待交换的格子中,可以有一个为空。

# 参数:

属性 类型
params Omit‹SwapBackpackItemsParams, "backpackId"›

# 返回值:

Promise‹SwapBackpackItemsRes