找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 248|回复: 4

[已解决] 有什么办法可以用矩阵图播放序列帧动画?

[复制链接]
发表于 2026-4-12 22:18:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 反对司令 于 2026-4-16 15:46 编辑

主要用在 CTC 上,看到一篇可以实现效果的文章,但使用后播放出现卡顿(即使不用矩阵图也很卡),前进对话后直接卡在某一帧不动。

https://zhuanlan.zhihu.com/p/362449324

难道还是只能用老方法:
[RenPy] 纯文本查看 复制代码
image ctc_animation:
    "gui/ctc/CTC_00000.png"
    0.05
    "gui/ctc/CTC_00001.png"
    0.05
    "gui/ctc/CTC_00002.png"
……

这个帧数一多代码量就长得不美观(麻烦倒是其次,可以用 ai 帮忙填)。

发表于 2026-4-16 10:29:43 | 显示全部楼层
[RenPy] 纯文本查看 复制代码
        # SpriteSheetAnimator的render方法
        def render(self, width, height, st, at):
            ## st为0时,表示组件重新显示
            if (st == 0):
                self.last_time= 0
                self.current_frame = 0
        # 省略不需要修改的部分
回复 支持 1 抱歉 0

使用道具 举报

发表于 2026-4-13 10:37:13 | 显示全部楼层
楼主光说“卡顿”, 不发代码,我也没办法知道原因……
回复 支持 抱歉

使用道具 举报

 楼主| 发表于 2026-4-15 13:35:09 | 显示全部楼层
本帖最后由 反对司令 于 2026-4-15 13:38 编辑
被诅咒的章鱼 发表于 2026-4-13 10:37
楼主光说“卡顿”, 不发代码,我也没办法知道原因……

不好意思,这就贴上代码
这个是没用矩阵图的版本,但前进到第二句话之后,就会卡住一会,再往前前进,卡顿就更严重了……
[RenPy] 纯文本查看 复制代码
init python:
    from renpy.display.layout import Crop

    class SpriteSheetAnimator(renpy.Displayable):
        def __init__(self, image_path, rows, cols, interval, loop=True, pingpong=False, **kwargs):
            super(SpriteSheetAnimator, self).__init__(**kwargs)
            self.image_path = image_path
            self.full_width, self.full_height = renpy.image_size(image_path)
            self.rows = rows
            self.cols = cols
            self.frame_w = self.full_width // cols
            self.frame_h = self.full_height // rows
            self.length = rows * cols
            self.frames = []
            for row in range(rows):
                for col in range(cols):
                    x = col * self.frame_w
                    y = row * self.frame_h
                    frame = Crop((x, y, self.frame_w, self.frame_h), renpy.displayable(image_path))
                    self.frames.append(frame)
            self.current_frame = 0
            self.last_time = 0
            self.interval = interval
            self.loop = loop
            self.pingpong = pingpong
            self.direction = 1  # 1 表示正向,-1 表示反向

        def render(self, width, height, st, at):
            if st - self.last_time >= self.interval:
                self.last_time = st
                if self.pingpong:
                    self.current_frame += self.direction
                    # 边界检测并反转方向
                    if self.current_frame >= self.length - 1:
                        self.current_frame = self.length - 1
                        self.direction = -1
                    elif self.current_frame <= 0:
                        self.current_frame = 0
                        self.direction = 1
                else:
                    self.current_frame += 1
                    if self.current_frame >= self.length:
                        if self.loop:
                            self.current_frame = 0
                        else:
                            self.current_frame = self.length - 1
            frame_render = renpy.render(self.frames[self.current_frame], width, height, st, at)
            renpy.redraw(self, 0)
            return frame_render

    # 创建动画实例,启用乒乓模式
    ctc_anim = SpriteSheetAnimator(
        "gui/ctc/ctc_sheet.png",
        rows=5,
        cols=5,
        interval=0.05,
        loop=True,
        pingpong=True   # 关键:启用往返播放
    )

# 将动画实例注册为图像
image ctc_animation = ctc_anim

# 定义 CTC 屏幕
screen ctc():
    zorder 100
    add "ctc_animation":
        xalign 0.98
        yalign 0.98
        zoom 1

回复 支持 抱歉

使用道具 举报

 楼主| 发表于 2026-4-16 15:45:51 | 显示全部楼层

非常感谢您的回复!已成功解决了卡顿的问题。
回复 支持 抱歉

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|RenPy中文空间 ( 苏ICP备17067825号 )

GMT+8, 2026-5-28 04:57 , Processed in 0.024370 second(s), 10 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表