【问题标题】:object releases another smaller object?对象释放另一个更小的对象?
【发布时间】:2021-05-24 06:33:39
【问题描述】:

谁能帮帮我?

所以,我应该有一个水平移动的球,这样每次我按下鼠标时,一个球就会垂直射出,然后由于摩擦而减速。垂直球将保持在旧位置,但球员会重置。

如何在不使用类的情况下做到这一点?

到目前为止,这是我的代码:

boolean circleupdatetostop = true;
float x = 100;

float yshot = 880;

float speedshot =  random(4,10);
float speedx = 6;


void setup() {
 size(1280,960); 
}

void draw() {
 background(255);
  
 stroke(0);
 fill(0);
 circle(x,880,80);
 
   if (x > width || x < 0 ) {
   speedx = speedx * -1;
   }
   
   if (circleupdatetostop) {
   x = x + speedx;
   }
  
 if (circleupdatetostop == false) {
   float locationx = x;
   stroke(0);
   fill(255,0,255);
   circle(locationx,yshot,30);
   yshot = yshot - speedshot; 
} 
}

void mousePressed () {
  circleupdatetostop = !circleupdatetostop;
}

【问题讨论】:

    标签: processing


    【解决方案1】:

    我不完全确定这是否是您的意思,但是您可以通过使用ArrayList 以及处理的PVector 来更好地处理 x 和 y 坐标对来实现射击多个球。如果您想查看课程,请参阅this post

    import java.util.*;
    
    // Whether the ball is moving or not
    boolean circleupdatetostop = true;
    
    // Information about the main_ball
    PVector position = new PVector(100, 880);
    PVector speed = new PVector(6, 0);
    float diameter = 80;
    
    // Information about the sot balls
    ArrayList<PVector> balls_loc = new ArrayList<PVector>();
    ArrayList<PVector> balls_speed = new ArrayList<PVector>();
    float diameter_shot = 30;
    float friction = 0.994;
    
    void setup() {
      size(1280, 960);
    }
    
    void draw() {
      background(255);
    
      stroke(0);
      fill(0);
      circle(position.x, position.y, diameter);
    
      // Remember to consider the radius of the ball when bouncing off the edges
      if (position.x + diameter/2 > width || position.x - diameter/2 < 0 ) {
        speed.mult(-1);
      }
    
      if (circleupdatetostop) {
        position.add(speed);
      }
      
      // Cycle through the list updating their speed and draw each ball
      for (int i = 0; i<balls_loc.size(); i++) {
        balls_speed.get(i).mult(friction+random(-0.05, 0.05));
        balls_loc.get(i).add(balls_speed.get(i));
    
        stroke(0);
        fill(255, 0, 255);
        circle(balls_loc.get(i).x, balls_loc.get(i).y, diameter_shot);
      }
    }
    
    void mousePressed(){
      // Add a new ball to be drawn
      if(circleupdatetostop){
        balls_loc.add(new PVector(position.x, position.y));
        balls_speed.add(new PVector(0, random(-4, -10)));
      }
      circleupdatetostop = !circleupdatetostop;
    }
    

    【讨论】:

    • 大量使用 PVector 和 ArrayList:使代码更加简洁易读(与使用浮点数组和不使用 PVector 相比)(+1)。也非常注重细节(例如在边缘弹跳时考虑球的尺寸)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多