| 
 
| 赞 | 0 |  
| VIP | 3 |  
| 好人卡 | 0 |  
| 积分 | 1 |  
| 经验 | 37430 |  
| 最后登录 | 2020-10-25 |  
| 在线时间 | 267 小时 |  
 Lv1.梦旅人 
	梦石0 星屑67 在线时间267 小时注册时间2008-7-28帖子13 | 
| 
本帖最后由 wanghanqing 于 2012-7-10 10:23 编辑
x
加入我们,或者,欢迎回来。您需要 登录 才可以下载或查看,没有帐号?注册会员  
 潜水很久了,试发个帖子,呵呵
 目标:实现vx下的真八方向行走
 1.素材准备:行走图一张
 就拿这张8*8的行走图好了
 
   把它命名为“$楚歌”复制到Graphics\Characters文件夹下
 2.修改Game_Character类
 原脚本(以向左下移动为例)是这样的:
 修改为复制代码#--------------------------------------------------------------------------
# ● 向左下移动
#--------------------------------------------------------------------------
def move_lower_left
  unless @direction_fix
   @direction = (@direction == 6 ? 4 : @direction == 8 ? 2 : @direction)
  end
if (passable?(@x, @y+1) and passable?(@x-1, @y+1)) or
(passable?(@x-1, @y) and passable?(@x-1, @y+1))
  @x -= 1
  @y += 1
  increase_steps
  @move_failed = false
else
  @move_failed = true
end
end
主要是添加了两个条件分歧来增强手感复制代码def move_lower_left
#~ unless @direction_fix
#~ @direction = (@direction == 6 ? 4 : @direction == 8 ? 2 : @direction)
#~ end
@direction=1                    
if (passable?(@x, @y+1) and passable?(@x-1, @y+1)) or
(passable?(@x-1, @y) and passable?(@x-1, @y+1))
@x -= 1
@y += 1
increase_steps
@move_failed = false
elsif (passable?(@x,@y+1)==false)and (passable?(@x-1,@y+1)==false)and passable?(@x-1,@y)
@x-=1
increase_steps
@move_failed=false
elsif (not passable?(@x-1,@y+1))and (not passable?(@x-1,@y))and passable?(@x,@y+1)
@y+=1
increase_steps
@move_failed=false
else
@move_failed = true
end
end
其它三个斜方向可同理修改
 3.修改Game_Player类
 将原脚本的case Input.dir4改为case Input.dir8,用以实现八方向输入判断复制代码#--------------------------------------------------------------------------
# ● 方向键移动处理
#--------------------------------------------------------------------------
def move_by_input
return unless movable?
return if $game_map.interpreter.running?
case Input.dir8
when 2; move_down
when 4; move_left
when 6; move_right
when 8; move_up
when 1; move_lower_left
when 3; move_lower_right
when 7; move_upper_left
when 9; move_upper_right
end
end
自然下面要多出四种分歧when1,3,7,9对应四个斜方向
 4.修改Sprite_Character类,让八方向和行走图对应起来
 if @tile_id > 0复制代码#--------------------------------------------------------------------------
# ● 更新传送源的位图 
#--------------------------------------------------------------------------
def update_bitmap
if @tile_id != @character.tile_id or
@character_name != @character.character_name or
@character_index != @character.character_index
@tile_id = @character.tile_id
@character_name = @character.character_name
@character_index = @character.character_index
if @tile_id > 0
sx = (@tile_id / 128 % 2 * 8 + @tile_id % 8) * 32;
sy = @tile_id % 256 / 8 % 16 * 32;
self.bitmap = tileset_bitmap(@tile_id)
self.src_rect.set(sx, sy, 32, 32)
self.ox = 16
self.oy = 32
else
self.bitmap = Cache.character(@character_name)
sign = @character_name[/^[\!\$]./]
if sign != nil and sign.include?('$')
@cw = bitmap.width / 8
@ch = bitmap.height / 8
else
@cw = bitmap.width / 12
@ch = bitmap.height / 8
end
self.ox = @cw / 2
self.oy = @ch
end
end
end
...
 else
 ...
 else
 ...
 end
 这个条件分歧大意就是
 如果传送的元件是地图元件的话
 ...
 如果不是(言下之意就是行走图)
 ...
 结束
 行走图又分两种
 一种是8个角色在一张图上
 一种是1个角色在一张图上,就像本例中的
 所以出现两个else
 if sign != nil and sign.include?('$')
 这句话是说如果行走图名称中包含'$'字符
 就像本例中的$楚歌
 所以把这句话后面的两句改为
 @cw = bitmap.width / 8
 @ch = bitmap.height / 8
 这样就可以把行走图横竖各分为八块
 5.还是修改Sprite_Character类,调整传送源矩形
 被注释的是原脚本复制代码#--------------------------------------------------------------------------
# ● 更新传送源的矩形
#--------------------------------------------------------------------------
def update_src_rect
if @tile_id == 0
sx = @character.pattern * @cw
case @character.direction
when 2
sy = 0 * @ch
when 4
sy = 1 * @ch
when 6
sy = 2 * @ch
when 8
sy = 3 * @ch
when 1
sy = 4 * @ch
when 3
sy = 5 * @ch
when 7
sy = 6 * @ch
when 9
sy = 7 * @ch
end
self.src_rect.set(sx, sy, @cw, @ch)
#~ index = @character.character_index
#~ pattern = @character.pattern < 3 ? @character.pattern : 1
#~ sx = (index % 4 * 3 + pattern) * @cw
#~ sy = (index / 4 * 4 + (@character.direction - 2) / 2) * @ch
#~ self.src_rect.set(sx, sy, @cw, @ch)
end
end
新加的脚本很简单
 就是根据角色的朝向来确定sx,sy
 sx,sy就是确定下在行走图上以那个点为左上角的坐标来传送位图
 
 6.在Game_Character类
 def update_animation
 ...
 @pattern = (@pattern + 1) % 4
 ...
 end
 有这么一句
 把其中的4改成8
 经过以上修改
 就可以实现真八方向行走效果了
 
 不足:
 1.素材只能是8*8的行走图,由于没有针对默认的素材做修改,所以使用默认素材会出错
 有兴趣的同学可以自行修改
 2.不能斜方向触发事件,待我再研究研究……
 3.我也是新手,不足之处请多多包容
 
 
 晚上又有点心得,解决了斜方向触发事件的问题
 关键在于修改Game_Player类
 以上代码,注释掉的是原代码,用于计算出位于角色前方的事件坐标复制代码  def check_event_trigger_there(triggers)
    return false if $game_map.interpreter.running? #如果某事件正在执行,则不被触发
    result = false
    case @direction
      when 1
        front_x=@x-1
        front_y=@y+1
      when 2
        front_x=@x
        front_y=@y+1
      when 3
        front_x=@x+1
        front_y=@y+1
      when 4
        front_x=@x-1
        front_y=@y
      when 6
        front_x=@x+1
        front_y=@y
      when 7
        front_x=@x-1
        front_y=@y-1
      when 8
        front_x=@x
        front_y=@y-1
      when 9
        front_x=@x+1
        front_y=@y-1
    end
#~     front_x = $game_map.x_with_direction(@x, @direction)
#~     front_y = $game_map.y_with_direction(@y, @direction)
修改为上方case...end这一块
 由于能力有限,所以写成散的
 主要就是根据角色的朝向来确定角色前方的事件坐标
 很简单,不多解释了
 写了这么多,希望大家多多支持!
 
 
 | 
 |