我很确定我已将其更改为遵循规则,所以我再次询问。 问题是我要发射子弹,该子弹的初始方向将与鼠标最初与鼠标单击时字符的位置进行比较的位置一致。但是我现在在mouseclick上的代码将绘制另一个项目符号,但是绘制的所有项目符号都将沿着与mouseclick上当前字符位置相应的角度的方向行进(这不是我想要的)
这是一个看起来像的例子
int bulletTotal = 5;
float[] bulletX = new float [bulletTotal];
float[] bulletY = new float [bulletTotal];
int shots;
float bulletSpeed;
float [] angle = new float [bulletTotal];
int charaStartX, charaStartY;
int charaWid, charaHght;
int sx, sy;
boolean isUp, isDown, isLeft, isRight;
void setup()
{
size(1500,900);
charaStartX = width/2;
charaStartY = height/2;
charaWid = charaHght = 75;
sx = sy = 4;
for(int j =0; j < bulletTotal; j++)
{
bulletX[j] = charaStartX;
bulletY[j] = charaStartY;
}
bulletSpeed = 5;
isUp = false;
isDown = false;
isLeft = false;
isRight = false;
}
void draw()
{
ellipse(charaStartX, charaStartY, charaWid, charaHght);
for(int j = 0; j < bulletTotal; j++)
{
rect(bulletX[j], bulletY[j], 25, 25);
movement();
bulletX[j] += bulletSpeed * cos(angle[j]);
bulletY[j] += bulletSpeed * sin(angle[j]);
}
}
void movement()
{
if(isUp == true)
{
charaStartY = charaStartY - sy;
}else if(isDown == true)
{
charaStartY = charaStartY + sy;
}
if(isLeft == true)
{
charaStartX = charaStartX - sx;
}else if (isRight == true)
{
charaStartX = charaStartX + sx;
}
}
void mousePressed()
{
if (shots<bulletTotal)
{
bulletX[shots] = charaStartX;
bulletY[shots] = charaStartY;
shots = shots+1;
}
for(int j=0; j<bulletTotal; j++)
{
angle[j] = atan2(mouseY - charaStartY, mouseX - charaStartX);
}
}
void keyPressed()
{
if(key == 'w'||key == 'W')
{
isUp = true;
}
if(key == 's'||key == 'S')
{
isDown = true;
}
if(key == 'a'||key == 'A')
{
isLeft = true;
}
if(key =='d'||key == 'D')
{
isRight = true;
}
}
void keyReleased()
{
if(key == 'w'||key == 'W')
{
isUp = false;
}
if(key == 's'||key == 'S')
{
isDown = false;
}
if(key == 'a'||key == 'A')
{
isLeft = false;
}
if(key =='d'||key == 'D')
{
isRight = false;
}
}
答案 0 :(得分:1)
您的代码中的问题是
for(int j=0; j<bulletTotal; j++)
{
angle[j] = atan2(mouseY - charaStartY, mouseX - charaStartX);
}
按下鼠标时,它将设置所有项目符号的角度。您只想为刚创建的项目符号设置它。您需要删除上面的部分并使用:
if (shots<bulletTotal)
{
bulletX[shots] = charaStartX;
bulletY[shots] = charaStartY;
angle[shots] = atan2(mouseY - charaStartY, mouseX - charaStartX);
shots = shots+1;
}