matt72228 偶而上來逛逛的過客
註冊時間: 2011-04-20 文章: 6 來自: 桃園 183.11 果凍幣
|
發表於: 2011-4-22, PM 4:26 星期五 文章主題: RPGMaker XP - XAS - L2 - 建立隨機產生怪物 |
|
|
原始技術材自於:http://www.66rpg.com/htm/news700.htm
但是有經過相當程度的修改!
若有侵權,還請告知,將立即移除本文章,感謝:)
本文章目的:XAS中,如何隨機在地圖上隨機產生怪物。
要知道,XAS的怪物在地圖上也是個事件,如果想在地圖上生出怪物,而不是採用GUI點出來,那說穿了就是在地圖上隨機產生"事件"的功能。
怎麼做到呢?
1. 首先在F11(也就是Script Editor)中,新增一個MONSTER_EVENTS的Script,輸入以下函式:
代碼: |
def createMonster(map_id, event_id, x, y)
map = load_data(sprintf("Data/Map%03d.rxdata", map_id))
temp = Marshal.load(Marshal.dump(map))
if !$game_system.map_events_number
$game_system.map_events_number = {}
end
sum = $game_system.map_events_number
m_id = $game_map.map_id
sum[m_id] == nil ? sum[m_id] = 2000 : sum[m_id] += 1
new_event_id = sum[m_id]
temp.events[event_id].id = new_event_id
temp.events[event_id].x,temp.events[event_id].y = x,y
$game_map.events[new_event_id] = Game_Event.new(m_id,temp.events[event_id])
$scene.spriteset.dispose
$scene.spriteset = Spriteset_Map.new
end
|
2.建立怪物庫,建立一張新地圖,我這張地圖取名叫做MONSTERS,地圖編號為190。
並且將Toturial - 08中的史萊姆貼到MONSTERS這張地圖上,所以該事件編號會是1。
此時只要代入createMonster(map_id, event_id, x, y)即可產生怪物。
3.這個時候回到你的地圖上(好比說MAP_TEST),新增一個事件,觸發採用Parallel Process,輸入以下事件:
代碼: |
Control Variables: [1000: Test] = 0
Loop
Wait: 30frames
Control Branch: Variables: [1000: Test] <= 10
#我的地圖是30x30,所以rX為0~29,rY為0~29
Control Variables: [0998: rX] = Random No. (0...29)
Control Variables: [0998: rY] = Random No. (0...29)
Script: createMonster(190, 2, $game_variables[998].to_i, $game_variables[999].to_i)
Control Variables: [1000: Test] += 1
Else
Branch End
Repeat Above
|
這樣就會每秒鐘產生一個史萊姆,且最高到30隻囉。
--------------------------------------
上述方案在過場時,怪物會消失,此時我們必須儲存事件,所以要做下述修改:
1.在Game_System中加入:
代碼: |
attr_accessor :map_events_number
attr_accessor :map_data
|
2.在Scene_Map中加入:
[size=/16]
代碼: |
attr_accessor :spriteset
|
[size=16]
3.在Game_MAP中,修改以下:
3.1修改setup中,load_data的行為:
代碼: |
def setup(map_id)
# Put map ID in @map_id memory
@map_id = map_id
# Load map from file and set @map
#將原本以下這行移除
#@map = load_data(sprintf("Data/Map%03d.rxdata", @map_id))
#加入以下這行
@map = map_data_load(@map_id)
...
|
3.2加入map_data_load的相關函式:
代碼: |
def map_data_load(map_id)
if !$game_system.map_data
$game_system.map_data = {}
self.map_data_init
end
return $game_system.map_data[map_id]
end
def map_data_init
for i in 1..999
begin
map = load_data(sprintf("Data/Map%03d.rxdata", i))
rescue Errno::ENOENT
next
end
$game_system.map_data[i] = Marshal.load(Marshal.dump(map))
end
end
|
5.在MONSTER_EVENTS中加入:
代碼: |
def createMonsterForever(map_id, event_id, x, y)
return unless is_map?
map = map_data_load(map_id)
temp = Marshal.load(Marshal.dump(map))
if !$game_system.map_events_number
$game_system.map_events_number = {}
end
sum = $game_system.map_events_number
m_id = $game_map.map_id
sum[m_id] == nil ? sum[m_id] = 2000 : sum[m_id] += 1
new_event_id = sum[m_id]
temp.events[event_id].id = new_event_id
temp.events[event_id].x,temp.events[event_id].y = x,y
$game_map.events[new_event_id] = Game_Event.new(m_id,temp.events[event_id])
now_map = self.map_data_load(m_id)
now_map.events[new_event_id] = temp.events[event_id]
self.map_data_save(m_id,now_map)
refresh_map
end
def map_data_init
for i in 1..999
begin
map = load_data(sprintf("Data/Map%03d.rxdata", i))
rescue Errno::ENOENT
next
end
$game_system.map_data[i] = Marshal.load(Marshal.dump(map))
end
end
def map_data_save(map_id,event)
$game_system.map_data[$game_map.map_id] = event
end
def map_data_load(map_id)
if !$game_system.map_data
$game_system.map_data = {}
self.map_data_init
end
return $game_system.map_data[map_id]
end
def delete_events(event_id)
return unless self.is_map?
$game_map.events.delete(event_id)
self.refresh_map
end
def refresh_map
return unless self.is_map?
$scene.spriteset.dispose
$scene.spriteset = Spriteset_Map.new
end
def is_map?
(return true) if $scene.is_a?(Scene_Map)
return false
end
|
6.此時你就可以修改上面的函式:
代碼: |
createMonster(190, 2, $game_variables[998].to_i, $game_variables[999].to_i)
|
變成:
代碼: |
createMonsterForever(190, 2, $game_variables[998].to_i, $game_variables[999].to_i)
|
這樣就可以於過場時,保留那些怪物(事件)囉。 _________________ JoinUs - 一起做遊戲吧!
誰說我們只要程式設計師?
程式、腳本、美素、音樂、~只要你有興趣都可以!
意者請洽 matt72228@gmail.com |
|