设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 1930|回复: 4
打印 上一主题 下一主题

[有事请教] 脚本中,如何获取人物在屏幕中的相对位置

[复制链接]

Lv2.观梦者

梦石
0
星屑
751
在线时间
51 小时
注册时间
2020-5-26
帖子
13
跳转到指定楼层
1
发表于 2020-12-2 01:30:04 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x


萌新,做了一个追光的效果,如图。
做法是在scene_map上添加一个window,用sprite加载一张图片,作为window的背景,设置锚点到屏幕中心,不断缩放。

当人物走到地图边缘、离开屏幕中心时,图片也应该随之调整位置。
我想在window.update()里获取人物的位置,计算出距屏幕中心的偏移量,根据这个偏移量来调整图片位置。

我只知道$gamePlayer.x可以绝对位置。
但是不知道如何获取人物在屏幕中的相对位置?

貌似在地图边缘留出足够大的空白可以规避这个问题,但我刚入门,想弄明白点儿。
还请大佬们指点一二。




另外我所在的用户组只能上传2MB的附件我也是服了。

评分

参与人数 1+1 收起 理由
wr282828 + 1 塞糖

查看全部评分

Lv2.观梦者

梦石
0
星屑
751
在线时间
51 小时
注册时间
2020-5-26
帖子
13
2
 楼主| 发表于 2020-12-2 15:16:09 | 只看该作者
本帖最后由 wangdm 于 2020-12-2 15:18 编辑



解决了。
获取"玩家画面坐标"的方法:$gamePlayer.screenX(), $gamePlayer.screenY()。

下边是主要代码。

RUBY 代码复制
  1. (() => {
  2.     const pluginName = "Spotlight";
  3.  
  4.     PluginManager.registerCommand(pluginName, "show", args => {
  5.         let scene = SceneManager._scene;
  6.         if (scene.objName() && scene.objName() === 'Scene_Map') {
  7.             scene.createSpotLightWindow();
  8.         }
  9.     });
  10.  
  11.     Scene_Map.prototype.objName = function() {
  12.         return 'Scene_Map';
  13.     };
  14.  
  15.     Scene_Map.prototype.createSpotLightWindow = function () {
  16.         this._spotLightWindow = new Window_SpotLight();
  17.         // 本window放在最下层
  18.         this.addWindowAt(this._spotLightWindow, 0);
  19.     };
  20.  
  21.     Scene_Base.prototype.addWindowAt = function(window, index) {
  22.         this._windowLayer.addChildAt(window, index);
  23.     };
  24.  
  25.     // windowNameMap 隐藏
  26.     Window_MapName.prototype.updateFadeOut = function() {
  27.         this.contentsOpacity -= 16;
  28.         if (this.contentsOpacity === 0) {
  29.             this.hide();
  30.         }
  31.     };
  32.  
  33.     function Window_SpotLight() {
  34.         this.initialize(...arguments);
  35.     }
  36.  
  37.     Window_SpotLight.prototype = Object.create(Window.prototype);
  38.     Window_SpotLight.prototype.constructor = Window_SpotLight;
  39.  
  40.     Window_SpotLight.prototype.initialize = function () {
  41.         Window.prototype.initialize.call(this);
  42.  
  43.         let x = -this._margin / 2;
  44.         let y = -this._margin / 2;
  45.         let w = $dataSystem.advanced.screenWidth;
  46.         let h = $dataSystem.advanced.screenHeight;
  47.  
  48.         this._sceenCenterX = w / 2;
  49.         this._sceenCenterY = h / 2;
  50.         // console.log(this._sceenCenterX, this._sceenCenterY);
  51.  
  52.         this.move(x, y, w, h);
  53.  
  54.         this._spotLightSprite = new Sprite(ImageManager.loadSystem('_spot_light2'));
  55.         this._spotLightSprite.x = this._sceenCenterX;
  56.         this._spotLightSprite.y = this._sceenCenterY;
  57.  
  58.         this._spotLightSprite.anchor.x = 0.5;
  59.         this._spotLightSprite.anchor.y = 0.5;
  60.  
  61.         this.addChild(this._spotLightSprite);
  62.  
  63.         this._lastPlayerX = this._sceenCenterX;
  64.         this._lastPlayerY = this._sceenCenterY + 18; // TODO 不知道这18是怎么来的
  65.  
  66.         this._canZoomOut = true;
  67.     };
  68.  
  69.     Window_SpotLight.prototype.update = function () {
  70.         if (this._lastPlayerX !== $gamePlayer.screenX()) {
  71.             this._spotLightSprite.x -= this._lastPlayerX - $gamePlayer.screenX();
  72.             this._lastPlayerX = $gamePlayer.screenX();
  73.         }
  74.         if (this._lastPlayerY !== $gamePlayer.screenY()) {
  75.             this._spotLightSprite.y -= this._lastPlayerY - $gamePlayer.screenY();
  76.             this._lastPlayerY = $gamePlayer.screenY();
  77.         }
  78.  
  79.         if (this._canZoomOut) {
  80.             this._spotLightSprite.scale.x += 0.01;
  81.             this._spotLightSprite.scale.y += 0.01;
  82.             if (this._spotLightSprite.scale.x >= 1.5) {
  83.                 this._canZoomOut = false;
  84.             }
  85.         } else {
  86.             this._spotLightSprite.scale.x -= 0.01;
  87.             this._spotLightSprite.scale.y -= 0.01;
  88.             if (this._spotLightSprite.scale.x <= 1) {
  89.                 this._canZoomOut = true;
  90.             }
  91.         }
  92.     };
  93. })();
回复 支持 反对

使用道具 举报

Lv2.观梦者

梦石
0
星屑
751
在线时间
51 小时
注册时间
2020-5-26
帖子
13
3
 楼主| 发表于 2020-12-2 16:07:46 | 只看该作者
本帖最后由 wangdm 于 2020-12-2 16:09 编辑

左了。
根本不用考虑偏移量,直接指定sprite的坐标和玩家画面坐标相同就行了。

RUBY 代码复制
  1. Window_Spotlight.prototype.update = function () {
  2.     this._spotlightSprite.x = $gamePlayer.screenX();
  3.     this._spotlightSprite.y = $gamePlayer.screenY() - 18;
  4.  
  5.     if (this._canZoomOut) {
  6.         this._spotlightSprite.scale.x += 0.01;
  7.         this._spotlightSprite.scale.y += 0.01;
  8.         if (this._spotlightSprite.scale.x >= 1.5) {
  9.             this._canZoomOut = false;
  10.         }
  11.     } else {
  12.         this._spotlightSprite.scale.x -= 0.01;
  13.         this._spotlightSprite.scale.y -= 0.01;
  14.         if (this._spotlightSprite.scale.x <= 1) {
  15.             this._canZoomOut = true;
  16.         }
  17.     }
  18. };

点评

你不如直接把Sprite加到地图上,也好过先加到window,再放到地图的周转  发表于 2020-12-5 06:37

评分

参与人数 1+1 收起 理由
wabcmcc + 1 塞糖

查看全部评分

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-5-28 22:42

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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