Project1

标题: 求这类脚本的简便写法 [打印本页]

作者: CCD0645    时间: 2018-4-16 13:03
标题: 求这类脚本的简便写法
主要想判断地图某格没有任何事件时,才则执行
  zuo = false
$game_map.events.values.each do |e|
       if e.x == x and e.y == y
       zuo = true
       end
     end
      if zuo == false
       执行内容
     end

但感觉自己的写法有点傻,尤其是内容一多起来就显得很长,有没有简便的写法
作者: 夜狠简单    时间: 2018-4-16 13:09
$game_map.check_event(x, y)
作者: CCD0645    时间: 2018-4-16 13:21
夜狠简单 发表于 2018-4-16 13:09
$game_map.check_event(x, y)

其实主要还是想知道
a = false
for i in 1..5
if i == @event_id
a = true
end
end
if a == false
执行内容
end
像这类全否才执行的写法,该怎么简写
作者: soulsaga    时间: 2018-4-16 14:04
zuo = true if e.x == x and e.y == y
作者: 夜狠简单    时间: 2018-4-16 14:39
调用方法

RUBY 代码复制
  1. class Game_Map
  2.   def batch_check_event(start_x, start_y, width, height)
  3.     end_x = start_x + width
  4.     end_y = start_y + height
  5.     if start_x > end_x
  6.       start_x = start_x^end_x
  7.       end_x = end_x^start_x
  8.       start_x = start_x^end_x
  9.     end
  10.     if start_y > end_y
  11.       start_y = start_y^end_y
  12.       end_y = end_y^start_y
  13.       start_y = start_y^end_y
  14.     end
  15.     for x in start_x .. end_x
  16.       for y in start_y .. end_y
  17.         return true if $game_map.check_event(x, y).is_a? Integer
  18.       end
  19.     end
  20.     return false
  21.   end
  22. end

作者: 芯☆淡茹水    时间: 2018-4-16 17:59
本帖最后由 芯☆淡茹水 于 2018-4-16 18:07 编辑
CCD0645 发表于 2018-4-16 13:21
其实主要还是想知道
a = false
for i in 1..5




  1. 执行内容 unless (1..15).include?(@event_id)
复制代码


至于主楼的,貌似这样可以:

RUBY 代码复制
  1. 执行内容 unless $game_map.events.values.any?{|e| e.x == x && e.y == y}


或者用 find 也可以:

RUBY 代码复制
  1. 执行内容 unless $game_map.events.values.find{|e| e.x == x && e.y == y}






欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1