gpt4 book ai didi

c++ - 将值从一个类传递到另一个类。单独的 cpp 文件。 C++

转载 作者:行者123 更新时间:2023-11-30 02:40:42 25 4
gpt4 key购买 nike

我正在努力将值从一个 CPP 类文件传递到一个单独的 CPP 文件。

我一直在尝试大量不同的运算符来尝试传递值,我知道我遗漏了一些小东西,但我想不出还有什么可以做的,所以我来了。

我正在尝试将 player 类中的位置 x_posy_pos 传递给我的 bullet 类,这样子弹从玩家的位置发射。我只是无法将值传递给项目符号类,包括 header 。

其中一些已被注释掉,目前我已将其设置为从同一位置触发。

#include "bullet.h"
#include "player.h"

Bullet::Bullet()
{
x_pos = 400;
y_pos = 300;
sprite = load_bitmap("bullet.bmp", 0);
}

Bullet::~Bullet()
{
destroy_bitmap(sprite);
destroy_sample(Music);
}

void Bullet::update()
{
x_pos -= 0.5;
y_pos -= 0.5;
//Music = load_sample("shot.wav");
// play_sample(Music,255,128,500,true);
if(x_pos > SCREEN_W)
{
destroy_bitmap(sprite);
}
if(x_pos < 0)
{
destroy_bitmap(sprite);
}
if(y_pos > SCREEN_H)
{
destroy_bitmap(sprite);
}
if(y_pos < 0)
{
destroy_bitmap(sprite);
}
}

void Bullet::draw(BITMAP* buff)
{
//if(key[mouse_b&1])

// {

masked_blit(sprite,buff,0,0,(x_pos),(y_pos),sprite->w,sprite->h);

// y_pos --;

// }

}
//masked_blit(sprite,buff,0,0,(&player::getX),(&player::getY),sprite->w,sprite->h);


#include "player.h"
#include "bullet.h"

player::player()
{
x_pos = 400;
y_pos = 300;
sprite = load_bitmap("player.bmp", 0);
bulletSprite = load_bitmap("bullet.bmp", 0);
spriteState = idle;



rightWalk = 0;
leftWalk = 0;
upWalk = 0;
downWalk = 0;
}

float player::getX()
{

return x_pos;
}

float player::getY()
{
return y_pos;
}

如有任何帮助,我们将不胜感激。

包含编辑头文件。

子弹.h

#include "Headers.h"


class Bullet
{
private:
//float x_pos, y_pos; // this is the initial x and y position variable for the bullet
float angle;
BITMAP *sprite; // this is the sprite variable
SAMPLE *Music;


public:
Bullet::Bullet();
~Bullet();
int x,y;
int x_pos, y_pos; // this is the initial x and y position variable for the bullet
void draw(BITMAP* buff);
void update();
float setX(float x);
float setY(float y);
};

player.h

#include "Headers.h"


class player
{
private:

//double rightX, leftX; // this is the initial x and y position variable for the player
BITMAP *sprite; // this is the sprite variable
BITMAP *bulletSprite; // this is the sprite variable
// put enemy sprite here.
enum state{idle, up, right,down,left}; // this holds the movement for the player
state spriteState;

int leftWalk;
int rightWalk;
int upWalk;
int downWalk;



double bullet_direction;
double bulletDirection();

public:
player();
~player();

double radians;
double aimAngle();

float x_pos, y_pos; // this is the initial x and y position variable for the player

void mouseAngle(BITMAP* bullets);
void hit();
void update(BITMAP* buff);
void draw(BITMAP* buff);

float getX();
float getY();
};

嗯,我知道我做错了什么。最后一个小时我一直在玩。收到类似的消息。

错误非静态成员引用必须相对于特定对象

看看人们的评论,我认为我没有链接这两个 CPP 文件,或者我没有根据玩家坐标设置 x 位置。

目前在游戏中,当我按下鼠标按钮时,它会从 300,400 并且仅在这个位置开火。

我想将玩家的 x 和 y 值传递给子弹,这样我就可以从玩家的位置 blit 子弹?我不能发送非静态值吗?当我四处走动时,它会发生变化。在我的代码中,我有一个 while 循环始终在运行,因此它会更新子弹位置和玩家位置。

再次感谢。

最佳答案

构造器方式

最好的方法是像这样通过 Bullet 的构造函数传递它:

Bullet::Bullet(float x, float y)
: x_pos(x), y_pox(y)
{
sprite = load_bitmap("bullet.bmp", 0);
}

使用示例:

Bullet bullet(player.getX(), player.getY()); // x_pos and y_pos are properly set

二传手方式

如果实在不行,试试setters :

// Modifies x_pos from outside.
Bullet::setX(float x)
{
this->x_pos = x;
}

// Modifies y_pos from outside.
Bullet::setY(float y)
{
this->y_pos = y;
}

使用示例:

Bullet bullet;
bullet.setX(player.getX()); // x_pos is now properly set
bullet.setY(player.getY()); // y_pos is now properly set

关于c++ - 将值从一个类传递到另一个类。单独的 cpp 文件。 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28764828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com