C++ :: Video Game - Accessing Objects In Another Class?
Dec 3, 2014
I am working on a video game that is based on a tutorial I found online and I ran into an issue associated with accessing an object's method inside another class object's function without making the object global which seems bad.
Here is basically how the tutorial set things up in the game: The game has a base class called GameObject then there are other child classes that inherit from this class. The GameObject class has all the basic information an object would have that needs rendered on the screen (x/y position, size of bitmap, bitmap, whether or not it is collidable, etc).
One of the child classes SpaceShip, which is the player so it has attributes and methods associated with managing # of lives and points scored.
There are other child classes of GameObjects in the game that need to take life and add points from the SpaceShip object if they collide with the spaceship or other objects.
In the collision handling routine I basically call a function "void Collided(int objectID)" when a game object collides with another. Within the Collided() routine, there is logic that executes code or other functions based on the objectID it collided with. Some of the collisions require taking life from the spaceship or adding points. The way this was accomplished in the tutorial was with function pointers (see Bullet class constructor and collided method for example). Is this really the best way to handle this sort of thing? It seems like there has to be a simpler way than to keep referencing function pointers in any new class I want to add to the game. I realized this when I went to add a method for the spaceship to fire bullets rather than inside my game class.
My Project Source found here: [URL] .....
View 5 Replies
ADVERTISEMENT
Dec 21, 2014
I'm working on my first video game. So far I have a few classes in the game starting with the Game class which includes a list of GameObjects (another class). There are several classes that inherit from GameObjects used to implement things like bullets, explosions, various enemy types, etc.
The game essentially iterates through the list of GameObjects to update/render them. I would like to provide access to the Game's list of GameObjects inside another class (like the Bullet class) so I can put new objects on the list. For example, when a bullet hits, I want to add an explosion to the Game's GameObject list it can be updated/rendered.
How this should be setup? I was considering adding a pointer to the Game or GameObject list to the GameObject class (and methods to access it), but I was wondering if there is a better way to set this up?
View 4 Replies
View Related
Apr 28, 2015
We are coding a Blackjack/21 game. I have a Deck.cpp class, Deck.h, Play.cpp (holds Main), and Card.h (holds card struct). I also have a Hand class/header, but I'm not using it yet. This is what is required per instructor.I am having issues accessing the functions that are in my Deck class. I have tried a few other means to access the class's function, but I've already gotten rid of those. These three are my latest attempts with the specific errors in the comment on the line the error was happening.
ve.
Here is my Deck.h
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include "Card.h"
#include "Hand.h"
using namespace std;
class Deck
[Code]...
View 3 Replies
View Related
Apr 5, 2014
is is possible to put a video in your c++ game using allegro 5?before starting the game................
View 1 Replies
View Related
May 20, 2013
I would like to create a video game. It would be text based. How could I set up a save system? Also, how would I load it?
View 3 Replies
View Related
Apr 9, 2014
I am making a game of quiz and i want to play a small clip in one of the rounds, but i dont know how to do it in c++ ....
View 3 Replies
View Related
Jun 10, 2013
How would you create a video game in c++? I'm not sure really how to display the graphics and set properties and do all that stuff. I don't have money to buy a book about so how do you?
View 2 Replies
View Related
Oct 21, 2013
I was tasked with creating a program that asks for the scores of three gamers then takes these scores and outputs the highest. I THINK I have the other parts down but I don't know how to get the program to compare the scores and output the highest without having to write a long list of commands comparing playerone to playertwo, then player one to playerthree, then player two to playerone, etc.
#include <iostream>
using namespace std;
int main () {
int playerOne, playerTwo, playerThree;
cout << "Please enter score for Player One: ";
cin >> playerOne;
[Code] .....
View 10 Replies
View Related
Feb 8, 2014
I'm currently trying to access a variable contained within a base object from another completely different object and I continually get error messages. I'll attempt to put up the important code since it's contained within fairly large classes. From the Base .cpp file. ObjectPosition:
void ObjectPosition::init(float x,float y, float speed, int boundx, int boundy, float hit, int lives, bool live) {
ObjectPosition::x = x;
ObjectPosition::y = y;
ObjectPosition::speed = speed;
ObjectPosition::boundx = boundx;
ObjectPosition::boundy = boundy;
ObjectPosition::hit = hit;
ObjectPosition::lives = lives;
ObjectPosition::live = live;
}
This is the initialization function for the BaseObject. All objects are inheriting these variables which are Protected within the ObjectPosition class.Then they are initialized within the Pig class thus wise:
void Pig::binit(float sx,float sy, ALLEGRO_BITMAP *simage) {
//Sets all ObjectPosition Variables
ObjectPosition::init(800,900,10,80,40,40,10,true);
smaxFrame = 4;
scurFrame = 0;
[code]...
I tried to initialize the boundx through the pig via pinitx but I get errors and I can't access through the pig to the object position to the boundx.
View 10 Replies
View Related
May 9, 2013
I have a pointer to a vector of objects, each object has an array and a couple of strings. how to access the data in the objects via the pointer.
Best tree::chooseSplit(vector <pgm> *ptr)
{
Best splits;
cout<<ptr[1].filePath; //not working!!!
}
filepath is a string in the pgm object. i also need to be able to access elements in an array that also exists in pgm.
View 2 Replies
View Related
Oct 21, 2014
I'm working on designing a game in C++ that is similar to the "find the ball under the cup" game. I have a Sonic the Hedgehog icon (weird I know, but it was the first thing that came to mind) that will be hidden underneath one of three rectangular blocks.
Here is how I envision this working:
On the main menu I have 3 buttons which represent 3 different difficulty levels
Easy- blocks move 3 times at a slow speed
Medium- blocks move 5 times at a slightly faster speed
Hard- blocks move 10 times at a fast speed
When the user clicks one of these buttons they will be taken to the game screen.
Sonic will be displayed for 3 seconds and the user will then see him be covered with one of the three blocks.
The three blocks will then move in a random pattern along the middle of the screen at the speed and number of times associated with the button that was pressed.
Once the blocks stop moving, the user is to click on the one they think Sonic is underneath.
If they choose correctly, they'll be taken to a "Winning Screen" that displays a congratulatory message and 2 buttons. Play Again- returns the user to the main menu and the game starts over with a new random pattern. Quit- the window closes.
If they choose incorrectly, they'll be taken to a "Losing Screen" that displays a "Try Again" message and 2 buttons that have the same function as the buttons on the winning screen.
I have never worked with any kind of graphics before other than in HTML and Javascript. I have managed to create the main menu, but how to do the actual game portion of the project. I've been trying to take it a step at a time, (for example, I first figured out how to set the background color for the console window, after I got that right I figured out how to add buttons) but the rest of this seems to depend on each other.
Here is what I have so far:
Main menu:
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
using namespace std;
class MainMenu
[Code]....
So I've got the screens pretty much designed now (with the exception of the game screen itself) but how to tie everything together.
In case my description wasn't too clear, here is a game that I found on Google that is pretty much exactly what I'm looking for. [URL]
View 2 Replies
View Related
Mar 16, 2014
I am creating a game and I using classes for other things in my game, I was wondering if i should use classes for rendering objects?
View 2 Replies
View Related
Feb 19, 2013
Here is the link for my program, I want to access the average value which is located in grade.c (calculate_grade) class through driver.c (main function) but I don't know how to make "average" visible
[URL] .....
View 1 Replies
View Related
Aug 3, 2013
I've been attempting to create a game with curses and I keep running into the problem of scope. I want to change or use variables of a different class in a different header file. But I don't know if I should use pointers, references or neither. Should I be programming in a manner that doesn't make it necessary to use variables outside of their class?
View 1 Replies
View Related
Apr 4, 2014
I am trying to access the private member from main . The code is as follow. I wanted to call below function from main.
uint16_t Modbus::calcCRC(uint8_t u8length)
#define MAX_BUFFER 64
typedef struct {
[Code]....
View 1 Replies
View Related
Mar 21, 2015
In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,
Code:
class B
{
public:
B():_y(1){}
virtual ~B() {
cout<<"~B()"<<endl;
[Code] ....
This sample code does exactly what I want. So does the author mean the way I did is undefined?
View 1 Replies
View Related
Jan 16, 2013
Please consider the following code :
#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;
[Code] ....
As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.
View 2 Replies
View Related
Apr 16, 2014
I am not able to access the class variable noof_vertex in the function merge , the error is the variable is private . below is the code :
#include <iostream>
#include <ctime>
#include <cstdlib>
[Code]....
View 1 Replies
View Related
Jul 24, 2013
How I can use class functions in a vector. I have 2 classes:
Gamemode class
bullets class
Inside the Gamemode class I declared: vector<bullets> Bullet and bullets * b.
If the user presses the shoot button a new bullets class will be added to the Bullet vector:
b = new bullets;
Bullet.push_back(b)
Bow I'd like to check all objects in the Bullet vector for the collision() function inside the bullets class.
I already set up a for loop which counts from 0 to the end of the vector:
for( int i=0;i<Bullet.size;i++)
{
}
My idea was to do something like:
if(Bullet[i].collision()) then erase Bullet[i]
But that doesn't work...
View 2 Replies
View Related
Jan 6, 2015
Let's say I have a Car object , and it contains inner Engine object.
Code:
struct Car{
Engine mEngine;
};
In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:
Code:
Car::Car:
mEngin(arg1,arg2,...)
{
other stuff here
}
Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?
Code:
class Toyota:
Car(...),
mEngine(...),
mGear(..)
{
...
};
The other options are:
1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive
2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .
View 5 Replies
View Related
Jan 10, 2013
Let's say I have the following class:
class MyClass {
private:
int m_myInt;
public:
int myInt() {return this->m_myInt;};
int myFunc();
};
Which of the following is to prefer;
int MyClass::myFunc() {
return 2*this->m_myInt;
}
or
int MyClass::myFunc() {
return 2*this->myInt();
}
The second one seems better? Are both OK? What's the best practice?
View 13 Replies
View Related
Jun 23, 2014
I have this proxy class generated from a webservice:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18408")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="java:se.atg.aisbean.calendar")]
public partial class RaceDayCalendarSimple : object, System.ComponentModel.INotifyPropertyChanged {
[Code] ....
I'm trying to access this method "RaceDaySimple[] raceDay" and display it on "WebForm1.aspx":
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebServiceClient.WebForm1" %>
<!DOCTYPE html>
[Code] ....
So on "WebForm1.aspx.cs" I'm trying to access the method "RaceDaySimple[] raceDay" and display it inside the div "test":
test.InnerHtml = pbs.fetchRaceDayCalendarSimple().raceDay;
But I'm getting the error:
Cannot implicitly convert 'WebServiceClient.InformationServiceReference.RaceDaySimple[]' to 'String'
So how do I acces this method proparly and display the content on "WebForm1.aspx" inside the div "test"?
View 1 Replies
View Related
Mar 30, 2013
how to access the private and protected member functions of the class.....
View 5 Replies
View Related
Feb 22, 2013
I have two classes, a Package class and a Person class. The Package class has two Person objects has member variables, a Sender and a Receiver. While overloading the << operator for the Package class so that it will make an output label from everything in the Package class. Here is my code...
class Package{
public:
Person Sender;
Person Reciever;
int weight;
double cost;
friend ostream &operator<<(ostream &out, Package &pack);
[Code] .....
So my problem is on that last output line, I am unable to call Sender.getName()... etc. Is there a proper syntax so that I can access the members of the Person class while overloading the << operator for the Package class?
View 2 Replies
View Related
Oct 12, 2014
class FlashCard {
public:
int a,b;
void PrintCard(void);
int CorrectAnswer(void);
};
void SwapFlashCard(FlashCard &a,FlashCard &b)
[Code]....
Why does Xode warn me that Type'FlashCard'does not provide a subscript operator on line 22,23,29 and 30?
View 7 Replies
View Related
Apr 9, 2014
I have been working on an assignment where I have to add three objects of a class Matrix. The class should have the flexibility to add more than two oprands without changing any operand on Left hand side of the '=' operator.
Matrix obj1, obj2, obj3, obj4
Obj1=obj2+obj3+obj4
I am using the following code for this
Matrix Matrix::operator + (Matrix &obj) {
if((this->Columns == obj.Columns) && (this->Rows == obj.Rows)) {
for(int i=0;i<obj.Rows;i++)
[Code] .....
the problem is that it is just adding only 2 operands. How can I add more?
View 2 Replies
View Related