Shooting in game help

Started by
15 comments, last by theOcelot 15 years, 9 months ago
theOcelot is correct.

[Edited by - blewisjr on July 4, 2008 12:08:56 PM]
Advertisement
Quote:Original post by theOcelot
You're getting wierd errors because you're doing weird things. Why are you using the syntax Gun::x, PlayerShip::x? You should be using Gun.x, Player.x. Actually, in a member function of Player, whenever you use x it is assumed to be Player.x / Player::x / whatever. You need to have an actual instance of your Gun struct. I would suggest making the Gun a member of PlayerShip, either by value or by a pointer.

If that didn't make sense, you probably need to go back and do some basic research on OOP, and make sure you're clear on the distinction between a class and an object.


Lol your right you know its been a while since I last programmed I guess I have forgotten some things.

Ok the last part what do you mean by making Gun a member of Player ship?? How would I do that? I understand you said either by value or by a pointer but what do mean by making Gun a member or PlayerShip?

Thanks for your guys patience with me.
Quote:Original post by theOcelot
You're getting wierd errors because you're doing weird things. Why are you using the syntax Gun::x, PlayerShip::x? You should be using Gun.x, Player.x. Actually, in a member function of Player, whenever you use x it is assumed to be Player.x / Player::x / whatever. You need to have an actual instance of your Gun struct. I would suggest making the Gun a member of PlayerShip, either by value or by a pointer.

If that didn't make sense, you probably need to go back and do some basic research on OOP, and make sure you're clear on the distinction between a class and an object.


Is this what you meant?

// PlayerShip.h Implementation File#include "PlayerShip.h"Gun GunFire;void PlayerShip::CallMovement(){		GunFire.x = PlayerShip::x;	GunFire.y = PlayerShip::y;  if(Key_Down(DIK_LEFT))  {	  if(PlayerShip::x != 0)	{		PlayerShip::x -= PlayerShip::movex;		GunFire.x -= PlayerShip::movex;	}  }  if(Key_Down(DIK_RIGHT))  {	  if(PlayerShip::x < (SCREEN_WIDTH - PlayerShip::width))	{		PlayerShip::x += PlayerShip::movex;		GunFire.x += PlayerShip::movex;	}  }  if(Key_Down(DIK_UP))  {	  if(PlayerShip::y != 0)	{		PlayerShip::y -= PlayerShip::movey;		GunFire.y -= PlayerShip::movey;	}  }  if(Key_Down(DIK_DOWN))  {	  if(PlayerShip::y < (SCREEN_HEIGHT - PlayerShip::height))	{		PlayerShip::y += PlayerShip::movey;		GunFire.y += PlayerShip::movey;	}  }}
yes that is basically what he means. I do however still stand at my statement saying that you are using too much OOP.

[Edited by - blewisjr on July 4, 2008 12:01:19 PM]
Actually, I had something a little more like this in mind. I'm just inlining functions for clarity.

class PlayerShip{    public:    Gun* ship_gun;    PlayerShip(){        //stuff...        ship_gun = new Gun(/*whatever args*/);        //more stuff...    }    ~PlayerShip(){        //this may be all that's required in the destructor        delete ship_gun;    }};


I'm not sure exactly what PlayerShip::CallMovement is supposed to do; I think you should change the name, probably to something boring like update(). It seems, though that it is just supposed to move the ship based on key-downs and make sure that the gun is always in the same position relative to it. If that's the case, here's about how I would do it.

void PlayerShip::CallMovement(){    if(keyDown(DIK_LEFT)){        x -= xmove;//notice how it is assumed that these are members of PlayerShip        //also notice that I am not moving the gun yet    }    //repeat the key-down tests as necessary    //now move the gun, all in one shot    ship_gun.x = x + GUN_OFFSET_X;    ship_gun.y = y + GUN_OFFSET_Y;    //For what it's worth, I would make firing a method of gun    if(Key_Down(DIK_SPACE)){        ship_gun->Fire(/*args?*/);//this function puts a bullet sprite in the world    }}


GUN_OFFSET_X and GUN_OFFSET_Y are just constants that tell how far the position of the gun is from the position of the ship, since their positions are generally their top-left corners, and just assigning their positions to be equal would look funny.

Don't copy my code, because I'm in a hurry and it's almost gauranteed not to compile, but you probably get the idea.
Quote:Original post by blewisjr
There alone is the first flaw. Your design is slightly off. The classes for the ship and the gun can be separated but why. The gun is part of the ship.

Composition. Create a generic gun class and you can give your ship one or more guns fairly easy. You can reuse the gun code for your enemies as well. Just because something is part of something else doesn't mean you shouldn't create separate classes for them.

Quote:If you insist on keeping them separate then you should put in a property structure into each class. Use getters and setters.

Create a decent interface instead. For example, Move() is better than a SetX() and SetY() pair, as it is more geared towards the actual use of the object.

Quote:However, in all honesty since the gun is part of the players ship I would just keep the 2 together in the same class and share the position values where the origin of the gun projectile is the position of the ship and after the projectile leaves the ship it should have its own variable that tracks its travel. Making the ship and the gun move as one unit.

Here you're saying something interesting. Make them move as one. Personally, I would make the gun position relative to the player. This means you only need to update the player location. Whenever you fire a bullet, you add the player and gun positions together to get the initial bullet position.


A few more notes: in C++, structs are almost the same as classes, although they're often used to indicate that something is merely a collection of data. I would say that a Gun is more than just data - it also has specific behaviour (hey, it fires stuff, right?), so the current approach here is somewhat off. You're storing gun data in a Gun struct but the gun functionality resides elsewhere. Why this separation?

@theOcelot: why create the Gun on the heap? In this case, the manual memory management is more trouble than it's worth. After all, a Gun isn't a heavy object anyway.
Create-ivity - a game development blog Mouseover for more information.
Quote:Original post by Captain P
@theOcelot: why create the Gun on the heap? In this case, the manual memory management is more trouble than it's worth. After all, a Gun isn't a heavy object anyway.


Oh, no good reason. It's just what comes naturally. I actually like pointers. Allocating in the constructor and deleting in the destructor doesn't strike me as horrendously difficult. I suppose though that having it by value would be easier, esp. for a beginner or someone brushing up, and it seems OP is one of the two.

This topic is closed to new replies.

Advertisement