Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Bullets won't shoot up or down


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 MindOfCorruption97   Members   -  Reputation: 140

Like
0Likes
Like

Posted 12 June 2013 - 11:07 AM

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()".



Sponsor:

#2 Sik_the_hedgehog   Crossbones+   -  Reputation: 1811

Like
0Likes
Like

Posted 12 June 2013 - 11:56 AM

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.

#3 MindOfCorruption97   Members   -  Reputation: 140

Like
0Likes
Like

Posted 12 June 2013 - 11:57 AM

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?



#4 eloadrin   Members   -  Reputation: 171

Like
0Likes
Like

Posted 12 June 2013 - 12:40 PM

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.

#5 LordRhys   Members   -  Reputation: 364

Like
0Likes
Like

Posted 12 June 2013 - 12:48 PM

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.

#6 Dave Hunt   Crossbones+   -  Reputation: 2468

Like
2Likes
Like

Posted 12 June 2013 - 01:12 PM

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.



#7 MindOfCorruption97   Members   -  Reputation: 140

Like
0Likes
Like

Posted 12 June 2013 - 02:45 PM

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.



#8 Dave Hunt   Crossbones+   -  Reputation: 2468

Like
1Likes
Like

Posted 12 June 2013 - 03:49 PM

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



#9 Khatharr   Crossbones+   -  Reputation: 3030

Like
0Likes
Like

Posted 12 June 2013 - 09:32 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS