首页 > 解决方案 > 对象释放另一个较小的对象?

问题描述

谁能帮我?

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

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

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

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

解决方案


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

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;
}

推荐阅读