如何从我点击的地方创建一个移动的对象
How to create a moving object from where I clicked
我是处理新手,我正在尝试创建一个 Space Invaders 类型的机制,我从船上发射子弹,但是我在尝试让子弹从x 坐标我点击并继续沿 X 轴向上并远离飞船。我正在尝试使用 mouseClicked,但我最终得到的是子弹跟随 mouseX 或根本不在 Y 轴上移动,感谢任何帮助。
Hero theHero;
Bullet theBullet;
void setup(){
size(300,600);
theHero = new Hero(color(255,0,0),mouseX,mouseY);
theBullet = new Bullet(color(255,255,0),300,600,-4);
}
void draw(){
background(255);
theHero.move();
theHero.display();
theBullet.displayb();
theBullet.mouseClicked();
}
class Hero {
color c;
float xpos;
float ypos;
Hero(color tempC,float tempXpos, float tempYpos){
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
}
void display() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos, ypos, 20, 10);
}
void move() {
xpos = mouseX;
ypos = 580;
}
}
class Bullet {
color c;
float xpos;
float ypos;
float yspeed;
Bullet(color tempC, float tempXpos, float tempYpos, float tempYspeed) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
yspeed = tempYspeed;
}
void displayb() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos, ypos, 5, 5);
}
void mouseClicked(){
xpos = mouseX;
ypos = ypos + yspeed;
if (ypos < 0) {
ypos = 580;
xpos = mouseX;
}
}
}
这是因为您在绘制循环的每一帧都调用了 mouseClicked() 方法。不断改变子弹的 xpos 到 mouseX 的位置。为了从 draw() 函数中删除 theBullet.mouseClicked() 。
您还需要创建项目符号对象数组。并画出它们中的每一个。在 mouseClicked()(每次单击鼠标时都会调用)中,使用当前 Hero 位置的 xpos 创建一个新的 Bullet 对象。这应该是这样的。
void mouseClicked(){
bulletCount++;
float startX = theHero.xpos;
bullets[bulletCount] = new Bullet(color(255,0,0),startX,theHero.ypos, -5 /*YSPEED*/);
}
为了创建和绘制所有 Bullet 对象,您需要一个数组。这可以像这样创建和显示:
Hero theHero;
public Bullet[] bullets = new Bullet[10000];
public static int bulletCount = 0;
void setup(){
size(300,600);
theHero = new Hero(color(255,0,0),mouseX,mouseY);
}
void draw(){
background(255);
for(int i = 1; i <= bulletCount;i++){
bullets[i].updatePos();
bullets[i].displayb();
}
theHero.move();
theHero.display();
}
我还向 Bullet 对象添加了一个 updatePos() ,它每帧按 y 速度递增其 y 位置。如果你想要完整的代码,只需复制这个 pastebin:https://pastebin.com/2CpLPTnQ
我是处理新手,我正在尝试创建一个 Space Invaders 类型的机制,我从船上发射子弹,但是我在尝试让子弹从x 坐标我点击并继续沿 X 轴向上并远离飞船。我正在尝试使用 mouseClicked,但我最终得到的是子弹跟随 mouseX 或根本不在 Y 轴上移动,感谢任何帮助。
Hero theHero;
Bullet theBullet;
void setup(){
size(300,600);
theHero = new Hero(color(255,0,0),mouseX,mouseY);
theBullet = new Bullet(color(255,255,0),300,600,-4);
}
void draw(){
background(255);
theHero.move();
theHero.display();
theBullet.displayb();
theBullet.mouseClicked();
}
class Hero {
color c;
float xpos;
float ypos;
Hero(color tempC,float tempXpos, float tempYpos){
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
}
void display() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos, ypos, 20, 10);
}
void move() {
xpos = mouseX;
ypos = 580;
}
}
class Bullet {
color c;
float xpos;
float ypos;
float yspeed;
Bullet(color tempC, float tempXpos, float tempYpos, float tempYspeed) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
yspeed = tempYspeed;
}
void displayb() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos, ypos, 5, 5);
}
void mouseClicked(){
xpos = mouseX;
ypos = ypos + yspeed;
if (ypos < 0) {
ypos = 580;
xpos = mouseX;
}
}
}
这是因为您在绘制循环的每一帧都调用了 mouseClicked() 方法。不断改变子弹的 xpos 到 mouseX 的位置。为了从 draw() 函数中删除 theBullet.mouseClicked() 。
您还需要创建项目符号对象数组。并画出它们中的每一个。在 mouseClicked()(每次单击鼠标时都会调用)中,使用当前 Hero 位置的 xpos 创建一个新的 Bullet 对象。这应该是这样的。
void mouseClicked(){
bulletCount++;
float startX = theHero.xpos;
bullets[bulletCount] = new Bullet(color(255,0,0),startX,theHero.ypos, -5 /*YSPEED*/);
}
为了创建和绘制所有 Bullet 对象,您需要一个数组。这可以像这样创建和显示:
Hero theHero;
public Bullet[] bullets = new Bullet[10000];
public static int bulletCount = 0;
void setup(){
size(300,600);
theHero = new Hero(color(255,0,0),mouseX,mouseY);
}
void draw(){
background(255);
for(int i = 1; i <= bulletCount;i++){
bullets[i].updatePos();
bullets[i].displayb();
}
theHero.move();
theHero.display();
}
我还向 Bullet 对象添加了一个 updatePos() ,它每帧按 y 速度递增其 y 位置。如果你想要完整的代码,只需复制这个 pastebin:https://pastebin.com/2CpLPTnQ