[Thảo Luận] Cần hướng dẫn về animation lúc quay người của nhân vật cùng 1 số vấn đề khác

thangngudotthangngudot Posts: 1Registered
edited July 2019 in Game Maker: Studio
Đây là code hiện tại của mình

create event:
grav = 1;spd = 7;jspd = 11;hspd = 0;hspd_dir = 1;vspd = 0;fric=1;countdown = 10;state = scr_di_chuyen_nhan_vat;
phần step event:
/// Excute the player's state
script_execute(state);
phần scr_di_chuyen_nhan_vat:
/// scr_di_chuyen_nhan_vat
var rkey = keyboard_check(vk_right);
var lkey = keyboard_check(vk_left);
var jkey = keyboard_check(vk_space);
var skey = keyboard_check(vk_shift);

// check for ground
if (place_meeting(x, y+1, obj_solid))
{
   vspd = 0;
  
   //   jumping
   if (jkey) {
        vspd = -jspd;
  }
} 
else {
  // gravity
   if (vspd < 10)
  {
    vspd += grav;  
  }
   if (keyboard_check_released(vk_space) && vspd <-4)
   { vspd = -4;
}}

//moving right (right walk)
if (rkey) {
  hspd = spd;
     // nhan vat chay ben phai (right sprint)
if (skey) {   
hspd = spd+3;
} else {
hspd = spd;
  }

  //left wall jump
  if(place_meeting(x-1, y,obj_solid) and !place_meeting(x,y+1,obj_solid) and keyboard_check_pressed(vk_space))
     vspd = -jspd;
}

//moving left (left walk)
if (lkey) {
  hspd = -spd;
        // nhan vat chay ben trai left sprint)
if (skey) {   
hspd = -spd-3;
} else {
hspd = -spd;
  }
  
  //right wall jump
  if(place_meeting(x+1, y,obj_solid) and !place_meeting(x,y+1,obj_solid) and keyboard_check_pressed(vk_space))
     vspd = -jspd;
  
}

//check for not moving
if ((!rkey && !lkey) || (rkey && lkey)) {
    hspd = 0;
}

if (hspd !=0) {
   hspd_dir = sign(hspd);
}



//Horizontal collisions
if (place_meeting(x+hspd, y, obj_solid)) {
   while (!place_meeting(x+sign(hspd), y, obj_solid )) {
        x += sign(hspd);
   }
   hspd = 0;
}

//move horizontally
x += hspd;

//Vertical collisions
if (place_meeting(x, y+vspd, obj_solid)) {
   while (!place_meeting(x, y+sign(vspd), obj_solid )) {
        y+= sign(vspd);
   }
 vspd = 0;
}

//move vertically
y += vspd;

//add this part
if (yprevious !=y) and(sprite_index = nhan_vat_leo) {
     if (!rkey and !lkey)    sprite_index = nhan_vat_nhay;
     if (!place_meeting(x-1,y,obj_solid) and !place_meeting(x+1,y,obj_solid)) sprite_index = nhan_vat_nhay;
}

// Control the sprites
if (yprevious !=y) and (sprite_index != nhan_vat_leo)
  {
    sprite_index = nhan_vat_nhay;
    image_speed = 0; }//(cu dong luc nhay) (jumping)
else
{
   if (xprevious != x) && (place_meeting(x,y+1,obj_solid ))
   {
       sprite_index = nhan_vat_di_bo;
       image_speed = .3;  //( di binh thuong) (when walking)
                  
   if (xprevious != x) && (skey) && (place_meeting(x,y+1,obj_solid ))
    {
       sprite_index = nhan_vat_chay;
       image_speed = .6;  //(luc chay) (when sprinting)
      } 
   }
   else  if (xprevious = x) && (place_meeting(x,y+1,obj_solid ) )
   {
       sprite_index = nhan_vat;
       image_speed = 0; // (dung im) (standing and do nothing)
   }
}

//control the direction the player is facing
if (xprevious < x) {
   image_xscale = 1;
} else if (xprevious > x) {
   image_xscale = -1;
   
}
// Conllisions end
var was_free = !position_meeting(x+(17*hspd_dir), yprevious-4, obj_solid );
var is_not_free = position_meeting(x+(17*hspd_dir), y-4, obj_solid);
var moving_down = yprevious < y;

if (was_free && is_not_free && moving_down) {
   hspd = 0;
   vspd = 0;
   
   // Move against the ledge
   while (!place_meeting(x+hspd_dir, y, obj_solid)) 
   {
       x+=hspd_dir;
   }
     
   //
   while (position_meeting(x+(17*hspd_dir), y-5, obj_solid))
   {
       y-=1;
   }
   
   state = bam_tuong;
   sprite_index = nhan_vat_chay;
   
   
 
   
}


  // Attack
  if (place_meeting(x, y+1, obj_solid )) {if instance_exists(Obj_nhan_vat) then
 {
 if (Input.attack) {
   hspd = 0;
   image_index = 0;
   state = danh_dam_1;
 }
  
}
}

  
  
    ////Wall Slide
if (position_meeting(bbox_right+1,y,obj_solid  )&& (rkey)) or (position_meeting(bbox_left-1,y,obj_solid )&&(lkey))
 and !(position_meeting(bbox_left,y+1,obj_solid ))
   {
       if vspd >= 2
           {
               vspd -= 1.5 //0.1 is friction
               sprite_index = nhan_vat_leo;
               
           }}

   

Comments

  • Focker_cFocker_c Posts: 1,577Registered
    Đọc qua script mình thấy bạn đã có chút kiến thức về Game Maker Language cũng như cơ sở lập trình khá ổn.
    Về phần di chuyển như vậy là ok rồi.

    Về phần hiển thị hình ảnh thì bạn nên bắt đầu tiếp cận mảng này. Bao gồm các lệnh hiển thị sprite, điều chỉnh sprite,..
    Để show một sprite : sprite_index = <hình sprite của nhân vật chẳng hạn>
    Để sprite hiển thị các frames : image_index = -1
    Để tùy chỉnh tốc độ đổi giữa các frames trong một sprite : image_speed = 0.5 (làm chậm đi một nửa)
    Để đảo (lật) sprite (như yêu cầu của bạn trong topic này) : image_xscale = -1

    Chúc bạn mau chóng tiến bộ.
Sign In or Register to comment.