Página 1 de 1

Sistema de tiro com bug

Enviado: Ter Jan 01, 2019 11:00 pm
por AragaSam
Criei um sistema de atirar laser, porém quando estou andando e atiro pra cima ou pra baixo o tiro não sai, somente se ando pra esquerda ou direita, quando parado não importa a direção funciona normalmente.

O código é o seguinte: (bem grande, mas não sei como diminuir e ainda vou incluir uma pausa durante o momento em que esta atirando)

////////////////////
// ATIRAR LASER ///
////////////////////

if keyboard_check_pressed (ord ("K")) and global.laser>0 {
if sprite_index = spr_player_parado_direita {
     global.laser-=1 {
      with ( instance_create_layer (x,y-16, "Instances", obj_laser)) {
         motion_set (0, 4) {
 sprite_index = spr_laser_horizontal
   }
  }
 }
}
}

if keyboard_check_pressed (ord ("K")) and global.laser>0 {
  if sprite_index = spr_player_parado_esquerda {
     global.laser-=1 {
      with ( instance_create_layer (x,y-16, "Instances", obj_laser)) {
         motion_set (180, 4) {
 sprite_index = spr_laser_horizontal
}
  }
 }
}
}

if keyboard_check_pressed (ord ("K")) and global.laser>0 {
if sprite_index = spr_player_parado_cima {
     global.laser-=1 {
      with ( instance_create_layer (x,y-16, "Instances", obj_laser)) {
         motion_set (90, 4) {
}
  }
 }
}
}

if keyboard_check_pressed (ord ("K")) and global.laser>0 {
if sprite_index = spr_player_parado_baixo {
     global.laser-=1
      with ( instance_create_layer (x,y-16, "Instances", obj_laser)) {
         motion_set (270, 4) {
  }
 }
}
}

// atirar andando

if keyboard_check_pressed (ord ("K")) and global.laser>0 {
if sprite_index = spr_player_movendo_direita {
     global.laser-=1 {
      with ( instance_create_layer (x,y-16, "Instances", obj_laser)) {
         motion_set (0, 4) {
 sprite_index = spr_laser_horizontal
}
  }
 }
}
}

if keyboard_check_pressed (ord ("K")) and global.laser>0 {
if sprite_index = spr_player_movendo_esquerda {
     global.laser-=1 {
      with ( instance_create_layer (x,y-16, "Instances", obj_laser)) {
         motion_set (180, 4) {
 sprite_index = spr_laser_horizontal
}
  }
 }
}
}

if keyboard_check_pressed (ord ("K")) and global.laser>0 {
if sprite_index = spr_player_movendo_cima {
     global.laser-=1 {
      with ( instance_create_layer (x,y-16, "Instances", obj_laser)) {
         motion_set (90, 4) {
}
  }
 }
}
}

if keyboard_check_pressed (ord ("K")) and global.laser>0 {
if sprite_index = spr_player_movendo_baixo {
     global.laser-=1
      with ( instance_create_layer (x,y-16, "Instances", obj_laser)) {
         motion_set (270, 4) {
  }
 }
}
}

Re: Sistema de tiro com bug

Enviado: Ter Jan 01, 2019 11:30 pm
por None
Honestamente eu faria seu código todo do zero...

Enfim, se o seu laser é um laser de verdade, o ideal é usar um raycast, eu não sei se você está criando um objeto que vai se mover como uma bala ou se é um laser que vai sair da sua arma até a colisão com algo.


Usa um sistema de face/dir para determinar qual Sprite ele deve usar, tente usar uma state machine para deixar seu código mais organizado e fácil de manipular.

Se fosse para chutar eu diria que seu problema é usar o -= 1

Enfim, eu tenho um curso na Udemy, eu ensino como criar tudo isso no curso (para ser preciso ainda não ensinei raycast), se você tiver interesse posso deixar as aulas de animação e movimentação que você quer desbloqueadas por um tempo, assim você consegue desenvolver o que você quer com mais facilidade.
Se quiser é só falar que eu desbloqueio estás aulas

E se depois você tiver interesse posso te dar um cupom de desconto do curso.

Re: Sistema de tiro com bug

Enviado: Qua Jan 02, 2019 6:45 pm
por rafarp23
Olá, eu aconselho usar direction para atirar na direção desejada!

Então o script ficará assim:

Código: Selecionar todos

if keyboard_check_pressed(ord("K")) and global.laser>0 {
if direction == 90{
 global.laser -= 1;

 with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
 motion_set (90, 4);
 sprite_index = spr_laser_up;
 }
}else if direction == 270{
 global.laser -= 1;

 with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
 motion_set (270, 4);
 sprite_index = spr_laser_down;
 }
}else if direction == 0{
 global.laser -= 1;

 with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
 motion_set (0, 4);
 sprite_index = spr_laser_right;
 }
}else if direction == 180{
 global.laser -= 1;

 with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
 motion_set (180, 4);
 sprite_index = spr_laser_left;
 }
}

Re: Sistema de tiro com bug

Enviado: Qui Jan 03, 2019 1:01 pm
por AragaSam
Pra ser sincero meu conhecimento com GMS2 se resume a duas semanas de pesquisa então não sei muito sobre raycast e essas coisas, mas no momento eu crio um objeto na direção do inimigo que tira vida e se destroi quando colide, mas a intenção é exibir um sprite do player levantando a arma e atirando um laser que sai da arma após o sprite acontecer. Sobre o curso, eu agradeceria muito se pudesse disponibilizar.

E sobre o script enviado, por algum motivo aconteceu a mesma coisa que estava acontecendo e fiz isso:

Mas novamente quando ele anda  pra cima ou baixo o tiro não sai, somente quando esta parado nessas direções.

if keyboard_check_pressed(ord("K")) and global.laser>0 {
if sprite_index = spr_player_movendo_cima or sprite_index = spr_player_parado_cima{
global.laser -= 1;

with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
motion_set (90, 4);
sprite_index = spr_laser_vertical;
}
}else if sprite_index = spr_player_movendo_baixo or sprite_index = spr_player_parado_baixo{
global.laser -= 1;

with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
motion_set (270, 4);
sprite_index = spr_laser_vertical;
}
}else if sprite_index = spr_player_movendo_direita or sprite_index = spr_player_parado_direita{
global.laser -= 1;

with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
motion_set (0, 4);
sprite_index = spr_laser_horizontal;
}
}else if sprite_index = spr_player_movendo_esquerda or sprite_index = spr_player_parado_esquerda{
global.laser -= 1;

with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
motion_set (180, 4);
sprite_index = spr_laser_horizontal;
 }
}
}

Re: Sistema de tiro com bug

Enviado: Qui Jan 03, 2019 10:21 pm
por rafarp23
AragaSam escreveu:Pra ser sincero meu conhecimento com GMS2 se resume a duas semanas de pesquisa então não sei muito sobre raycast e essas coisas, mas no momento eu crio um objeto na direção do inimigo que tira vida e se destroi quando colide, mas a intenção é exibir um sprite do player levantando a arma e atirando um laser que sai da arma após o sprite acontecer. Sobre o curso, eu agradeceria muito se pudesse disponibilizar.

E sobre o script enviado, por algum motivo aconteceu a mesma coisa que estava acontecendo e fiz isso:

Mas novamente quando ele anda  pra cima ou baixo o tiro não sai, somente quando esta parado nessas direções.

if keyboard_check_pressed(ord("K")) and global.laser>0 {
if sprite_index = spr_player_movendo_cima or sprite_index = spr_player_parado_cima{
global.laser -= 1;

with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
motion_set (90, 4);
sprite_index = spr_laser_vertical;
}
}else if sprite_index = spr_player_movendo_baixo or sprite_index = spr_player_parado_baixo{
global.laser -= 1;

with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
motion_set (270, 4);
sprite_index = spr_laser_vertical;
}
}else if sprite_index = spr_player_movendo_direita or sprite_index = spr_player_parado_direita{
global.laser -= 1;

with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
motion_set (0, 4);
sprite_index = spr_laser_horizontal;
}
}else if sprite_index = spr_player_movendo_esquerda or sprite_index = spr_player_parado_esquerda{
global.laser -= 1;

with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
motion_set (180, 4);
sprite_index = spr_laser_horizontal;
 }
}
}
Tenta fazer o seguinte, no script que te mandei. esse abaixo:

Código: Selecionar todos

if keyboard_check_pressed(ord("K")) and global.laser>0 {
if direction == 90{
 global.laser -= 1;

 with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
 motion_set (90, 4);
 sprite_index = spr_laser_up;
 }
}else if direction == 270{
 global.laser -= 1;

 with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
 motion_set (270, 4);
 sprite_index = spr_laser_down;
 }
}else if direction == 0{
 global.laser -= 1;

 with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
 motion_set (0, 4);
 sprite_index = spr_laser_right;
 }
}else if direction == 180{
 global.laser -= 1;

 with ( instance_create_layer(x,y-16, "Instances", obj_laser)) {
 motion_set (180, 4);
 sprite_index = spr_laser_left;
 }
}
No movimento para direita, esquerda, baixo e cima você coloca o direction = //numero correspondente da direção para que ele mude a direção após se mover.

por ex:
if keyboard_check(vk_left){
direction = 180;
}else if keyboard_check(vk_right){
direction = 0;
}