all. I have some function that add sprite to layer
-(void)drawBoard {
for (int y = 0; y < 18; y++) {
for (int x = 0; x < 12; x ++) {
if (tetrisBoard[x][y] != NULL) {
[self addChild:tetrisBoard[x][y]];
}
}
}
}
But when I call this function next time my app just freezes. I think it freezes because i already have the same sprite on layer. How can i check all child on my layer and if i have the same child on layer, then do nothing ? Thanks
You can get an NSArray of the children of the layer, so you could do a check like this:
if ([[self children] containsObject:tetrisBoard[x][y]]) {
[self addChild:tetrisBoard[x][y]];
}
Related
I have been trying to make asteroids in the console, I know that the console isn't an ideal way to do this, but I wanted to challenge myself.
The problem I am having is with printing to the screen. At first, my screen was flickering because I wasn't updating certain screen parts and instead redrawing every frame by appending the whole array to a string and outputting that string.
So I changed my code to do that, but my current problem now is that the fps of my game is really low because I can't batch call it to one cout function.
To print my code to the screen I have a 2d array of characters with width and height as its size. Then I copy the array, change the output, and compare it to the previous output to see if the pixel needs to be changed, then change it.
Here is my draw function
void draw() {
char prevOutput[ArrayBorder][ArrayBorder];
copyOutput(outputBuffer, prevOutput);
innitOutput();
plotPolygons();
for (int y = 0; y < GameBorder; y++) {
for (int x = 0; x < GameBorder; x++) {
if (outputBuffer[x][y] == prevOutput[x][y]) {continue;}
setCursorPosition(x, y);
cout << outputBuffer[x][y];
}
}
cout.flush();
}
Here is my previous draw function
void draw() {
innitOutput();
plotPolygons();
system("CLS");
string output = "";
for (int y = 0; y < GameBorder; y++) {
for (int x = 0; x < GameBorder; x++) {
output += outputBuffer[x][y];
}
output += "\n";
}
cout << output;
}
I'm making a simple, gravity based particle system, so I made three classes:
ParticleManager, that store the particle system
ParticleSystem, the VertexArray containing particles with an update function
Particle, a Vertex inheriting from sf::Transformable.
Actually, I'm stuck with ParticleSystem update function here's the code:
ParticleSystem()
{
particles.setPrimitiveType(PrimitiveType::Points);
int indexX = 0, indexY = 0;
for (int i = 1; i < CHUNK_SIZE; i++)
{
Particle particle;
particle.position = Vector2f(indexX, indexY);
particles.append(particle);
if (indexX < 100)
indexX++;
else
{
indexX = 0;
indexY++;
}
force.push_back(0);
}
}
void Update()
{
for (int i = 0; i < particles.getVertexCount(); i++)
{
particles[i].Update();
}
}
The compiler gives an error when calling particles[i].Update(), because VertexArray can contain just sf::Vertex;
Any suggestion for implementing that function?
Make your own vector.
std::vector<Particle> particles;
I am making a game in sfml and at the moment when all of the enemies die. They are set to respawn however when this is happening they are respawning with one extra sprite than before.
The code for loading in the sprites is
unsigned int orcNumber = 5;
for (int i = 0; i < orcNumber; i++)
{
SpriteVector.push_back(ogreSprite);
SpriteVector[i].setPosition(spawnPointX[i], spawnPointY[i]);
}
The code for removing the enemies if they are offscreen or shot is similar to below using erase.
for (unsigned j = 0; j < SpriteVector.size(); j++)
{
if (this->SpriteVector[j].getPosition().x < 0 - 80 )
{
//this succesfully removes the object from the vector
SpriteVector.erase(SpriteVector.begin() + j);
std::cout << "Container size..." << SpriteVector.size() << "\n";
}
}
The statement for redrawing them is:
unsigned int orcNumberRespawn = 5;
if (SpriteVector.size() <= 1)
{
for (int i = 0; i < orcNumberRespawn; i++)
{
SpriteVector.push_back(ogreSprite);
SpriteVector[i].setPosition(spawnPointX[i], spawnPointY[i]);
}
}
window.draw(SpriteVector[i]);
Can anyone identify why when the sprites need to be redrawn it draws with + 1 sprite everytime?
The issue was with a loop outside of these vector loops.
I'm working on a school project using C++ and SFML 2.0, it's due for Tuesday and I've just recently run in to this problem.
I've put an arrow on where the latest Call Stack is.
These are the code parts that I suspect might have something wrong in them. I'm also very new to c++ and I know I do alot of things wrong.
This is in my Background class, it loads all my tiles, sets their position and tiletype and so on.
The GetWayPointList returns a vector of tiles to my EnemyManager class
void Background::Load(QuadTree * quadTree) {
int tempTiles[32][32] = {... Lots of 1's and 0's in here };
for(int i = 0; i < 32; i++)
{
for(int j = 0; j < 32; j++)
{
---> tile[j][i]->tileType = tempTiles[j][i];
tile[j][i]->setPosition(sf::Vector2f((float)(i*32),(float)(j*32)));
if(tile[j][i]->tileType == 1)
{
quadTree->AddObject(tile[j][i]);
}
else if(tile[j][i]->tileType == 0)
{
tile[j][i]->distanceFromStart = INT_MAX;
tile[j][i]->parent = NULL;
vector<Tile*> tempTiles;
if(tile[j+1][i]->tileType == 0)
tempTiles.push_back(tile[j+1][i]);
if(tile[j-1][i]->tileType == 0)
tempTiles.push_back(tile[j-1][i]);
if(tile[j][i+1]->tileType == 0)
tempTiles.push_back(tile[j][i+1]);
if(tile[j][i-1]->tileType == 0)
tempTiles.push_back(tile[j][i-1]);
tile[j][i]->setSuccessors(tempTiles);
tilesList->push_back(tile[j][i]);
}
}
}
}
vector<Tile*>* Background::GetWayPointList()
{
return tilesList;
}
The Call Stack itself says TileShooter v0.1.exe!Background::Load(QuadTree*quadTree) Line 68 + 0x2e bytes.
Line 68 is where the arrow is.
If you need more code / info just say.
Any points on what's possibly wrong would be greatly appriciated.
Best Regards, Fredrik W
Edit
I've edited the code some including tile[j][i] = new Tile();
void Background::Load(QuadTree * quadTree) {
int tempTiles[32][32] = {... Lots of 1's and 0's in here };
for(int i = 0; i < 32; i++)
{
for(int j = 0; j < 32; j++)
{
tile[j][i] = new Tile();
tile[j][i]->tileType = tempTiles[j][i];
tile[j][i]->setPosition(sf::Vector2f((float)(i*32),(float)(j*32)));
if(tile[j][i]->tileType == 1)
{
quadTree->AddObject(tile[j][i]);
}
else if(tile[j][i]->tileType == 0)
{
tile[j][i]->distanceFromStart = INT_MAX;
tile[j][i]->parent = NULL;
vector<Tile*> tempTiles;
----> if(tile[j+1][i]->tileType == 0)
tempTiles.push_back(tile[j+1][i]);
if(tile[j-1][i]->tileType == 0)
tempTiles.push_back(tile[j-1][i]);
if(tile[j][i+1]->tileType == 0)
tempTiles.push_back(tile[j][i+1]);
if(tile[j][i-1]->tileType == 0)
tempTiles.push_back(tile[j][i-1]);
tile[j][i]->setSuccessors(tempTiles);
tilesList->push_back(tile[j][i]);
}
}
}
}
vector<Tile*>* Background::GetWayPointList()
{
return tilesList;
}
That didn't really solve but rather moved the error downwards, I've moved my arrow on where the Call Stack has it's latest call.
So your tile member is defined as Tile* tile[32][32]. That's a 2D array of size 32x32 where each element is a pointer to Tile. However, they are just pointers and currently don't point anywhere in particular - accessing them is undefined behaviour (in your case, an access violation). You need to allocate the actual Tile objects:
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
tile[i][j] = new Tile();
}
}
Don't forget to do the same kind of loop in the destructor of Background that instead does delete tile[i][j];.
Better yet, don't dynamically allocate your tiles and just define tile as:
Tile tile[32][32];
Even better yet (!), use std::array<std::array<Tile,32>,32>. :)
To answer your edit, even though it's invalidated my above answer: You're accessing the j+1th element of tile. When j is 31, you're accessing tile[32] which is outside the bounds of your array.
I'm making a Asteroids game but I can not get to play more than one explosion at a time. Just get to do one at a time ...
This is my code I call in the main loop:
for(i = 0; i < MAX_SHOTS; i++) {
for(j = 0; j < MAX_ASTEROIDS; j++) {
if(shot[i].CheckCollision(asteroide[j])) {
shot[i].SetPos(-100, 0);
explosionSnd.Play();
numAst = j;
explosion[numExp++].Enable(true);
if(numExp == MAX_EXPLOSIONS-1) {
numExp = 1;
}
}
}
}
for(i = 1; i < MAX_EXPLOSIONS; i++) {
if(explosion[i].Enable()) {
explosion[i].SetPos(asteroide[numAst].GetX(), asteroide[numAst].GetY());
explosion[i].Draw();
if(explosion[i].GetFrame() == 5) {
explosion[i].Enable(false);
}
}
}
If I shot to an asteroid and after I shot to another, the animation is cut and goes to the new asteroid.
Any help?
Thank you.
Inside your second loop, you're moving each explosion to the location of the asteroid asteroide[numAst] - you're playing all the explosions, just all at the same place!
You should only position the explosion once after you Enable(true) it, when it's created in the first loop, not each time you draw it.
Hope that helps.