Visual Studio interprets the class method as static but they are not. I have 110 Errors when i build below code.
Errors: Foto with errors 1. , Foto with errors 2.
It may wrong declares these methods?
//header
class Player {
public:
Player(int x, int y) {
PlayerX = x;
PlayerY = y;
};
void doAction(int input, Mapa *mapa);
int getDirection();
Vector2 getPosition();
int
PlayerX, PlayerY, direction;
void turn(int dir);
void move(int move, Vector2 mapSize, Mapa *mapa);
Vector2 getCordInFrontOfCharacter();
Vector2 getCordBehindCharacter();
};
and cpp file:
#include "Vector2.h"
#include "Player.h"
#include "Mapa.h"
using namespace std;
int PlayerX = 0, PlayerY = 0;
int direction = 0;
void Player::doAction(int input, Mapa *mapa) {
if (input == (char)72)
this->move(1, *mapa->mapSize, mapa);
else if (input == (char)80)
this->move(-1, *mapa->mapSize, mapa);
if (input == (char)75)
this->turn(-1);
else if (input == (char)77)
this->turn(1);
}
void Player::turn(int dir) {
if (dir < 0)
dir = 2 - dir;
direction = (direction + dir) % 4;
}
void Player::move(int move, Vector2 mapSize, Mapa *mapa) {
if (
move = 1
&& getCordInFrontOfCharacter().y - 1 >= 0
&& getCordInFrontOfCharacter().x - 1 >= 0
&& getCordInFrontOfCharacter().y - 1 < mapSize.y
&& getCordInFrontOfCharacter().x - 1 < mapSize.x
&& mapa->_Mapa[getCordInFrontOfCharacter().y - 1][getCordInFrontOfCharacter().x - 1] == '0') {
if (this->direction == 0)
this->PlayerY -= move;
else if (this->direction == 2)
this->PlayerY += move;
else if (this->direction == 1)
this->PlayerX += move;
else if (this->direction == 3)
this->PlayerX -= move;
if (this->PlayerY < 1)
this->PlayerY = 1;
if (this->PlayerX < 1)
this->PlayerX = 1;
if (this->PlayerY > mapSize.y)
this->PlayerY = mapSize.y;
if (this->PlayerX > mapSize.x)
this->PlayerX = mapSize.x;
}
else if (
move = 1
&& this->getCordBehindCharacter().y - 1 >= 0
&& this->getCordBehindCharacter().x - 1 >= 0
&& this->getCordBehindCharacter().y - 1 < mapSize.y
&& this->getCordBehindCharacter().x - 1 < mapSize.x
&& mapa->_Mapa[this->getCordBehindCharacter().y - 1][this->getCordBehindCharacter().x - 1] == '0') {
if (this->direction == 0)
this->PlayerY -= move;
else if (this->direction == 2)
this->PlayerY += move;
else if (this->direction == 1)
this->PlayerX += move;
else if (this->direction == 3)
this->PlayerX -= move;
if (this->PlayerY < 1)
this->PlayerY = 1;
if (this->PlayerX < 1)
this->PlayerX = 1;
if (this->PlayerY > mapSize.y)
this->PlayerY = mapSize.y;
if (this->PlayerX > mapSize.x)
this->PlayerX = mapSize.x;
}
}
int Player::getDirection() {
return this->direction;
}
Vector2 Player::getPosition() {
return Vector2(this->PlayerX, this->PlayerY);
}
Vector2 Player::getCordInFrontOfCharacter() {
if (this->direction == 2)
return Vector2(this->PlayerX, this->PlayerY + 1);
else if (this->direction == 0)
return Vector2(this->PlayerX, this->PlayerY - 1);
else if (this->direction == 3)
return Vector2(this->PlayerX - 1, this->PlayerY);
else if (this->direction == 1)
return Vector2(this->PlayerX + 1, this->PlayerY);
return Vector2(0, 0);
}
Vector2 Player::getCordBehindCharacter() {
if (direction == 2)
return Vector2(this->PlayerX, this->PlayerY - 1);
else if (this->direction == 0)
return Vector2(this->PlayerX, this->PlayerY + 1);
else if (this->direction == 3)
return Vector2(this->PlayerX + 1, this->PlayerY);
else if (this->direction == 1)
return Vector2(this->PlayerX - 1, this->PlayerY);
return Vector2(0, 0);
}
Thanks for help.
The Visual Studio compilater apparently gets confused at this function signature
void Player::move(int move, Vector2 mapSize, Mapa *mapa)
where the same name is used for a parameter and the function itself.
Change that to make them distinct:
void Player::move(int move_, Vector2 mapSize, Mapa *mapa)
// ^
In cpp file i initialize non-static methods with "::"
It's pretty much impossible to initialize method in c++. I you meant class members, well, then that's wrong.
If you have non-static member, you can't do that:
int foo::member = 0; // error, member is non-static
Every foo object has it's own int member, so it's not shared between them. You must set it for every object in constructor (maybe default values are what you are looking for):
foo::foo (int _mem = 0, ... ) : member(_mem) {...}
Related
I'm working in the watershed algortih in C++. I have implemented a source that i've found but i didn't get the expected results. I obtain:
[
But the result should be this:
[
I have charge the image .bmp into a matrix an then i obtain the Gradient of the image using the Sobel operator.
My wathershed algorith now is:
void Watershed() {
stack<punto> s;
punto p, neighbour;
C_Matrix prueba3 (Gradiente.FirstRow(), Gradiente.LastRow(), Gradiente.FirstCol(), Gradiente.LastCol(), -1);
int auxU, auxV, Eaux, L=1;
for (double g = Gradiente.Min(); g <= Gradiente.Max(); g++) {
for (int i = Gradiente.FirstRow(); i <= Gradiente.LastRow(); i++) {
for (int j = Gradiente.FirstCol(); j <= Gradiente.LastCol(); j++) {
if (Gradiente(i, j) == g) {
p.Guarda(i, j);
s.push(p);
}
while (s.empty() == 0) {
p = s.top();
s.pop();
auxU = p.x;
auxV = p.y;
Eaux = -1;
// 8-connectivity
for (int i = 0; i < 8; i++) {
if (i == 0)
neighbour.Guarda(auxU - 1, auxV - 1);
else if (i == 1)
neighbour.Guarda(auxU, auxV - 1);
else if (i == 2)
neighbour.Guarda(auxU + 1, auxV - 1);
else if (i == 3)
neighbour.Guarda(auxU - 1, auxV);
else if (i == 4)
neighbour.Guarda(auxU + 1, auxV);
else if (i == 5)
neighbour.Guarda(auxU - 1, auxV + 1);
else if (i == 6)
neighbour.Guarda(auxU, auxV + 1);
else
neighbour.Guarda(auxU + 1, auxV + 1);
if (neighbour.x >= Gradiente.FirstRow() && neighbour.x <= Gradiente.LastRow()
&& neighbour.y >= Gradiente.FirstCol() && neighbour.y <= Gradiente.LastCol()) {
if (prueba3(neighbour.x, neighbour.y) > 0) {
if (Eaux == -1) {
Eaux = prueba3(neighbour.x, neighbour.y);
}
else if (prueba3(neighbour.x, neighbour.y) != Eaux)
Eaux = 0;
}
}
}
if (auxU >= Gradiente.FirstRow() && auxU <= Gradiente.LastRow()
&& auxV >= Gradiente.FirstCol() && auxV <= Gradiente.LastCol()) {
if (Eaux >= 0) {
prueba3(auxU, auxV) = Eaux;
}
else {
prueba3(auxU, auxV) = L;
L++;
}
}
else {
C_Print("Se sale");
C_PrintNum("AuxU", auxU);
C_PrintNum("AuxV", auxV);
}
}
}
}
}
C_PrintNum("L = ", L);
double max = prueba3.Max();
if (max > 255.0) {
prueba3.Stretch(0, 255);
}
aux = C_Image(prueba3);
}
I don't know where is the fail, maybe my source has mistakes.
I'm having trouble with g++ producing this error code when I try to compile my code:
maze.h:16:29: error: array bound is not an integer constant before ‘]’ token
bool canMove(int m[mazeSize][mazeSize], int r, int c);
Now, I have already done some research into this error and it seems to be causes by the array size not being known at compile time. I have tried making the array constant, but that ends up causing more errors later on as the array is reassigned later on in the code and produces this error:
maze.cpp: In member function ‘int Maze::startMazeGen()’:
maze.cpp:185:15: error: assignment of read-only location ‘maze[i][j]’
maze[i][j] = 1;
^
I have also seen people mention that it would just be easier to work with vectors instead, but I'm also having issues with trying to repurpose the code to work with vectors instead of arrays.
Here's the rest of my code:
movement.h
#pragma once
#include <iostream>
#include <curses.h>
#ifndef MOVEMENT_H
#define MOVEMENT_H
class Movement
{
public:
static const int playerX = 2; // sets player starting position
static const int playerY = 2;
};
#endif
movement.cpp
#include <iostream>
#include <curses.h>
#include <ctime>
#include "maze.h"
//#include "movement.h"
bool running = true;
int playerX = 2;
int playerY = 2;
//Maze::maze Maze::mazeGen;
//int Maze::mazeGen.Maze::maze::generateMaze::maze(int m[Maze::mazeSize]
[Maze::mazeSize], int r, int c);
// Detect Char input
// and move player in direction
void getUserInput()
{
char userInput = getch();
if (userInput == 'w') {
int playerY2 = playerY - 1;
if (Maze::maze[playerY2][playerX] == ' ') {
Maze::maze[playerY][playerX] = ' ';
playerY--;
Maze::maze[playerY][playerX] = 'x';
}
}
if (userInput == 'a') {
int playerX2 = playerX - 1;
if (Maze::maze[playerY][playerX2] == ' ') {
Maze::maze[playerY][playerX] = ' ';
playerX--;
Maze::maze[playerY][playerX] = 'x';
}
}
if (userInput == 's') {
int playerY2 = playerY + 1;
if (Maze::maze[playerY2][playerX] == ' ') {
Maze::maze[playerY][playerX] = ' ';
playerY++;
Maze::maze[playerY][playerX] = 'x';
}
}
if (userInput == 'd') {
int playerX2 = playerX + 1;
if (Maze::maze[playerY][playerX2] == ' ') {
Maze::maze[playerY][playerX] = ' ';
playerX++;
Maze::maze[playerY][playerX] = 'x';
}
}
}
// Main game update
// Runs through all functions required
void update()
{
getUserInput();
clear();
Maze::generateMaze;
refresh();
}
//
//
/*int main()
{
// Initate nCurses display
initscr();
while (true) {
update();
}
// End nCurses display
endwin();
return 0;
}*/
maze.h
#pragma once
// MAZE.h
#include <iostream>
#include <ctime>
#ifndef MAZE_H
#define MAZE_H
extern int r;
extern int c;
extern int mazeSize; //number can be changed to make some big sweaty mazes making it an even number makes it act a bit weird sometimes so its better to use an odd number
extern int maze[mazeSize][mazeSize];
class Maze
{
public:
int blockedSquare = 1;
void move(int m[mazeSize][mazeSize], int &r, int &c);
bool canMove(int m[mazeSize][mazeSize], int r, int c);
void solve(int m[mazeSize][mazeSize], int &r, int &c);
bool canSolve(int m[mazeSize][mazeSize], int r, int c);
void generateMaze(int m[mazeSize][mazeSize], int r, int c);
int findStart();
void printMaze(int m[mazeSize][mazeSize]);
int startMazeGen();
};
#endif
maze.cpp
#include <iostream>
#include <ctime>
#include <vector>
#include "maze.h"
bool foundExit = false;
int mazeSize = 31;
int maze[mazeSize][mazeSize] = { 0 };
void Maze::generateMaze(int const m[mazeSize][mazeSize], int r, int c)
{
bool made = false;
while (made == false)
{
if (c == mazeSize - 1)
foundExit = true;
if (canSolve(m, r, c))
{
solve(m, r, c);
}
else if (canMove(m, r, c))
{
m[r][c] = 2; //2 means you can't move from that square, setting any lower stops maze from being made
move(m, r, c); //move to first open space that can be found
}
else
made = true;
}
}
void Maze::move(int m[mazeSize][mazeSize], int &r, int &c)
{
if (m[r][c + 1] == 0)
c++;
else if (m[r + 1][c] == 0)
r++;
else if (m[r][c - 1] == 0)
c--;
else if (m[r - 1][c] == 0)
r--;
else
generateMaze(maze, r, c); //if maze cant be solved it generates a new one so the player doesnt have something that is impossible to solve
}
bool Maze::canMove(int m[mazeSize][mazeSize], int r, int c) //if there is an adjacent zero space, return true
{
if (m[r][c + 1] == 0)
return true;
else if (m[r + 1][c] == 0)
return true;
else if (m[r][c - 1] == 0)
return true;
else if (m[r - 1][c] == 0)
return true;
else
return false;
}
void Maze::solve(int m[mazeSize][mazeSize], int &r, int &c) //solves maze through with dijkstras algorithmto ensure it can be solved
{
bool foundSolution = false;
while (foundSolution == false)
{
int direction = (1 + rand() % 4) * 3;
switch (direction)
{
case 3:
if (c + 1 <= mazeSize - 1 && m[r][c + 2] == blockedSquare && m[r - 1][c + 1] == blockedSquare && m[r + 1][c + 1] == blockedSquare && m[r][c + 1] == blockedSquare)
{
if (c == mazeSize - 2 && foundExit == true)
; //do nothing
else
{
c++;
foundSolution = true;
}
}
break;
case 6:
if (r + 1 <= mazeSize - 2 && m[r + 2][c] == blockedSquare && m[r + 1][c + 1] == blockedSquare && m[r + 1][c - 1] == blockedSquare && m[r + 1][c] == blockedSquare && c != 0 && c != mazeSize - 1)
{
r++;
foundSolution = true;
}
break;
case 9:
if (c - 1 >= 0 && m[r][c - 2] == blockedSquare && m[r - 1][c - 1] == blockedSquare && m[r + 1][c - 1] == blockedSquare && m[r][c - 1] == blockedSquare && c - 1 != 0)
{
c--;
foundSolution = true;
}
break;
case 12:
if (r - 1 >= 1 && m[r - 2][c] == blockedSquare && m[r - 1][c + 1] == blockedSquare && m[r - 1][c - 1] == blockedSquare && m[r - 1][c] == blockedSquare && c != 0 && c != mazeSize - 1)
{
r--;
foundSolution = true;
}
break;
}
}
m[r][c] = 0;
}
bool Maze::canSolve(int m[mazeSize][mazeSize], int r, int c) //if an adjacent square can be moved to, return true
{
bool solvable = false;
if (r <= mazeSize - 3 && m[r + 2][c] == blockedSquare && m[r + 1][c + 1] == blockedSquare && m[r + 1][c - 1] == blockedSquare && m[r + 1][c] == blockedSquare && c != 0 && c != mazeSize - 1) //if adjacent space can be moved to
{
solvable = true;
}
else if (c <= mazeSize - 2 && m[r][c + 2] == blockedSquare && m[r - 1][c + 1] == blockedSquare && m[r + 1][c + 1] == blockedSquare && m[r][c + 1] == blockedSquare)
{
if (c == mazeSize - 2 && foundExit == true)
; //do nothing
else
{
solvable = true;
}
}
else if (r >= 2 && m[r - 2][c] == blockedSquare && m[r - 1][c + 1] == blockedSquare && m[r - 1][c - 1] == blockedSquare && m[r - 1][c] == blockedSquare && c != 0 && c != mazeSize - 1) //if not on extreme left or right
{
solvable = true;
}
else if (c >= 1 && m[r][c - 2] == blockedSquare && m[r - 1][c - 1] == blockedSquare && m[r + 1][c - 1] == blockedSquare && m[r][c - 1] == blockedSquare && c - 1 != 0)
{
solvable = true;
}
return solvable;
}
int Maze::findStart()
{
return 1 + rand() % (mazeSize - 2);
}
void Maze::printMaze(int m[mazeSize][mazeSize])
{
std::cout << std::endl;
for (int i = 0; i < mazeSize; ++i) {
for (int j = 0; j < mazeSize; ++j)
{
switch (m[i][j])
{
case 0:
std::cout << " ";
break;
case 1:
std::cout << "▓▓";
break;
case 2:
std::cout << " ";
break;
case 3:
std::cout << " ";
break;
}
}
std::cout << std::endl;
}
}
int Maze::startMazeGen()
{
srand(time(0));
for (int i = 0; i < mazeSize; ++i)
for (int j = 0; j < mazeSize; ++j)
maze[i][j] = 1;
int r = findStart();
//int r = 0;
int c = 0;
maze[r][c] = 0;
generateMaze(maze, r, c);
maze[r][c] = 2;
printMaze(maze);
std::cout << "Press enter to continue ...";
std::cin.get();
}
The purpose of this code is to randomly generate a maze, solve it, and then print it to the screen if it can be solved. If the maze can't be solved, it keeps generating a new one until it can be solved.I aim to make this work with the movement code so that the user can navigate the maze.
Any help is appreciated on this issue. Thank you!
"Now, I have already done some research into this error and it seems to be causes by the array size not being known at compile time. I have tried making the array constant, but that ends up causing more errors later on as the array is reassigned later on in the code"
You're conflating two things here, the array and the array size.
The array size should be a a compile-time constant. Since you're assigning to the array, the array elements shouldn't be const at all.
const int arrSize = 3;
int arr[arrSize][arrSize];
I'm fighting with this for like few days, and I have no idea, how to do that, so I'd like to ask you for help. I've got no idea how collision should look like right here, so player could jump through 'down zone' of the block, and stay right on the block.
block.cpp
bool block::CollidingWithPlayer(character& player) {
for (int i = 1; i < MAX_BLOCKS; i++) {
if (player.x + player.width >= coordinateX[i] && player.x <= coordinateX[i] + width[i] && player.y + player.height >= coordinateY[i] && player.y <= coordinateY[i] + block_height) {
player.onGround = true;
return true;
}
}
}
character.cpp
void character::startJump(map& Map, character& player) {
if (onGround)
{
vel[1] = -11.0;
onGround = false;
}
}
void character::updateJump(block& Block, character& player) {
if (!onGround) {
Block.CollidingWithPlayer(player);
vel[1] += 0.5;
y += vel[1];
x += vel[0];
}
if (y > 460){
y = 460;
vel[1] = 0.0;
onGround = true;
vel[0] = 0.0;
}
if ((x + width >= START_OF_RIGHT_WALL && x <= WALL_WIDTH + START_OF_RIGHT_WALL) || (x + width >= START_OF_LEFT_WALL &&x <= START_OF_LEFT_WALL + WALL_WIDTH)){
vel[0] *= -1;
bound = true;
if (direction == 1)
direction = 2;
else if (direction == 2)
direction = 1;
}
}
I am trying to create a maze generator using recursive backtracking and have come across a problem that I just can't get my head around. For some reason my move function is returning the value "18446744073709551615". This is (of course) leading to a segmentation fault. Why is my move function returning such a large value when my move function can only increase or decrease the value by 2?
bool maze::generate(size_t x, size_t y) {
//mark the position as visited
labyrinth.s[y][x] = true;
//print to see progress
//this->print();
//if the position is not out of bounds
if (x < 0 || x > labyrinth.MAXWIDTH - 1 || y < 0 || y > labyrinth.MAXHIGHT - 1) {
//if the position is the endpoint return true
if (labyrinth.v[y][x - 1] == 'W' || labyrinth.v[y][x + 1] == 'W' || labyrinth.v[y - 1][x] == 'W' || labyrinth.v[y + 1][x] == 'W') {
return true;
}
}
//pick a random direction
do {
d = size_t(rand() % 4);
} while(!this->pos_test(x, y, d));
std::cout << x << ' ' << y << std::endl;
if (d == UP) {
y = move(x, y, UP);
}
else if (d == DOWN) {
y = move(x, y, DOWN);
}
else if (d == RIGHT) {
x = move(x, y, RIGHT);
}
else if (d == LEFT) {
x = move(x, y, LEFT);
}
else{
}
std::cout << x << ' ' << y << std::endl;
//recursively generate the maze
if (this->generate(x, y)) {
return true;
}
}
void maze::initialize(size_t x, size_t y) {
//set the maxhight and the maxwidth to y and x
labyrinth.MAXHIGHT = y;
labyrinth.MAXWIDTH = x;
//set all elements in the vector to #
for (size_t i = 0; i < labyrinth.MAXHIGHT; i++) {
std::vector<char> temp;
for (size_t j = 0; j < labyrinth.MAXWIDTH; j++) {
temp.push_back(labyrinth.wall);
}
labyrinth.v.push_back(temp);
}
for (size_t i = 0; i < labyrinth.MAXHIGHT; i++) {
for (size_t j = 0; j < labyrinth.MAXWIDTH; j++) {
if (j % 2 == 1 && i % 2 == 1 && j != labyrinth.MAXWIDTH - 1 && j != 0 && i != labyrinth.MAXHIGHT - 1 && i != 0) {
labyrinth.v[j][i] = labyrinth.path;
}
}
}
//set all posistions to unvisited
for (size_t i = 0; i < labyrinth.MAXHIGHT; i++) {
std::vector<bool> temp2;
for (size_t j = 0; j < labyrinth.MAXWIDTH; j++) {
temp2.push_back(false);
}
labyrinth.s.push_back(temp2);
}
//setup the start point
labyrinth.v[0][1] = 'S';
//setup the endpoint
labyrinth.v[labyrinth.MAXHIGHT - 2][labyrinth.MAXWIDTH - 1] = 'W';
}
//if a position has been visited or if not possible to go to return true
bool maze::pos_test(size_t x, size_t y, size_t d) const {
//if the position is out of bounds return false
if (x < 0 || y < 0 || x > labyrinth.MAXWIDTH - 1 || y > labyrinth.MAXHIGHT - 1) {
return true;
}
else if (x == 1 && d == LEFT) {
return true;
}
else if (y == 1 && d == UP) {
return true;
}
else if (x == labyrinth.MAXWIDTH - 1 && d == RIGHT) {
return true;
}
else if (y == labyrinth.MAXHIGHT - 1 && d == DOWN) {
return true;
}
else if (d == UP) {
return labyrinth.s[y - 2][x];
}
else if (d == DOWN) {
return labyrinth.s[y + 2][x];
}
else if (d == RIGHT) {
return labyrinth.s[y][x + 2];
}
else if (d == LEFT) {
return labyrinth.s[y][x - 2];
}
else {
return true;
}
}
size_t maze::move(size_t x, size_t y, size_t d) {
//if the position is out of bounds return without modifying
if (x < 0 || x > labyrinth.MAXWIDTH - 1) {
return x;
}
else if (y < 0 || y > labyrinth.MAXHIGHT - 1) {
return y;
}
else if (d == UP) {
labyrinth.v[y - 1][x] = labyrinth.path;
return y = y - 2;
}
else if (d == DOWN) {
labyrinth.v[y + 1][x] = labyrinth.path;
return y = y + 2;
}
else if (d == RIGHT) {
labyrinth.v[y][x + 1] = labyrinth.path;
return x = x + 2;
}
else if (d == LEFT) {
labyrinth.v[y][x - 1] = labyrinth.path;
return x = x - 2;
}
else {
}
}
You are underflowing your unsigned 64-bit return type size_t.
You are checking whether x and y are below zero, but that's not enough, because 0 and 1 will still be too low because you are subtracting 2!
The number you get is 0xFFFFFFFFFFFFFFFF in hexadecimal. This is the highest possible value for an unsigned 64-bit integer.
It comes from calculating 1 - 2. Yes, this is supposed to be -1, but because your move function doesn't return a signed number but an unsigned one (check the docs on size_t), it can't be negative! Instead, it wraps around to the highest possible number.
You can imagine this in the same way you would get ...99999999999 when you try to calculate 1 - 2 on paper ignoring the "you can't subtract a higher number from a smaller one on paper" rule.
As a side note: I guess the negative result is undesired anyway, because actually your huge number, once added to a pointer, will in turn overflow back into positive, so basically it will work the same is a real -1 in your case and the segmentation fault comes from accessing something right before the beginning of your buffer, not far beyond it, but it comes down to the same thing.
Apart from that, there is no need to do return y = y - 2 and such. Just return y - 2.
I'm trying to make a game (simple 2d platformer).
The program runs as it should, but an if statement doesn't work correctly.
I have this function:
int Collision::platformCollision(SDL_Rect *hitbox, SDL_Rect plat) {
if (checkCollision(*hitbox, plat)) {
//X
//LEFT SIDE
if (hitbox->x + hitbox->w > plat.x && hitbox->x + hitbox->w < plat.x + 5) {
hitbox->x = plat.x - hitbox->w;
return 1;
}
//RIGHT SIDE
if (hitbox->x < plat.x + plat.w && hitbox->x > plat.x + plat.w - 5) {
hitbox->x = plat.x + plat.w;
return 2;
}
//Y
//UPPER SIDE
if (hitbox->y + hitbox->h > plat.y && hitbox->y + hitbox->h < plat.y + 10) {
hitbox->y = plat.y - hitbox->h;
return 3;
}
//BOTTOM SIDE
if (hitbox->y < plat.y + plat.h && hitbox->y > plat.y + plat.h - 10) {
hitbox->y = plat.y + plat.h;
return 4;
}
}
//NOT COLLIDING
return -1;
}
So I have this function return an int whenever it collides with a certain part of the platform.
Then I have this function:
void Player::playerCheckPlatCollision(SDL_Rect rect) {
if (platformCollision(p_hitboxPTR, rect) == 3) {
setGravityF(0.0);
}
if (platformCollision(p_hitboxPTR, rect) == 4) {
p_space = false;
}
return;
}
The problem should be easy to fix.
When I debug the program it gets to return 4; in the platformCollision function, but when I do
if (platformCollision(p_hitboxPTR, rect) == 4) {
p_space = false;
}
It doesn't put p_space as false, it just ignores the == 4 and when I debugged, I saw it got to that if statement.
Can someone please help.
Thanks.
If platformCollision returns 4 on the first call, it alters state, and will not return 4 on the second call.
void Player::playerCheckPlatCollision(SDL_Rect rect) {
int bang = platformCollision(p_hitboxPTR, rect);
if (bang == 3) {
setGravityF(0.0);
} else if (bang == 4) {
p_space = false;
}
}