本帖最后由 亿万星辰 于 2016-10-28 11:20 编辑
系统分析一下这个问题
1、眼看到这个问题,就要先考虑下它涉及到的一些知识点:
朝向、坐标、事件判断
2、逐一分析知识点:
朝向:角色共有四个朝向,有对应的方法可调用:$game_player.direction;
坐标:左上角为原点,↘方向为XY坐标正方向同样也有对应的方法可以调用让我们来获取角色的坐标:$game_player.x, $game_player.y;
事件判断:如何判断某个坐标处是否存在一个事件,系统正好也提供了这个方法$game_map.check_event(x, y),返回值为事件ID,无事件则返回nil;
3、有了这些东西,我们可以清点一下思路了。我们要判断角色是否面对着一个事件,其实就是:根据角色朝向不同计算出角色面前一格坐标内是否存在一个事件,具体来看:
首先我们要获取到角色面前这一格的坐标,而角色的方向又有四种可能,细想一下其实可以把x坐标和y坐标单独开来分析,如果角色面朝左则角色当前的x坐标-1,朝右就+1;同理y轴方向朝上的话-1,朝下就+1。这样我们角色面前一格的坐标就的出来了,再运用一下“:”这个三目运算符,角色面前一格的坐标就是
$game_player.x+($game_player.direction == 6 ? 1 : ($game_player.direction == 4 ? -1 : 0)),$game_player.y + ($game_player.direction == 2 ? 1 : ($game_player.direction == 8 ? -1 : 0))
$game_player.x+($game_player.direction == 6 ? 1 : ($game_player.direction == 4 ? -1 : 0)),$game_player.y + ($game_player.direction == 2 ? 1 : ($game_player.direction == 8 ? -1 : 0))
看着挺长,不过还好事件中条件分歧里的那个脚本栏里能输不少字……
接着就是把这一串坐标放到$game_map.check_event这个方法的参数里,得到
$game_map.check_event($game_player.x+($game_player.direction == 6 ? 1 : ($game_player.direction == 4 ? -1 : 0)),$game_player.y + ($game_player.direction == 2 ? 1 : ($game_player.direction == 8 ? -1 : 0)))
$game_map.check_event($game_player.x+($game_player.direction == 6 ? 1 : ($game_player.direction == 4 ? -1 : 0)),$game_player.y + ($game_player.direction == 2 ? 1 : ($game_player.direction == 8 ? -1 : 0)))
当然还要记得我们的初衷是判断面前是否有一个事件,所以最终就是这样的
!$game_map.check_event($game_player.x+($game_player.direction == 6 ? 1 : ($game_player.direction == 4 ? -1 : 0)),$game_player.y + ($game_player.direction == 2 ? 1 : ($game_player.direction == 8 ? -1 : 0))).nil?
!$game_map.check_event($game_player.x+($game_player.direction == 6 ? 1 : ($game_player.direction == 4 ? -1 : 0)),$game_player.y + ($game_player.direction == 2 ? 1 : ($game_player.direction == 8 ? -1 : 0))).nil?
这样判断为“真”的情况就是说面前真的有事件,如果为“假”则说面面前空无一物。
最后公共并行事件里以这个作为事件操作--条件分歧--脚本的判断内容,然后在处理中打开对应的开关就好了。 |