简体   繁体   English

无法使我的Java程序正常工作

[英]Can't get my java program working

I have this program where I want the speed of the ball to move quicker when it is clicked on. 我有一个程序,希望在单击该程序时球的速度更快。 Here are the two classes: 这是两个类:

import processing.core.*;
public class Ball {

int xPos = 0;
int xDir = 1;
int speed = 1;
PApplet parent;


Ball (int _x, PApplet p){
xPos = _x;
parent = p;
}

void move() {
xPos = xPos + xDir * speed;
if (xPos>400-20 || xPos<20)
{
xDir =-xDir;
    }
}

void speedUp() {
    speed=speed+1;  
}

void display() {
parent.ellipse(xPos, 200, 40, 40);   
}

void run(){
      move();
      display();
  }
}

and

import processing.core.*;
public class Game extends PApplet{

public static void main(String args[]){
    PApplet.main(new String[] { "--present", "Game" });
}

Ball b1 = new Ball(xPos,this);

public void setup()
{
  size (400, 400);
  smooth();
  background(0);
  noStroke();
  fill(255);
}

public void draw()
{
  background(0);
  b1.run();
}

public void mousePressed()
  {
    if (dist(mouseX, mouseY, xPos, 200)<=20)
    {
        b1.speedUp();   
    }
  } 
}

I can't find a way to reference to xPos in my Game client so when I click on the ball it will pick up speed. 我找不到在我的游戏客户端中引用xPos的方法,因此,当我单击球时,它将加快速度。 I'm using processing, even though I am not too familiar with it. 我正在使用处理,即使我不太熟悉它。 But it is a requirement in my project. 但这是我项目中的要求。 Need help desperately! 迫切需要帮助!

Ball b1 = new Ball(xPos,this); 

do you have a xpos in the parent applet? 父小程序中有xpos吗? else you have to pass some starting number like 10 and expost a getXPos() in Ball. 否则,您必须传递一些起始编号(如10)并在Ball中发布一个getXPos()。

Also I see that you call the run method in the draw method. 我还看到您在draw方法中调用run方法。 who calls draw? 谁叫抽奖? If its only when repainting then the ball wont be animate. 如果仅在重新粉刷时,则该球将没有动画。 Need to make a thread to make the ball move every second or so. 需要穿线使球每秒钟左右移动一次。

NOTE This should be obvious : even if you add getXPos() in Ball you cannot use it in the constructor of Ball. 注意:这应该很明显:即使在Ball中添加getXPos(),也不能在Ball的构造函数中使用它。 So you must seed it with some other value. 因此,您必须使用其他一些值作为种子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM