第 4 个射击语句在 SFML 游戏中不起作用

4th Shooting statement not working in SFML game

本文关键字:SFML 游戏 不起作用 语句 射击      更新时间:2023-10-16

我目前正在使用SFML 2.0创建游戏。基本上它是一个自上而下的射击游戏,我对下面的陈述有困难。当按下空格键时会射出一个箭头。有四种陈述取决于玩家精灵(archSprite)面对的方式。这是从此函数计算的 source.y 确定

enum DirectionKeys { Up, Left, Down, Right};
sf::Vector2i source(0, 0);
int getCellY(DirectionKeys direction)
{
    return (direction*64);
}

我遵循的逻辑是声明

int dir = source.y/64;

会用我正在使用的精灵表找出精灵面对的方向 0 = 向上 1 = 向左 2 = 向下 3 = 右 此语句对除向右射击以外的所有内容都正确射击 谁能弄清楚为什么我的箭头向右射击而箭头没有旋转?就像射击权的声明被完全忽略了一样。任何人都可以提供建议吗?

#include "Game.h"
#include "Tilemap.h"
#include <SFMLGraphics.hpp>
#include <iostream>

 if(keypress == sf::Keyboard::Space && keyPressed)
 {
 int tblt = this->_game_data.curr_bullet;
 this->_arr_bullet_data[tblt].active        = true;
 //every 100th bullet it is set to the up rotation 
 int dir = source.y/64;
 if (dir == 0)
 {
    _arr_bullet_spr[tblt].rotate(90);
    this->_arr_bullet_data[tblt].startpos     = this->archSprite.getPosition() + sf::Vector2f(64,-42);
    this->_arr_bullet_data[tblt].direction  = sf::Vector2f(0,-1);
 }
 else if (dir == 1)
 {
    _arr_bullet_spr[tblt].rotate(360);
    this->_arr_bullet_data[tblt].startpos     = this->archSprite.getPosition() + sf::Vector2f(-22,0);
    this->_arr_bullet_data[tblt].direction  = sf::Vector2f(-1,0);
 }
 else if (dir = 2)
 {
    _arr_bullet_spr[tblt].rotate(270);
    this->_arr_bullet_data[tblt].startpos     = this->archSprite.getPosition() + sf::Vector2f(0,96);
    this->_arr_bullet_data[tblt].direction  = sf::Vector2f(0,1);
 }
 else if (dir = 3)
 {
    _arr_bullet_spr[tblt].setRotation(180);
    this->_arr_bullet_data[tblt].startpos = this->archSprite.getPosition() + sf::Vector2f(0,0);
    this->_arr_bullet_data[tblt].direction  = sf::Vector2f(1,0);
 }
 this->_arr_bullet_spr[tblt].setPosition(this>_arr_bullet_data[tblt].startpos);
 this->_game_data.curr_bullet++;
 }

非常感谢

if(dir = 2)if(dir = 3)必须分别if(dir == 2)if(dir == 3)