GameMaker 日本語掲示板

【GML】構造体使っていますか? / 1

3 コメント
views
17 フォロー
1
hayate212 2021/05/02 (日) 04:30:17

自分はこんな感じで使っています

InstanceBase

function InstanceBase(_hp, _speed, _assets) constructor{
  // 変数
  hp = _hp;
  speed = _speed;
  assets = _assets;
  
  // 一時変数
  mpX = undefined;
  mpY = undefined;
  
  // 状態変数
  isMoving = false;
  
  // 移動開始
  static move = function (_x, _y){
    mpX = _x;
    mpY = _y;
  }

  // インスタンス共通処理
  static step = function (){
    if(mpX != undefined && mpY != undefined){
      with(other){
        if(point_distance(x, y, other.mpX, other.mpY) > other.speed){
          other.isMoving = true;
          if(x < other.mpX){
            image_xscale = 1;
          }else{
            image_xscale = -1;
          }
          if(y < other.mpY){
            sprite_index = other.assets.front;
          }else{
            sprite_index = other.assets.back;
          }
          move_towards_point(other.mpX, other.mpY, other.speed);
        }else{
          other.isMoving = false;
          other.mpX = undefined;
          other.mpY = undefined;
          speed = 0;
        }
      }
    }
  }
}

player

// create
player = new InstanceBase(10, 0.5, {front: player_front, back: player_back});

// step
if(mouse_check_button(mb_left)){
  player.move(mouse_x, mouse_y);
}

player.step();

画像

通報 ...