I have managed to make my game not crash anymore. However, the code still doesn’t work correctly.
In my .cpp file, I create four instances of my class Player. However, in the shoot lambda, it only registers as the last player, i.e four players is player 4, two players is player 2 etc.
Here are my two files:
.cpp
if (oneText == "-") {
playersNumber += 1;
Player one (playersNumber, lives, Vec2(0, 1), mapBackground, Vec2(50, 50), Vec2(1, 0), Vec2(visibleSize.width*2/9, visibleSize.width/9), Vec2(0, 0), Vec2(visibleSize.width*2/9, visibleSize.width/20));
}
if (twoText == "-") {
playersNumber += 1;
Player two (playersNumber, lives, Vec2(1, 0), mapBackground, Vec2(50, mapBackground->getContentSize().height-50), Vec2(0, 0), Vec2(visibleSize.width/9, visibleSize.height-visibleSize.width*2/9), Vec2(0, 1), Vec2(visibleSize.width/20, visibleSize.height-visibleSize.width*2/9));
}
if (threeText == "-") {
playersNumber += 1;
Player three (playersNumber, lives, Vec2(0, -1), mapBackground, Vec2(mapBackground->getContentSize().width-50, mapBackground->getContentSize().height-50), Vec2(0, 1), Vec2(visibleSize.width*7/9, visibleSize.height-visibleSize.width/9), Vec2(1, 1), Vec2(visibleSize.width*7/9, visibleSize.height-visibleSize.width/20));
}
if (fourText == "-") {
playersNumber += 1;
Player four (playersNumber, lives, Vec2(-1, 0), mapBackground, Vec2(mapBackground->getContentSize().width-50, 50), Vec2(1, 1), Vec2(visibleSize.width*8/9, visibleSize.width*2/9), Vec2(1, 0), Vec2(visibleSize.width*19/20, visibleSize.width*2/9));
}
.h
class Player {
Sprite* stick;
Sprite* bullet;
int playerNumber;
int lives;
int ammo;
float angle;
Vec2 velocity;
PhysicsBody* playerPhysics;
public:
Sprite* joy;
bool alive;
int touch;
Sprite* player;
TMXTiledMap* mapBackground;
Player();
Player(int a, int b, Vec2 c, TMXTiledMap* d, Vec2 e, Vec2 f, Vec2 g, Vec2 h, Vec2 i) {
playerNumber = a;
lives = b;
ammo = 1;
angle = 0;
velocity = c;
alive = true;
mapBackground = d;
playerPhysics = PhysicsBody::createBox(Size(20, 20), PhysicsMaterial(0, 0, 0));
player = Sprite::create("player"+to_string(playerNumber)+".png");
player->setPosition(e);
player->addComponent(playerPhysics);
player->getPhysicsBody()->setCollisionBitmask(playerNumber);
player->getPhysicsBody()->setContactTestBitmask(true);
mapBackground->addChild(player);
joy = Sprite::create("joy"+to_string(playerNumber)+".png");
joy->setAnchorPoint(f);
joy->setPosition(g);
mapBackground->getParent()->addChild(joy);
stick = Sprite::create("stick.png");
stick->setPosition(joy->getContentSize()/2);
joy->addChild(stick);
bullet = Sprite::create("bullet"+to_string(playerNumber)+".png");
CCLOG("%f, %f", player->getPosition().x, player->getPosition().y); //logs position of correct player
auto shoot = Button::create("shoot"+to_string(playerNumber)+".png");
shoot->setAnchorPoint(h);
shoot->setPosition(i);
shoot->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type){
switch (type) {
case Widget::TouchEventType::ENDED:
if (ammo == 1 && alive) {
auto bulletPhysics = PhysicsBody::createCircle(5, PhysicsMaterial(0, 1, 0));
bullet = Sprite::create("bullet"+to_string(playerNumber)+".png");
CCLOG("%f, %f", player->getPosition().x, player->getPosition().y); //logs position of last player
CCLOG("%i", playerNumber); //logs number of last player
bullet->setPosition(player->getPosition()+velocity*20);
bullet->setPhysicsBody(bulletPhysics);
bullet->getPhysicsBody()->setVelocity(velocity*BULLETSPEED);
bullet->getPhysicsBody()->setCollisionBitmask(playerNumber*11);
bullet->getPhysicsBody()->setContactTestBitmask(true);
mapBackground->addChild(bullet);
ammo = 0;
}
break;
default:
break;
}
});
mapBackground->getParent()->addChild(shoot);
};