Processing Java处理对aim使用箭头键
我正在使用Java Processing 3制作一个两人坦克游戏。下面是我的炮塔代码。目前,我在tDir中使用鼠标跟踪炮塔的瞄准,我希望能够使用上下箭头将瞄准从0度向上和向下移动到90度 我该怎么办?谢谢Processing Java处理对aim使用箭头键,processing,Processing,我正在使用Java Processing 3制作一个两人坦克游戏。下面是我的炮塔代码。目前,我在tDir中使用鼠标跟踪炮塔的瞄准,我希望能够使用上下箭头将瞄准从0度向上和向下移动到90度 我该怎么办?谢谢 /* 欧宰尔 */ PVector mPos//鼠标位置 PVector TPO//炮塔位置 PVector-tDir//炮塔的射击方向 int gravMult=3; 无效设置(){ 大小(1200、600); init(); } void init(){ //PVector初始化 mPos
/*
欧宰尔
*/
PVector mPos//鼠标位置
PVector TPO//炮塔位置
PVector-tDir//炮塔的射击方向
int gravMult=3;
无效设置(){
大小(1200、600);
init();
}
void init(){
//PVector初始化
mPos=new PVector();//在鼠标x和y存在之前为零
tPos=新的PVector(宽/8,高);
tDir=新的PVector()//
}
void draw(){
//清除最后一帧
背景(100140);
//检查钥匙,查看是否有新的转塔钥匙输入
如果(按键){
如果(键=='w'){
tDir.y-=10;
}
否则如果(键='s'){
tDir.y+=10;
}
}
mPos.set(mouseX,mouseY);
tDir=PVector.sub(mPos、tPos);
tDir.normalize();
tDir.mult(50);
//画
填充(255);
椭圆(tPos.x,tPos.y,40,40);
冲程重量(5);
行(tPos.x、tPos.y、tPos.x+tDir.x、tPos.y+tDir.y);
填充(255,0,0);
椭圆(tPos.x+tDir.x,tPos.y+tDir.y,10,10);
}
堆栈溢出实际上不是为一般的“我该怎么做”类型的问题而设计的。这是针对特定的“我尝试了X,期望是Y,但得到了Z”类型的问题。话虽如此,我将尽力在一般意义上提供帮助
你需要把你的问题分解成更小的步骤,然后一步一步地去做。您是否可以创建一个单独的独立草图,当您按箭头键时,该草图仅将某些内容打印到控制台
与该草图分开,是否可以创建另一个草图,在草图顶部的变量中存储位置或角度?使用这些变量绘制场景,然后再次使其自行工作
当它们自己工作时,可以考虑在用户按下箭头键时通过更改草图级别变量来组合它们
如果你在某个特定的步骤上遇到困难,请在新的问题中发布一个新的问题,我们将从那里开始。祝你好运。堆栈溢出并不是专门为一般的“我该怎么做”类型问题设计的。这是针对特定的“我尝试了X,期望是Y,但得到了Z”类型的问题。话虽如此,我将尽力在一般意义上提供帮助 你需要把你的问题分解成更小的步骤,然后一步一步地去做。您是否可以创建一个单独的独立草图,当您按箭头键时,该草图仅将某些内容打印到控制台 与该草图分开,是否可以创建另一个草图,在草图顶部的变量中存储位置或角度?使用这些变量绘制场景,然后再次使其自行工作 当它们自己工作时,可以考虑在用户按下箭头键时通过更改草图级别变量来组合它们 如果你在某个特定的步骤上遇到困难,请在新的问题中发布一个新的问题,我们将从那里开始。祝你好运 从0度到90度向上和向下瞄准 这听起来像是增加角度/旋转,而不是增加当前的y位置 您需要将极坐标(角度/半径)转换为笛卡尔坐标(x,y) 这可以使用以下公式完成:
x = cos(angle) * radius
y = sin(angle) * radius
就你而言
tDir.x = cos(angle) * 50;
tDir.y = sin(angle) * 50;
…尽管值得记住,PVector已经通过提供了此功能
以下是为使用此想法而调整的代码:
PVector mPos; //mouse position
PVector tPos; //position of turret
PVector tDir; //the firing direction of the turret
int gravMult = 3;
//angle in radians
float angle = 0;
void setup() {
size(1200, 600);
init();
}
void init() {
//PVector initializations
mPos = new PVector(); //zero til mouse x and y exist
tPos = new PVector(width/8, height * .75);
tDir = new PVector(); //
}
void draw() {
//clear last frame
background(100,100,140);
//check keys to see if there is new key input for turret
if (keyPressed){
if (key == 'w'){
angle -= 0.1;
tDir = PVector.mult(PVector.fromAngle(angle),50);
}
else if (key == 's'){
angle += 0.1;
tDir = PVector.mult(PVector.fromAngle(angle),50);
}
}else if(mousePressed){
mPos.set(mouseX, mouseY);
tDir = PVector.sub(mPos, tPos);
tDir.normalize();
tDir.mult(50);
}
//draw
fill(255);
ellipse(tPos.x, tPos.y, 40, 40);
strokeWeight(5);
line(tPos.x, tPos.y, tPos.x + tDir.x, tPos.y + tDir.y);
fill(255, 0, 0);
ellipse(tPos.x + tDir.x, tPos.y + tDir.y, 10, 10);
}
你可能也会觉得很有帮助
从0度到90度向上和向下瞄准
这听起来像是增加角度/旋转,而不是增加当前的y位置
您需要将极坐标(角度/半径)转换为笛卡尔坐标(x,y)
这可以使用以下公式完成:
x = cos(angle) * radius
y = sin(angle) * radius
就你而言
tDir.x = cos(angle) * 50;
tDir.y = sin(angle) * 50;
…尽管值得记住,PVector已经通过提供了此功能
以下是为使用此想法而调整的代码:
PVector mPos; //mouse position
PVector tPos; //position of turret
PVector tDir; //the firing direction of the turret
int gravMult = 3;
//angle in radians
float angle = 0;
void setup() {
size(1200, 600);
init();
}
void init() {
//PVector initializations
mPos = new PVector(); //zero til mouse x and y exist
tPos = new PVector(width/8, height * .75);
tDir = new PVector(); //
}
void draw() {
//clear last frame
background(100,100,140);
//check keys to see if there is new key input for turret
if (keyPressed){
if (key == 'w'){
angle -= 0.1;
tDir = PVector.mult(PVector.fromAngle(angle),50);
}
else if (key == 's'){
angle += 0.1;
tDir = PVector.mult(PVector.fromAngle(angle),50);
}
}else if(mousePressed){
mPos.set(mouseX, mouseY);
tDir = PVector.sub(mPos, tPos);
tDir.normalize();
tDir.mult(50);
}
//draw
fill(255);
ellipse(tPos.x, tPos.y, 40, 40);
strokeWeight(5);
line(tPos.x, tPos.y, tPos.x + tDir.x, tPos.y + tDir.y);
fill(255, 0, 0);
ellipse(tPos.x + tDir.x, tPos.y + tDir.y, 10, 10);
}
您可能也会发现有帮助。Java没有处理。你会遇到一大群人,他们将处于Java模式而不是处理模式,所以我鼓励你删除对Java的引用,因为这不是Java特有的问题,Java不是处理。你会遇到一大群人,他们将处于Java模式而不是处理模式,所以我鼓励你删除对Java的引用,因为这不是Java特有的问题。