功能display()不存在

时间:2014-12-30 15:11:26

标签: arduino processing

我一直收到错误"函数display()不存在。我只是试图在处理过程中将自己的图像放入预先绘制的椭圆中。这是我的代码:

import processing.serial.*;
import cc.arduino.*;
import com.tinkerkit.*;

Arduino arduino;

//declare the button
TKButton but;
TKButton but1;

{
  arduino = new Arduino(this, Arduino.list()[2], 57600);
}

PFont myFont;

PImage ball1;

int pScore;  

// don't touch em!

int gameState; //0= pre game 1= in game 2= game over

//setting up perimeter to contain ball character
int width = 600;
int height = 600;

void setup() {
  size(width, height);
  smooth();

  //myFont = loadFont("MyFutura.vlw");
  //textFont(myFont);
  gameState = 0;
  ball1 = loadImage("ball.gif");
  pScore = 0;
}

{
  but = new TKButton(arduino, TK.I0);

  but1 = new TKButton(arduino, TK.I1);
}

void draw() {
  background(0);

  if (gameState==0) {
    fill(255, 255, 255, 70);
    rect(-10, 30, 370, 70, 7);

    fill(255, 255, 255, 70);
    rect(-10, 120, 330, 50, 7);

    fill(255);
    textSize(60);

    text("Ball Game", 30, 85);
    textSize(40);

    text("Press B to Start", 30, 157);
    if (keyPressed && key == 'b') {
      gameState = 1;
    }

  }

  if (gameState == 2) {
    fill(255, 255, 255, 70);
    rect(-10, 30, 370, 70, 7);

    fill(255, 255, 255, 70);
    rect(-10, 120, 250, 50, 7);
    fill(255, 255, 255, 70);
    rect(-10, 190, 330, 50, 7);

    fill(255);
    textSize(50);
    text("Final Score:", 20, 85);
    text(pScore, 280, 85);

    textSize(30);
    text("Play Again?", 30, 157);
    textSize(30);
    text("Press R to Restart", 30, 225);

    if (keyPressed && key == 'r') {
      gameState = 0;
      setup();
    }

  }

  ball1.display();
  ball1.keyPressed();
}

void display() {
  fill(255);
  noStroke();
}

void reset() {
   ...
}

class ball1 {   
  float x;
  float y;
  float speed;
  float r; //radius
  color c = color(255, 20, 245);  

  ball1(float tempX, float tempY, float tempR) {
    x = tempX;
    y = tempY;
    r = tempR;
    speed = 0;
  }

  void change() {
    c = color(random(255), random(220), random(245));
  }

  void display() {
    fill (c);
    noStroke();
    ellipse(x, y, r, r);
  }

  //key commands
  //ball flies off page in response to key command
  void keyPressed() {

    if (key == CODED) {
    }

    if (but.read ()== TK.HIGH) {
      x = x+5;
      if (x >= width - 25) {
        x = width - 25;
      }

      println(but.read());
    } else if (but1.read() == TK.HIGH) {
      x = x-5;
      if (x <= 25) {
        x = 25;
      }

      println(but1.read());
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您的ball1变量属于PImage类型。

PImage类没有display()方法。

也许你的ball1变量应该在ball1类中?如果是这样,请将您的类重命名为像Ball这样的理解(换句话说,遵循标准命名约定,不要对类和变量使用相同的名称)。然后将你的ball1变量重命名为像ballImage一样的理智。然后在Ball类的display()函数中,调用image(ballImage,x,y)或其他一个image()函数。

推荐阅读:

https://processing.org/reference/PImage.html

https://processing.org/reference/image_.html