Project1
标题:
自己写的选项脚本,但是不知道如何释放
[打印本页]
作者:
gaofei677
时间:
2015-1-14 23:50
标题:
自己写的选项脚本,但是不知道如何释放
本帖最后由 gaofei677 于 2015-1-15 00:14 编辑
#encoding:utf-8
#==============================================================================
# ■ Window_ChoiceList
#------------------------------------------------------------------------------
# 此窗口使用于事件指令中的“显示选项”的功能。
#==============================================================================
class Window_ChoiceList < Window_Command
#--------------------------------------------------------------------------
# ● 初始化对象
#--------------------------------------------------------------------------
alias old_initialize initialize
def initialize(message_window)
@choice_closed = false
old_initialize(message_window)
self.opacity = 0
creat_choicelist_bg
update
end
#--------------------------------------------------------------------------
# ● 更新窗口的位置
#--------------------------------------------------------------------------
def update_placement
self.width = [max_choice_width + 12, 96].max + padding * 2
self.width = [width, Graphics.width].min
self.width = self.width*2
self.height = fitting_height($game_message.choices.size)*2-22
self.x = Graphics.width/2-300#Graphics.width - width
if @message_window.y >= Graphics.height / 2
self.y = @message_window.y - height-150
else
self.y = @message_window.y + @message_window.height-150
end
end
#--------------------------------------------------------------------------
# ● 绘制带有控制符的文本内容
#--------------------------------------------------------------------------
def draw_text_ex(x, y, text)
reset_font_settings
text = convert_escape_characters(text)
x = (self.width-text_size(text).width)/2 -15
pos = {:x => x, :y => y, :new_x => x, :height => calc_line_height(text)*2}
process_character(text.slice!(0, 1), text, pos) until text.empty?
end
#--------------------------------------------------------------------------
# ● 显示选项条
#--------------------------------------------------------------------------
def show_choice_pad(x,x_2,y,y_2)
return if @choice_closed == true
@choice_pad_open = [] unless @choice_pad_open.is_a?(Array)
if mouse_in?(x,x_2,y,y_2)
return if @choice_pad_sprite[@index] == nil
return if @index_last == @index
@choice_pad_sprite[@index].bitmap = Cache.ui("dialog_select_1").clone
Sound.play_cursor unless item_max == 1
if (item_max == 1) and (@choice_pad_only_one != true)
Sound.play_cursor
@choice_pad_only_one = true
end
@index_last = 1 if @index_last == nil
return if item_max == 1
if @index_last != @index
@choice_pad_sprite[@index_last].bitmap = Cache.ui("dialog_select_2").clone
@index_last = @index
end
elsif item_max == 1
return if @choice_pad_sprite == nil
return if @choice_pad_sprite[@index] == nil
return if @index_last == nil
@choice_pad_sprite[@index].bitmap = Cache.ui("dialog_select_2").clone
@choice_pad_only_one = false
end
end
#--------------------------------------------------------------------------
# ● 检查光标是否在选项内
#--------------------------------------------------------------------------
def mouse_in?(x,x_2,y,y_2)
if (Mouse.x>=x) and (Mouse.x<=x_2)
if (Mouse.y>=y) and (Mouse.y<=y_2)
return true
end
end
end
#--------------------------------------------------------------------------
# ● 获取项目的高度
#--------------------------------------------------------------------------
def item_height
line_height*2
end
#--------------------------------------------------------------------------
# ● 更新画面
#--------------------------------------------------------------------------
alias old_update update
def update
old_update
return if @choice_closed == true
a = item_rect_for_text(@index)
#show_choice_pad((a.x+110),(a.x+a.width+110),(a.y+70),(a.y+a.height+70))
#rect = item_rect_for_text(@index)
case item_max
when 4
#sprite.x = rect.x+60
#sprite.y = rect.y+70
show_choice_pad((a.x+110),(a.x+a.width+110),(a.y+70),(a.y+a.height+70))
when 3
#sprite.x = rect.x+60
#sprite.y = rect.y+70
show_choice_pad((a.x+110),(a.x+a.width+110),(a.y+70+48),(a.y+a.height+70+48))
when 2
#sprite.x = rect.x+60
#sprite.y = rect.y+165
show_choice_pad((a.x+110),(a.x+a.width+110),(a.y+70+96),(a.y+a.height+70+96))
when 1
#sprite.x = rect.x+60
#sprite.y = rect.y+70
show_choice_pad((a.x+110),(a.x+a.width+110),(a.y+70+144),(a.y+a.height+70+144))
end
creat_choicelist_bg
clear_selected_flash
end
#--------------------------------------------------------------------------
# ● 清除选择框的闪烁
#--------------------------------------------------------------------------
def clear_selected_flash
return if cursor_rect == Rect.new
@backup_cursor_rect = cursor_rect.clone
cursor_rect.empty
end
#--------------------------------------------------------------------------
# ● 判断鼠标是位于光标矩形处
#--------------------------------------------------------------------------
def mouse_out_rect?
if viewport.nil?
vp_x, vp_y = 0, 0
else
vp_x = viewport.rect.x - viewport.ox
vp_y = viewport.rect.y - viewport.oy
end
mouse_rx = ox + Mouse.x - (x + vp_x + standard_padding )
mouse_ry = oy + Mouse.y - (y + vp_y + standard_padding )
return if @backup_cursor_rect == Rect.new
return @backup_cursor_rect.x > mouse_rx ||
@backup_cursor_rect.y > mouse_ry ||
@backup_cursor_rect.x + @backup_cursor_rect.width < mouse_rx ||
@backup_cursor_rect.y + @backup_cursor_rect.height< mouse_ry
end
#--------------------------------------------------------------------------
# ● 创建选项背景
#--------------------------------------------------------------------------
def creat_choicelist_bg
return if @choice_closed == true
return if item_max == 0 or item_max==nil
return if @choice_pad_sprite.is_a?(Array)
@choice_pad_sprite = Array.new(item_max)
for x in 0..(item_max-1)
@choice_pad_sprite[x] = Sprite.new
end
i = -1
@choice_pad_sprite.each {|sprite|
i+=1
rect = item_rect_for_text(i)
case item_max
when 4
sprite.x = rect.x+60
sprite.y = rect.y+70
when 3
sprite.x = rect.x+60
sprite.y = rect.y+70+48
when 2
sprite.x = rect.x+60
sprite.y = rect.y+165
when 1
sprite.x = rect.x+60
sprite.y = rect.y+70+144
end
sprite.bitmap = Cache.ui("dialog_select_2").clone
}
end
#--------------------------------------------------------------------------
# ● 关闭窗口
#--------------------------------------------------------------------------
alias old_close close
def close
@choice_pad_sprite.each {|sprite|
sprite.bitmap.dispose
sprite.dispose
}
@choice_pad_sprite = nil
@choice_pad_open = nil
@index_last = nil
@choice_pad_only_one = nil
@backup_cursor_rect = nil
@choice_closed = true
old_close
end
end
复制代码
使用一次之后,似乎释放得不干净,再用第二次之后,就会出现各种问题,就大大们指点,如何释放干净,使用完之后能还原到 未使用过的状态……
@喵呜喵5
作者:
喵呜喵5
时间:
2015-1-15 01:11
太长懒得看,只说一句:
请把释放的代码写进dispose中
作者:
国产吃货
时间:
2015-1-15 11:02
你的脚本还是有很多差错的,比如 initialize 没必要用alias,以及善用super。
同上,太长懒得看= =
如果你最近在奋力的学脚本,可以加入下面这个讨论群(见签名档),一起学习。
作者:
taroxd
时间:
2015-1-15 12:46
本帖最后由 taroxd 于 2015-1-15 12:48 编辑
直接把位图 blt 到窗口的指定位置就好了,不需要用到 sprite(也就没有了烦人的释放问题)
关于 blt,参考帮助文档的 Bitmap#blt 以及默认脚本的用法(比如绘制脸图)
作者:
gaofei677
时间:
2015-1-16 07:07
谢啦各位,最后发现原来不是没有释放干净,而是@list没有nil
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1