"字体错误myFont = createFont(" Verdana",48,true);"在游戏中

时间:2014-11-24 22:00:29

标签: fonts processing

遇到错误“找不到任何名为”myFont“的东西。使用”myFont = createFont(“Verdana”,48,true);“在游戏程序中,不确定我是否有拼写错误或真正的错误。通过程序多次校对并且没有提出任何建议。 如果这是输入错误,我会感到很愚蠢...... 试图通过tolbox加载,但这似乎没有工作...... 有程序和课程部分 - 将发布两者。

牌:

class Cards
{
  int show = 0; // shows the back of the card
  PImage cardImage;

  int cardX = 0;
  int cardY = 0;
  int faceValue = 0;
  int myPoints = 0;

  // card back is 00 so there are '53' cards vs 52

  String[] cardName = {
    "00.png", "01.png", "02.png", "03.png", "04.png", "05.png", "06.png", "07.png", "08.png", "08.png", 
    "09.png", "10.png", "11.png", "12.png", "13.png", "14.png", "15.png", "16.png", "17.png", "18.png", 
    "19.png", "20.png", "21.png", "22.png", "23.png"     
  };

  Cards (int x, int y, int fv, int mp)
  {
    cardX = x;
    cardy = y;
    faceValue = fv;
    myPoints = mp;  //keeps track of the points J=11, Q=12, K=13
  }

  void display()
  {    
    cardImage= loadImage(cardName[show]); //Display loads the images.
    image(cardImage, cardX, cardY);
  }

  void setX(int newX)  //move the card from the deck to the center and 
  {
    cardX = newX;
    show = faceValue;
  }

  int getPoints()  //gets the points from the card values compare the left to the right.
  {
    return (myPoints);
  }
}

程序:

void setup()    
{
  background(0);
  int myY =75;
  int myX1 = 430;  //each pile has its own x value
  int myX2 = 50;
  size(600, 600);
  fill(255, 0, 0);
  myFont = createFont("Verdana", 48, true);
  for (int i = 0; i<20; i++)
  {
    y[i] = myY;
    fv[i] = i+1;
    points [i] = currentPoint;
    currentPoint ++;
    if (currentPoint > 10)  //since only doing 10 cards each side - total 20 change this
    {
      currentPoint=1;  //then resetting at one - counts  -10 and then starts over.
      //so the point value matchs the face value of the cards. ace =1, 2=2, etc j=11, q=12, k=13
    }

    if (i%2==0)    
    {
      x[i] = myX1;
      myY+=2;  //changes the pixels of the cards so they are not all on top of eachother
      // the cards will show slightlu fanned down and to the right with these functions
      myX1+=2;
      myX2+=2;
    }
    else
    {
      x[i] = myX2;
    }
  }

  shuffle(); // shuffle a couple of times
  shuffle();

  for (int i=0; i<20; i++)
  {
    myCard[i] = new Cards (x[i], y[i], fv[i], points[i]);
  }
}

void draw()
{
  background(0);
  textFont(myFont);
  text(leftPoint, 75, 50);
  text(rightPoint, 515, 50);
  rect(200, 400, 200, 100);
  for (int i=0; i<20; i++)
  {
    myCard[i].display();  //display cards on the screen
  }

  if (leftDeck>19)  //CHANGE means went throug all the cards
  {
    if (leftPoint > rightPoint)
    {
      text("Left Wins!", 250, 350);
    }
    else if (leftPoint<rightPoint)
    {
      text("Right Wins!", 250, 350);
    }
    else
    {
      text("you TIED!!!!", 250, 350);
    }
  }
}

void mouseClicked()
{
  if (mouseX<400 && mouseX>200 && mouseY<500 && mouseY>400)
  {
    myCard[leftDeck].setX(190);  //set x value for left card display
    myCard[rightDeck].setX(310); //sets x value for right card display
    if (myCard[leftDeck].getPoints()>myCard[rightDeck].getPoints())
    {
      leftPoint++;
    }
    else if (myCard[leftDeck].getPoints()<myCard[rightDeck].getPoints())
    {
      rightPoint++;
    }  
    leftDeck+=2;
    rightDeck+=2;
  }
}

void shuffle()
{
  int temp = 0;
  int tempPoint =0;
  int rand = 0;
  for (int i=0; i<20; i++)  //CHANGE to 52
  {
    rand = int(random(0, 20));  //showing 20 needs to be 52
    temp = fv[i];
    tempPoint = points[i];
    fv[i] = fv[rand];
    points[i] = points[rand];  //have to move the point value with the facevalue
    fv[rand] = temp;
    points[rand] = tempPoint;
  }
}

1 个答案:

答案 0 :(得分:0)

这似乎相当明显:错误告诉您myFont在您调用它的上下文中不存在。这是有道理的:在初始化之前,你从未通过该名称声明变量。与setupdraw处于同一级别的某个位置,您需要发出:

Font myFont;

因此,当setup()运行时,会有一个名为myFont的实际变量,可以保存createFont调用的结果。