You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GDScript:如何获取AnimatedSprite像素尺寸以限制玩家位置?

AnimatedSprite屏幕边界限制方案
  • 别只依赖transform.scale计算尺寸,得获取精灵实际渲染的真实尺寸:AnimatedSprite的每帧纹理原始尺寸可通过frames.get_frame()方法获取,再乘以scale才是最终显示的大小。
  • 修正后的位置限制代码示例:
    # 获取当前动画帧的原始尺寸
    var frame_rect = $AnimatedSprite.frames.get_frame($AnimatedSprite.animation, $AnimatedSprite.frame)
    var actual_size = frame_rect.size * $AnimatedSprite.scale
    
    # X轴边界限制(假设锚点为中心,若锚点是左上角则去掉/2)
    position.x = clamp(position.x, actual_size.x/2, screen_size.x - actual_size.x/2)
    # Y轴同理处理
    position.y = clamp(position.y, actual_size.y/2, screen_size.y - actual_size.y/2)
    
  • 锚点适配提示:如果你的AnimatedSprite锚点不是默认的(0.5,0.5),要对应调整偏移值。比如锚点在左上角(0,0)时,X轴上限直接用screen_size.x - actual_size.x,下限设为0即可。

内容的提问来源于stack exchange,提问作者deadinside

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 10:45:44