実験 450x800 公開中

アナログスティック実験

投稿者:とりか 投稿日時: 2013/08/23 13:40:01
画面タップでタップ位置にアナログスティックの円が出現します
円よりも外側に指を移動するとその方向に倒したことになり、入力を行います
今回は入力領域を4方向に分割する処理でキャラを移動させています

アニメーション未実装、本当は内側の円が角度に応じて精密に動いてくれるものを作りたかった
現在実装手法検討中です
閲覧: 133 評価: 20
  • Starwhite
  • Starwhite
  • Starwhite
  • Starwhite
  • Starwhite
  • Starwhite
  • Starwhite
  • Starwhite
  • Starwhite
  • Starwhite
あなたはまだ評価していません。
# <とりかさんのCoR-RPG> game.draw_collision_mode true # ゲームに使用する画像のローディング game.loading do |loader| loader.add :window, :system => "window" loader.add :gui_item, :system => "gui_item" loader.add :human_image, 74 loader.add :GameWindow_image, 85 loader.add :image_AU, 67 loader.add :image_AR, 68 loader.add :image_AL, 69 loader.add :image_AD, 70 loader.add :image_ET, 72 loader.add :image_BK, 73 end # ゲームの初期化 game.on_init do set_window_image :window set_gui_image :gui_item # オープニングのシーンへ scene_change :start_scene end # 大域変数 AR_Pos = [128, 800 - 128] AR_Rad = 64 AR_Col = 48 BT_Pos = [450 - 128, 800 - 160] BT_Rad = 64 MasuSize = 32 CharaMoveD = 2 CharaAnimeF = 4 Tyuusin = [392, 237] Chara_now = [6, 6] Move_status = 0 Move_Mode = 0 TotchDown_Pos = [0, 0] TotchMove_Pos = [0, 0] TotchOn_Flag = 0 StickVector = [0, 0] SmallerR = 32 # ベクトルの長さを求めます。 def vector_len a Math.sqrt a[0] * a[0] + a[1] * a[1] end # ベクトルの足し算 # ここではベクトルを、長さ2の配列で扱っています。 def vector_add a, b [a[0] + b[0], a[1] + b[1]] end # ベクトルの引き算 def vector_sub a, b [a[0] - b[0], a[1] - b[1]] end # ベクトルとスカラーの掛け算 def vector_mul a, b [a[0] * b, a[1] * b] end # ベクトルの正規化、つまりベクトルの長さを1にする。 def vector_normal v l = vector_len v if l > 0 vector_mul v, 1.0 / l else [0, 0] end end # オープニングシーン scene :start_scene do |scene| scene.on_init do sp = scene.add :image, :template => :human, :center_position => [18 + 32 * Chara_now[0], 18 + 32 * Chara_now[1]] aa = scene.add :image, :template => :GameWindow, :position => [0, 0] au = scene.add :image, :template => :ARROW_U, :center_position => [AR_Pos[0], AR_Pos[1] - AR_Rad] ar = scene.add :image, :template => :ARROW_R, :center_position => [AR_Pos[0] + AR_Rad, AR_Pos[1]] al = scene.add :image, :template => :ARROW_L, :center_position => [AR_Pos[0] - AR_Rad, AR_Pos[1]] ad = scene.add :image, :template => :ARROW_D, :center_position => [AR_Pos[0], AR_Pos[1] + AR_Rad] be = scene.add :image, :template => :Button_ET, :center_position => [BT_Pos[0] + BT_Rad, BT_Pos[1]] bb = scene.add :image, :template => :Button_BK, :center_position => [BT_Pos[0], BT_Pos[1] + BT_Rad] AR_SUM = scene.add :collision, :layer_order => -1, :center_position => [250, 400], :scale => [500, 800] be.event :on_touch_down do |event| speak "こんにちわ!" end bb.event :on_touch_down do |event| speak "ゲームを終了します。" # ゲーム終了 game.change_project "start_menu" end AR_SUM.event :on_step do |event| if (TotchOn_Flag == 1) StickVector = vector_sub TotchMove_Pos, TotchDown_Pos SmallCheck = vector_len StickVector StickVector = vector_normal StickVector if StickVector[1] > 0.7 Move_Mode = 8 end if -0.7 > StickVector[1] Move_Mode = 2 end if -0.7 > StickVector[0] Move_Mode = 4 end if StickVector[0] > 0.7 Move_Mode = 6 end if SmallerR > SmallCheck Move_Mode = 0 end end if (0 < Chara_now[1] && Move_status == 0 && Move_Mode == 2) sp.change_motion :Move_up Chara_now[1] -= 1 end if (Chara_now[0] < 12 && Move_status == 0 && Move_Mode == 6) sp.change_motion :Move_right Chara_now[0] += 1 end if (0 < Chara_now[0] && Move_status == 0 && Move_Mode == 4) sp.change_motion :Move_left Chara_now[0] -= 1 end if (Chara_now[1] < 12 && Move_status == 0 && Move_Mode == 8) sp.change_motion :Move_down Chara_now[1] += 1 end end AR_SUM.event :on_touch_down do |event| TotchDown_Pos = [event.screen_x, event.screen_y] TotchMove_Pos = [event.screen_x, event.screen_y] TotchOn_Flag = 1 Joy_Stick1 = scene.add :image, :template => :JOY_S_Tmp, :name => :Stick1, :center_position => TotchDown_Pos Joy_Stick2 = scene.add :image, :template => :JOY_S_Tmp2nd, :name => :Stick2, :center_position => TotchDown_Pos end AR_SUM.event :on_touch_move do |event| TotchMove_Pos = [event.screen_x, event.screen_y] end AR_SUM.event :on_touch_up do |event| TotchOn_Flag = 0 Move_Mode = 0 #scene.delete :Stick1 Joy_Stick1.delete Joy_Stick2.delete end end end # 表示する人間のテンプレート sprite_template :human do |st| st.texture :human_image st.src_size 20, 28 st.dest_size 40, 56 st.center_offset 5, 25 st.layer_order 10 st.collision :rect, :position => [4, 24], :width => [MasuSize, MasuSize] st.animation :stop do |commands| commands.loop false commands.copy_rect :frame => CharaAnimeF / 2, :src => [1, 0] commands.wait_animation end st.animation :down do |commands| commands.loop false commands.copy_rect :frame => CharaAnimeF / 2, :src => [1, 0] commands.wait_animation commands.copy_rect :frame => CharaAnimeF, :src => [2, 0] commands.wait_animation commands.copy_rect :frame => CharaAnimeF, :src => [1, 0] commands.wait_animation commands.copy_rect :frame => CharaAnimeF, :src => [0, 0] commands.wait_animation commands.copy_rect :frame => CharaAnimeF / 2, :src => [1, 0] commands.wait_animation end st.animation :up do |commands| commands.loop false commands.copy_rect :frame => CharaAnimeF / 2, :src => [1, 3] commands.wait_animation commands.copy_rect :frame => CharaAnimeF, :src => [2, 3] commands.wait_animation commands.copy_rect :frame => CharaAnimeF, :src => [1, 3] commands.wait_animation commands.copy_rect :frame => CharaAnimeF, :src => [0, 3] commands.wait_animation commands.copy_rect :frame => CharaAnimeF / 2, :src => [1, 3] commands.wait_animation end st.animation :left do |commands| commands.loop false commands.copy_rect :frame => CharaAnimeF / 2, :src => [1, 1] commands.wait_animation commands.copy_rect :frame => CharaAnimeF, :src => [2, 1] commands.wait_animation commands.copy_rect :frame => CharaAnimeF, :src => [1, 1] commands.wait_animation commands.copy_rect :frame => CharaAnimeF, :src => [0, 1] commands.wait_animation commands.copy_rect :frame => CharaAnimeF / 2, :src => [1, 1] commands.wait_animation end st.animation :right do |commands| commands.loop false commands.copy_rect :frame => CharaAnimeF / 2, :src => [1, 2] commands.wait_animation commands.copy_rect :frame => CharaAnimeF, :src => [2, 2] commands.wait_animation commands.copy_rect :frame => CharaAnimeF, :src => [1, 2] commands.wait_animation commands.copy_rect :frame => CharaAnimeF, :src => [0, 2] commands.wait_animation commands.copy_rect :frame => CharaAnimeF / 2, :src => [1, 2] commands.wait_animation end # キャラモーション st.motion :default do |commands| commands.loop false commands.proc_call do |sprite| sprite.change_animation :stop end end # Move_up st.motion :Move_up do |commands| commands.loop false commands.proc_call do |sprite| Move_status = 1 sprite.change_animation :up end commands.move_by_v 0, -MasuSize, CharaMoveD commands.wait_motion commands.proc_call do |sprite| Move_status = 0 end end # Move_down st.motion :Move_down do |commands| commands.loop false commands.proc_call do |sprite| Move_status = 1 sprite.change_animation :down end commands.move_by_v 0, MasuSize, CharaMoveD commands.wait_motion commands.proc_call do |sprite| Move_status = 0 end end # Move_right st.motion :Move_right do |commands| commands.loop false commands.proc_call do |sprite| Move_status = 1 sprite.change_animation :right end commands.move_by_v MasuSize, 0, CharaMoveD commands.wait_motion commands.proc_call do |sprite| Move_status = 0 end end # Move_left st.motion :Move_left do |commands| commands.loop false commands.proc_call do |sprite| Move_status = 1 sprite.change_animation :left end commands.move_by_v -MasuSize, 0, CharaMoveD commands.wait_motion commands.proc_call do |sprite| Move_status = 0 end end end # 表示するゲーム画面のテンプレ sprite_template :GameWindow do |st| st.texture :GameWindow_image st.src_size 450, 800 st.dest_size 450, 800 end # 入力ボタンのテンプレ sprite_template :ARROW_U do |st| st.texture :image_AU st.src_size 48, 48 st.dest_size 64, 64 st.center_offset 64 / 2, 64 / 2 st.collision :circle, :mode => :center, :position => [0, 0], :radius => AR_Col end sprite_template :ARROW_R do |st| st.texture :image_AR st.src_size 48, 48 st.dest_size 64, 64 st.center_offset 64 / 2, 64 / 2 st.collision :circle, :mode => :center, :position => [0, 0], :radius => AR_Col end sprite_template :ARROW_L do |st| st.texture :image_AL st.src_size 48, 48 st.dest_size 64, 64 st.center_offset 64 / 2, 64 / 2 st.collision :circle, :mode => :center, :position => [0, 0], :radius => AR_Col end sprite_template :ARROW_D do |st| st.texture :image_AD st.src_size 48, 48 st.dest_size 64, 64 st.center_offset 64 / 2, 64 / 2 st.collision :circle, :mode => :center, :position => [0, 0], :radius => AR_Col end sprite_template :Button_ET do |st| st.texture :image_ET st.src_size 64, 64 st.dest_size 64, 64 st.center_offset 64 / 2, 64 / 2 st.collision :circle, :mode => :center, :position => [0, 0], :radius => 32 end sprite_template :Button_BK do |st| st.texture :image_BK st.src_size 64, 64 st.dest_size 64, 64 st.center_offset 64 / 2, 64 / 2 st.collision :circle, :mode => :center, :position => [0, 0], :radius => 32 end sprite_template :JOY_S_Tmp do |st| st.collision :circle, :mode => :center, :position => [0, 0], :radius => SmallerR end sprite_template :JOY_S_Tmp2nd do |st| st.collision :circle, :mode => :center, :position => [0, 0], :radius => (SmallerR / 2) #st.motion :default do |commands| # commands.loop true # commands.wait_frame 1 # commands.proc_call do |sprite| # Pos = Joy_Stick1.get_center_position # commands.move_to Pos[0] + (StickVector[0] * (SmallerR / 2)), # Pos[1] + (StickVector[1] * (SmallerR / 2)), 1 # commands.wait_frame 10 # # end #end # #st.motion :default2 do |commands| # commands.loop true # commands.wait_frame 10 # commands.proc_call do |sprite| # sprite.change_motion :default # end #end end
コード一覧
  • start.rb

コメントするには、ログインする必要があります。

コメント一覧
お知らせ

2014/03/04 ver. 0.1.39 を公開しました!
・0.1.36でWindowsで起動しない問題を修正しました
(Android版はバージョン番号のみの変更です。)

2014/03/04 ver. 0.1.36 を公開しました!
・アプリケーションアイコンを変更しました
・セーブ・ロードを繰り返すとアプリが強制終了する問題を修正しました
・他、重大なバグを修正しました

ダウンロードはこちらから。

2013/07/17 Code on Rmakeをα公開しました!