使用某些 UI 元素后模拟停止

Posted

技术标签:

【中文标题】使用某些 UI 元素后模拟停止【英文标题】:Simulation stops after certain UI elements are used 【发布时间】:2022-01-06 08:23:57 【问题描述】:

我正在处理一个正在处理的项目,但我在处理这个超级烦人的错误时遇到了一些困难。全部代码贴在下面,4个类文件。

每当“模拟速度”滑块向上移动然后向下移动时,模拟就会停止,不会出现错误。与“自动”按钮相同。我不知道为什么会发生这种情况,我已经按照变量检查了我的代码,我没有发现任何错误。

救命!

noisedemo 文件

int tableHeight = 20;
int tableWidth = 20;
float prismWidth = 10;
float tableTopx = 260;
float tableTopy = 200;
int k = 0;

int simulationSpeed;
int frameCounter;

Prism[][] prisms = new Prism[tableWidth][tableHeight];

//Declare the sliders

Slider simulationSpeedSlider = new Slider(10, 20, 100, 30, "Simulation Speed");
Slider noiseOctaveSlider = new Slider(10, 50, 100, 4, "Noise Ocatave");
Slider noiseScaleSlider = new Slider(10, 80, 100, 10, 10, "Noise Scale", true);

//Declare the buttons

Button autoScrollButton = new Button(400, 100, "Auto");

void setup()
  size(500, 500);
  smooth();
  pixelDensity(2);
  frameRate(30);
  noiseDetail((int)noiseOctaveSlider.sliderValue, noiseScaleSlider.sliderValue);  
  for(int i = 0; i < 20; i++)
    for(int j = 0; j < 20; j++)
      prisms[i][j] = new Prism(noise(i, j)*30+10, prismWidth);
      prisms[i][j].setHeight(noise(i, j)*30+10);
    
  


float[] convertCoordinate(int xcoord, int ycoord, float xtop, float ytop, float pwidth)
  float[] coord = new float[2];
  
  coord[0] = (xtop + xcoord*cos(radians(40))*prismWidth - ycoord * cos(radians(40))*prismWidth)*0.95;
  coord[1] = (ytop + ycoord*sin(radians(40))*prismWidth + xcoord * sin(radians(40))*prismWidth)*0.95;
  
  return coord;


void updateHeight()
  for(int i = 0; i < tableWidth; i++)
    for(int j = 0; j < tableHeight; j++)
      prisms[i][j].drawPrism(convertCoordinate(i, j, tableTopx, tableTopy, prismWidth)[0], convertCoordinate(i, j, tableTopx, tableTopy, prismWidth)[1]);
    
  


void setHeight()
  for(int i = 0; i < tableWidth; i++)
    for(int j = 0; j < tableHeight; j++)
      prisms[i][j].setHeight(noise(i+k, j+k)*30+10);
    
  


void mouseClicked()
  if(autoScrollButton.isInRange())
    autoScrollButton.buttonActive = !autoScrollButton.buttonActive;
  


void draw()
  background(0, 150, 0);
  noiseDetail((int)noiseScaleSlider.sliderValue, noiseScaleSlider.sliderValue);
  
  simulationSpeedSlider.drawSlider();
  simulationSpeed = (int)simulationSpeedSlider.sliderValue;
  noiseOctaveSlider.drawSlider();
  noiseScaleSlider.drawSlider();
  
  autoScrollButton.drawButton();
  
  if(frameCounter == simulationSpeed && autoScrollButton.buttonActive)
    k++;
    frameCounter = 0;
    setHeight();
  
  
  updateHeight();
  
  frameCounter++;

按钮文件

class Button
  
  boolean buttonActive = true;
  float buttonX;
  float buttonY;
  float buttonWidth = 30;
  float buttonHeight = 15;
  String buttonLabel;
  
  Button(float x, float y, String label)
    buttonX = x;
    buttonY = y;
    buttonLabel = label;
  
  
  
  void drawButton()
    
    
    if(!buttonActive)
      fill(100);
      rect(buttonX, buttonY, buttonWidth, buttonHeight, 5);
  
      fill(255);
      text(buttonLabel, buttonX + 4, buttonY + 10);
    else
      fill(200);
      rect(buttonX, buttonY, buttonWidth, buttonHeight, 5);
  
      fill(0);
      text(buttonLabel, buttonX + 4, buttonY + 10);
    
    
    
  
  boolean isInRange()
    
    if(mouseX > buttonX && mouseX < buttonX + buttonWidth && mouseY > buttonY && mouseY < buttonY + buttonHeight)
      return true;
    
    else
      return false;
    
    
  
  

棱镜文件

class Prism
  
  float pheight;
  float pwidth;
  
  Prism(float tempheight, float tempwidth)
    pheight = tempheight;
    pwidth = tempwidth;
  
  
  Prism()
    pheight = 10;
    pwidth = 5;
  
  
  void drawPrism(float x, float y)
    noStroke();
    fill(175);
    quad(x, y, x - cos(radians(40))*pwidth, y - sin(radians(40))*pwidth, x - cos(radians(40))*pwidth, y - sin(radians(40))*pwidth - pheight, x, y - pheight);
    fill(100);
    quad(x, y, x + cos(radians(40))*pwidth, y - sin(radians(40))*pwidth, x + cos(radians(40))*pwidth, y - sin(radians(40))*pwidth - pheight, x, y - pheight);
    fill(225);
    quad(x, y - pheight, x - cos(radians(40))*pwidth, y - sin(radians(40))*pwidth - pheight, x, y - 2*sin(radians(40))*pwidth - pheight, x + cos(radians(40))*pwidth, y - sin(radians(40))*pwidth - pheight);
  
  
  void setHeight(float tempheight)
    pheight = tempheight;
  
  

滑块文件

class Slider
  
  float sliderLength;
  float sliderX;
  float sliderY;
  float sliderWidth;
  float handleWidth;
  float sliderValue;
  float sliderPos;
  int sliderTotal;
  
  boolean handleActive;
  boolean isPrecise;
  String sliderLabel;
  
  //provide x and y position, length and the max value, the least amount of info
  Slider(float x, float y, float slength, int total)
    sliderX = x;
    sliderY = y;
    sliderLength = slength;
    sliderWidth = 10;
    handleWidth = sliderWidth*1.5;
    sliderTotal = total;
    sliderPos = sliderX + sliderWidth/2;
    sliderLabel = "";
  
  
  //provide x and y position, length and the max value AND a label
  Slider(float x, float y, float slength, int total, String label)
    sliderX = x;
    sliderY = y;
    sliderLength = slength;
    sliderWidth = 10;
    handleWidth = sliderWidth*1.5;
    sliderTotal = total;
    sliderPos = sliderX + sliderWidth/2;
    sliderLabel = label;
  
  
  //provide default values AND a label AND a width AND whether its precise
  Slider(float x, float y, float slength, float swidth, int total, String label, boolean p)
    sliderX = x;
    sliderY = y;
    sliderLength = slength;
    sliderWidth = swidth;
    handleWidth = sliderWidth*1.5;
    sliderTotal = total;
    sliderPos = sliderX + sliderWidth/2;
    sliderLabel = label;
    isPrecise = p;
  
  
  void drawSlider()
    fill(255);
    textSize(10);
    text(sliderLabel, sliderX, sliderY - 5);
    text(sliderValue, sliderX + sliderLength + 7, sliderY + sliderWidth/1.1);
    
    fill(100);
    rect(sliderX-1, sliderY, sliderLength+1, sliderWidth, 5);
    fill(255);
    ellipse(sliderPos, sliderY + (sliderWidth/2), handleWidth, handleWidth);
    
    if(!isPrecise)
      sliderValue = (int)((sliderPos - sliderX)/(sliderLength/sliderTotal));
    else if(isPrecise)
      sliderValue = (sliderPos - sliderX)/(sliderLength/sliderTotal);
    
    
    if(mouseX >= sliderX && mouseX <= sliderX+sliderLength + sliderX && mouseY >= sliderY - (handleWidth/2) && mouseY <= sliderY + (handleWidth/2) && mousePressed)
      handleActive = true;
    
    if(!mousePressed)
      handleActive = false;
    
    if(handleActive && sliderPos <= sliderX + sliderLength && sliderPos >= sliderX)
      sliderPos = mouseX;
    
    if(sliderPos > sliderX + sliderLength)
      sliderPos = sliderX + sliderLength;
    
    if(sliderPos < sliderX)
      sliderPos = sliderX + 1;
    
  
  

谢谢!对不起,如果代码有点乱:)

【问题讨论】:

【参考方案1】:

在您的主文件中,此块依赖于这两个控件的值:

if(frameCounter == simulationSpeed && autoScrollButton.buttonActive)
  k++;
  frameCounter = 0;
  setHeight();

当它被跳过时(因为您更改了 simulationSpeedbuttonActive),您的 frameCountersimulationSpeed 不同步,因此该块继续被跳过。

【讨论】:

以上是关于使用某些 UI 元素后模拟停止的主要内容,如果未能解决你的问题,请参考以下文章

想要在 Xcode 中的 UI 测试期间在测试运行后停止调试控制台清除

没有模拟器或设备的 IOS UI 测试

向下滚动以查找文本在 android 模拟器中不起作用的元素

JQuery:模拟球员在球场上移动

模拟器应用程序在启动第二个活动后停止

Android 从命令行停止模拟器