Project1
标题:
鼠标脚本应用ing如何获得鼠标X、Y坐标
[打印本页]
作者:
白鬼
时间:
2009-7-26 19:45
标题:
鼠标脚本应用ing如何获得鼠标X、Y坐标
本帖最后由 白鬼 于 2009-7-26 21:15 编辑
鼠标脚本核心部分
unless defined? Mouse
#==============================================================================
# ■ Mouse
#------------------------------------------------------------------------------
# 鼠标核心模块。 v1.0 by 灼眼的夏娜
#==============================================================================
module Mouse
#--------------------------------------------------------------------------
# ● DLL名
#--------------------------------------------------------------------------
DLL = "DLL/mouse"
#--------------------------------------------------------------------------
# ● 按键常量
#--------------------------------------------------------------------------
LBUTTON = 0x01
RBUTTON = 0x02
MBUTTON = 0x04
#--------------------------------------------------------------------------
# ● API函数声明
#--------------------------------------------------------------------------
MOUSE_INIT = Win32API.new(DLL,"_mouse_init","v","i")
MOUSE_EXIT = Win32API.new(DLL,"_mouse_exit","v","v")
MOUES_GET_POS = Win32API.new(DLL,"_mouse_get_pos","p","v")
MOUSE_SET_POS = Win32API.new(DLL,"_mouse_set_pos","ll","v")
MOUSE_GET_WHEEL = Win32API.new(DLL,"_mouse_get_wheel","v","i")
MOUSE_IS_OVER = Win32API.new(DLL,"_mouse_is_over","v","i")
GET_KEY_STATE = Win32API.new(DLL,"_get_key_state","i","i")
KEY_DOWN = Win32API.new(DLL,"_key_down","i","i")
KEY_UP = Win32API.new(DLL,"_key_up","i","i")
MOUSE_UPDATE = Win32API.new(DLL,"_mouse_update","v","v")
CLEAR_QUEUE = Win32API.new(DLL,"_clear_queue","v","v")
#--------------------------------------------------------------------------
# ● 鼠标系统启动
#--------------------------------------------------------------------------
def self.start
finish
@spr_cursor = Sprite.new
@spr_cursor.visible = false
@spr_cursor.bitmap = RPG::Cache.icon("cursor.png")
@spr_cursor.z = 99999
@mx = 0
@my = 0
@l_last_pressed = @l_pressed = false
@r_last_pressed = @r_pressed = false
@wheel = 0
raise "Init Mouse System Error..." if MOUSE_INIT.call != 0
end
#--------------------------------------------------------------------------
# ● 返回鼠标位置
#--------------------------------------------------------------------------
def self.pos
return @mx,@my
end
#--------------------------------------------------------------------------
# ● 鼠标系统结束
#--------------------------------------------------------------------------
def self.finish
if @spr_cursor != nil
@spr_cursor.bitmap.dispose
@spr_cursor.dispose
@spr_cursor = nil
@mx = 0
@my = 0
@l_last_pressed = @l_pressed = false
@r_last_pressed = @r_pressed = false
@wheel = 0
MOUSE_EXIT.call
end
end
#--------------------------------------------------------------------------
# ● 键按下?
#--------------------------------------------------------------------------
def self.key_down(key)
return KEY_DOWN.call(key) != 0
end
#--------------------------------------------------------------------------
# ● 键弹起?
#--------------------------------------------------------------------------
def self.key_up(key)
return KEY_UP.call(key) != 0
end
#--------------------------------------------------------------------------
# ● 连续按键?
#--------------------------------------------------------------------------
def self.press?(key)
case key
when LBUTTON
return @l_pressed
when RBUTTON
return @r_pressed
else
return false
end
end
#--------------------------------------------------------------------------
# ● 等同于:key_down
#--------------------------------------------------------------------------
def self.trigger?(key)
return key_down(key)
end
#--------------------------------------------------------------------------
# ● 滚动中?
#--------------------------------------------------------------------------
def self.scroll?
return @wheel != 0
end
#--------------------------------------------------------------------------
# ● 向上滚动?
#--------------------------------------------------------------------------
def self.scroll_up?
return @wheel > 0
end
#--------------------------------------------------------------------------
# ● 向下滚动?
#--------------------------------------------------------------------------
def self.scroll_down?
return @wheel < 0
end
#--------------------------------------------------------------------------
# ● 滚轮计数
#--------------------------------------------------------------------------
def self.scroll
return @wheel
end
#--------------------------------------------------------------------------
# ● 更新
#--------------------------------------------------------------------------
def self.update
MOUSE_UPDATE.call
pt = [0,0].pack("ll")
MOUES_GET_POS.call(pt)
@mx,@my = pt.unpack("ll")
@l_last_pressed = @l_pressed
@l_pressed = GET_KEY_STATE.call(LBUTTON) != 0
@r_last_pressed = @r_pressed
@r_pressed = GET_KEY_STATE.call(RBUTTON) != 0
@wheel = MOUSE_GET_WHEEL.call
@spr_cursor.x = @mx
@spr_cursor.y = @my
@spr_cursor.visible = MOUSE_IS_OVER.call != 0
end
#--------------------------------------------------------------------------
# ● 清空队列
#--------------------------------------------------------------------------
def self.clear_queue
CLEAR_QUEUE.call
end
end
#==============================================================================
# ■ Graphics
#------------------------------------------------------------------------------
# 图象模块,添加鼠标支持。
#==============================================================================
class << Graphics
alias origin_update update
def update
Mouse.clear_queue
origin_update
Mouse.update
end
end
#==============================================================================
# ■ Input
#------------------------------------------------------------------------------
# 按键输入模块,添加鼠标支持。
#==============================================================================
class << Input
alias origin_trigger? trigger?
def trigger?(key)
if origin_trigger?(key)
return true
end
case key
when Input::C
return Mouse.key_down(Mouse::LBUTTON)
when Input::B
return Mouse.key_down(Mouse::RBUTTON)
else
return false
end
end
end
end
Mouse.start
复制代码
鼠标脚本实现部分
class Window_Selectable
if @self_alias == nil
alias self_update update
@self_alias = true
end
def update
#self.cursor_rect.empty
self_update
if self.active and @item_max > 0
index_var = @index
tp_index = @index
mouse_x, mouse_y = Mouse.pos
mouse_not_in_rect = true
for i in 0...@item_max
@index = i
update_cursor_rect
top_x = self.cursor_rect.x + self.x + 16
top_y = self.cursor_rect.y + self.y + 16
bottom_x = top_x + self.cursor_rect.width
bottom_y = top_y + self.cursor_rect.height
if (mouse_x > top_x) and (mouse_y > top_y) and
(mouse_x < bottom_x) and (mouse_y < bottom_y)
mouse_not_in_rect = false
if tp_index != @index
tp_index = @index
$game_system.se_play($data_system.cursor_se)
end
break
end
end
if mouse_not_in_rect
@index = index_var
if Mouse.scroll_up?
# 光标向上移动
$game_system.se_play($data_system.cursor_se)
@index = (@index - @column_max + @item_max) % @item_max
end
if Mouse.scroll_down?
# 光标向上移动
$game_system.se_play($data_system.cursor_se)
@index = (@index + @column_max) % @item_max
end
update_cursor_rect
#Mouse.click_lock
else
#Mouse.click_unlock
end
end
end
end
复制代码
请问如何获取鼠标位置的坐标
(在事件里应用)
讲鼠标的X、Y坐标分别代入变量?
谢谢
作者:
猫哥哥
时间:
2009-7-26 19:50
mouse_x, mouse_y = Mouse.pos
没看见这句么……
作者:
dbshy
时间:
2009-7-26 19:51
mouse_x, mouse_y = Mouse.pos
作者:
白鬼
时间:
2009-7-26 19:53
回2楼,看见这句了,但是不会用
在事件里直接写脚本
$game_variables[2] = mouse_x
然后条件分歧判断2号变量产生不同结果,测试的时候:
undefined local variable or method 'mouse_x' for #<Interpreter:0x11dbdf8>
复制代码
作者:
白鬼
时间:
2009-7-26 20:25
本帖最后由 白鬼 于 2009-7-26 21:12 编辑
顶上去,帮下忙吧~~~真的不会用,是不是应该调用什么的啊?具体怎么做啊?
对不起我没仔细看版规,没到3个小时就顶了。
抱歉了。
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1