Bullets won't shoot up or down

Started by
7 comments, last by Khatharr 10 years, 10 months ago

Hey, I have a problem.
You see, for some reason, I am not able to shoot up or down. After a few hours looking I couldn't seem to find the error to this issue. So can anyone help me point out what is causing the bullets not go up nor down.

Player.h


#ifndef PLAYER_H
#define	PLAYER_H

#include <GL/glfw.h>
#include <vector>

#include "tools.h"

using namespace std;

class Bullet;

class Player{
public:
    void init();
    void draw();
    void update();
    
    void keyListener();
    void mouseMotionListener();
    void mouseClickListener();
    
    void setVx(int Vx){intVx = Vx;}
    void setVy(int Vy){intVy = Vy;}
    void velocity();
    void shoot(int intDir);
    
    void nothing(){}
    
    Rectangle getPlayer(){return rectPlayer;}
    Point getMotion(){return ptMotion;}
    Point getClick(){return ptClick;}
private:
    int intX,intY,intVx,intVy,intWidth,intHeight;
    bool blInitiated;
    
    vector<Bullet> vectBullet;
    
    Rectangle rectPlayer;
    Point ptMotion,ptClick;
};

class Bullet{
public:
    Bullet(int X,int Y);
    ~Bullet();
    
    void draw();
    void update();
    
    void setVx(int Vx){intVx = Vx;}
    void setVy(int Vy){intVy = Vy;}
    void velocity();
    
    Rectangle getBullet(){return rectBullet;}
private:
    int intX,intY,intVx,intVy;
    
    Rectangle rectBullet;
};

#endif

Player.cpp


#include "Player.h"

void Player::init(){
    blInitiated = false;
    
    intX = 0;
    intY = 0;
    intWidth = 50;
    intHeight = 50;
    
    rectPlayer = createRectangle(intX,intY,intWidth,intHeight);
    
    blInitiated = true;
}
void Player::draw(){
    glColor3f(0.0,0.0,255.0);
    drawRectangle(rectPlayer.intX,rectPlayer.intY,rectPlayer.intWidth,rectPlayer.intHeight);
    
    for(int i = 0;i < vectBullet.size();i++){
        vectBullet.at(i).draw();
    }
}
void Player::update(){
    if(!blInitiated){init();}else{nothing();}
    
    rectPlayer.intX = intX;
    rectPlayer.intY = intY;
    rectPlayer.intWidth = intWidth;
    rectPlayer.intHeight = intHeight;
    
    velocity();
    
    for(int i = 0;i < vectBullet.size();i++){
        vectBullet.at(i).update();
    }
}

void Player::velocity(){
    intX += intVx;
    intY += intVy;
}
void Player::shoot(int intDir){
    vectBullet.push_back(Bullet(intX + 23,intY + 23));
    if(intDir == 1){vectBullet.back().setVy(-10);}
    else if(intDir == 2){vectBullet.back().setVy(-10);}
    if(intDir == 3){vectBullet.back().setVx(-10);}
    else if(intDir == 4){vectBullet.back().setVx(+10);}
}

void Player::keyListener(){
    if(glfwGetKey('A') == GLFW_PRESS){setVx(-3);}
    else if(glfwGetKey('D') == GLFW_PRESS){setVx(+3);}
    else{setVx(0);}
    if(glfwGetKey('W') == GLFW_PRESS){setVy(-3);}
    else if(glfwGetKey('S') == GLFW_PRESS){setVy(+3);}
    else{setVy(0);}
    
    if(glfwGetKey(GLFW_KEY_UP) == GLFW_PRESS){shoot(1);}
    else if(glfwGetKey(GLFW_KEY_DOWN) == GLFW_PRESS){shoot(2);}
    else if(glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS){shoot(3);}
    else if(glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS){shoot(4);}
}
void Player::mouseMotionListener(){glfwGetMousePos(&ptMotion.intX,&ptMotion.intY);}
void Player::mouseClickListener(){
    if(glfwGetMouseButton(GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS){
        glfwGetMousePos(&ptClick.intX,&ptClick.intY);
    }
}

//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Bullet::Bullet(int X, int Y){
    intX = X;
    intY = Y;
    rectBullet = createRectangle(intX,intY,4,4);
}
Bullet::~Bullet(){}

void Bullet::draw(){
    glColor3f(0.0,0.0,255.0);
    drawRectangle(rectBullet.intX,rectBullet.intY,rectBullet.intWidth,rectBullet.intHeight);
}
void Bullet::update(){
    
    rectBullet.intX = intX;
    rectBullet.intY = intY;
    
    velocity();
}

void Bullet::velocity(){
    intX += intVx;
    intY += intVy;
}

It's just Vy(velocity of Y) won't set. I tried replacing "setVy" with "setVx" so I know the keyboard isn't the problem. It's something to do with setVy but I don't know what.

PS. I tested it out and I know the problem is somewhere in the "shoot()".

Advertisement

First of all:

if(intDir == 1){vectBullet.back().setVy(-10);}
else if(intDir == 2){vectBullet.back().setVy(-10);}

I don't think this will result in the shoot going in opposite ways.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

First of all:


if(intDir == 1){vectBullet.back().setVy(-10);}
else if(intDir == 2){vectBullet.back().setVy(-10);}

I don't think this will result in the shoot going in opposite ways.

Sorry, minor mistake. But the bullets still won't shoot on the Y axis. Is there anything else wrong witht the code?

Take a look in your keylistener and shoot functions for player. Change the else ifs to just ifs, because what is happening there is that if you press right and then press and hold down, it will not shoot down as well because of the else clause.
I'm guessing that your IntX, IntY, IntVx, and IntVY are all getting affected by the Player code, you have no distinction since you created a Bullet Constructor in the Player code but no Player Constructor. You intiialize an IntX and IntY in the Bullet constructor and what should be a Player IntX and IntY in the initialize event. I suggest splitting the Bullet class out to it own file and reference the Bullet object in your player code.

The debugger is your best friend here. Step through your shoot and update code and check that they are actually being called and the variables have the expected values and are getting updated as expected. That should tell you where things are going wrong.

Take a look in your keylistener and shoot functions for player. Change the else ifs to just ifs, because what is happening there is that if you press right and then press and hold down, it will not shoot down as well because of the else clause.

I tried that but it seems like the keyboard is not the issue. When I replace the setVy with setVx the program responds to the keyboard input. But if I replace it back then it won't respond with the setVy. Basically something is wrong with the methods/variable itself not the keyboard.

Seriously, your debugger will tell you what is going wrong.

Yeah, I was gonna post something to that effect, but I thought it might be pedantic after you'd already said it. I can't tell if he's in 'that phase' or if he just missed your first comment.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement