处理&#34;用法:PApplet [options] <class name =“”> [sketch args]&#34;

时间:2016-10-18 03:52:03

标签: java eclipse processing

每次编译代码时都会收到此消息,&#34;用法:PApplet [options] [sketch args]有关说明,请参阅Javadoc for PApplet。&#34; 我使用的代码是通过我的旧计算机通过闪存驱动器导入的,并且在该计算机上运行正常。当我把它放在我的工作区后尝试从src打开文件时,它并不认为它是一个项目,所以我把它放在一个新的处理项目中。所以基本上我不确定我是否在代码中安装了错误或错误的处理但我现在收到此错误并且它非常烦人,因为我想要处理这个旧项目。这是代码:

package tests;

import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;
import java.util.Random;

public class test1 extends PApplet {
    PImage background;
    PImage mole;
    PImage mallet1;
    PImage mallet2;

    PFont f;
    public int timer;
    public int startTime;
    public int gameTime;
    public int startGameTime;

    int score = 0;
    Random rnd = new Random();
    boolean mouseP = false;
    int life = 3;

    Mole mole1;
    Mole mole2;
    Mole mole3;
    Mallet mallet;
     enum GameState {
            MENU,
            RUNNING,
            RUNNING2
       }
     static GameState currentState;

    public void setup() {
        size(1000, 800);
        background = loadImage("background.png");
        currentState = GameState.MENU;
        mole = loadImage("mole.png");
        mole1 = new Mole(mole);
        mole2 = new Mole(mole);
        mole3 = new Mole(mole);
        f = createFont("comic.tff",16,true);
        textFont(f,36);
    }

     public void draw() {

            switch(currentState){
            case MENU:
                drawMenu();
                startTime = millis();
                timer = 0;
                life = 3;
                gameTime = millis();
                cursor(CROSS);
                break;
            case RUNNING:
                drawRunning();
                break;
            case RUNNING2:
                drawRunning2();
                gameTime = millis() - startGameTime;

                break;

            }
        }

    public void drawRunning() {
        clear();
        background(background);

        if(timer < 60000){
        mallet2 = loadImage("mallet2.png");
        timer = millis();

        background(background);
        mole1.drawMole();
        mole1.collision(mallet);
        timer = millis() - startTime;
        mallet1 = loadImage("mallet1.png");
        mallet = new Mallet(mallet1, mouseX, mouseY);

        fill(255,255,255);
        text("Time: " + ((60 - timer / 1000)), 850, 50);

        if (mouseP){
            mallet.drawMallet(mallet2, mouseX, mouseY);
        }
        else {
            mallet.drawMallet(mallet1, mouseX, mouseY);
        }
        if(timer > 60000){
            fill(255,255,255);
            text("Game over!" , background.width/2 - 100, background.height/2);

        currentState = GameState.MENU;

        }
        noCursor();
        text("Score: " + score ,25,50);
        }

    }

    public void drawRunning2() {
    clear();
    mallet1 = loadImage("mallet1.png");
    mallet = new Mallet(mallet1, mouseX, mouseY);


    mallet2 = loadImage("mallet2.png");

    background(background);

    timer = millis() - startTime;

    text("Life: " + life ,25,50);

    noCursor();

    text("Time: " + (gameTime / 1000), 825, 50);
    if(life <= 0){
        mole1.dead = true;
        mole2.dead = true;
        mole3.dead = true;
        text("Game over!" , background.width/2 - 100, background.height/2);

        if(mouseP){

            currentState = GameState.MENU;
            timer = 0;
            gameTime = 0;
            startGameTime = millis();
        }

    }

    if (timer < 2000){
        if (!mole1.dead){
            mole1.drawMole();
            mole1.collision(mallet);
        }
        if (!mole3.dead){
            mole3.drawMole();
            mole3.collision(mallet);
        }
        if (!mole2.dead){
            mole2.drawMole();
            mole2.collision(mallet);
        }
        if (mouseP){
            mallet.drawMallet(mallet2, mouseX, mouseY);
        }
        else {
            mallet.drawMallet(mallet1, mouseX, mouseY);
        }
    }
    else {
        startTime = millis();
        if (!mole1.dead || !mole2.dead || !mole3.dead) {
            life --;
        }
        if (life > 0){
            mole1.dead = false;
            mole2.dead = false;
            mole3.dead = false;

            mole1.xPos = rnd.nextInt(925);
            mole1.yPos = rnd.nextInt(725);
            mole3.xPos = rnd.nextInt(925);
            mole3.yPos = rnd.nextInt(725);
            mole2.xPos = rnd.nextInt(925);
            mole2.yPos = rnd.nextInt(725);

            }

        }
    }

    public void drawMenu(){
        clear();
        background(142,22,178);

        fill(165, 119, 249);
        rect(250, 150, 500, 200 );
        fill(255,255,255);
        text("Time Mode", 375, 270);
        fill(165, 119, 249);
        rect(250, 450, 500, 200 );
        fill(255,255,255);
        text("Survival Mode", 375, 570);

    }

            public void mousePressed()
            {
                mouseP = true;

                if( currentState == GameState.MENU && mouseX > 250 && mouseX < 750 && mouseY > 150 && mouseY < 350){
                    currentState = GameState.RUNNING;
                }
                if( currentState == GameState.MENU && mouseX > 250 && mouseX < 750 && mouseY > 450 && mouseY < 650){
                    currentState = GameState.RUNNING2;
            }

            }
                public void mouseReleased()
                {
                mouseP = false;
                }

    public class Mallet{
        PImage mallet1;
        PImage mallet2;
        float xPos1;
        float yPos1;

        public Mallet(PImage mallet1, float xPos1, float yPos1){

            this.mallet1 = mallet1;
            this.xPos1 = xPos1;
            this.yPos1 = yPos1;
        }

        public void drawMallet(PImage mallet1, float xPos1, float yPos1){
            image(mallet1, xPos1 - 40, yPos1 - 60);
        }
    }
    public class Mole{
        PImage Mole;
        float xPos;
        float yPos;
        boolean dead = false;

        public Mole(PImage mole){       
            this.Mole = mole;
            this.xPos = rnd.nextInt(925);
            this.yPos = rnd.nextInt(750);
        }


        public void drawMole(){
            if (dead == true) {
                this.xPos = rnd.nextInt(1000 - mole.width / 2);
                this.yPos = rnd.nextInt(800 - mole.height);
                dead = false;
            }
            image(Mole, xPos, yPos);    
        }   

        public void collision(Mallet m){
            if(
                    mouseP == true &&
            mouseX > xPos && mouseX < xPos + mole.width && mouseY > yPos && mouseY < yPos + mole.height){
                score ++;
                dead = true;
            }
        }
        }
    }

3 个答案:

答案 0 :(得分:1)

我建议先让小件工作。

重新开始一个新项目,并开始工作:

public class ProcessingTest extends PApplet{

    @Override
    public void settings() {
        size(200, 200);
    }

    @Override
    public void draw() {
        background(0);
        fill(255, 0, 0);
        ellipse(100, 100, 100, 100);
    }

    public static void main (String... args) {
        ProcessingTest pt = new ProcessingTest();
        PApplet.runSketch(new String[]{"ProcessingTest"}, pt);
    }
}

这将测试您是否正确设置了处理依赖项。如果你遇到困难,那么你就可以提出更具体的问题。

在您开始工作之后,您可以将小段代码添加到工作示例中,直到它停止工作,这将再次允许您提出更具体的问题。这完全是为了缩小问题范围:您的错误可能与字体无关,那么为什么您的代码包含字体逻辑?换句话说,请发布MCVE

如果你再次陷入困境,请尝试更具体地说明你的错误:你确定它在编译期间发生了,还是在你试图运行的时候?它是什么线?您使用的是什么版本的Processing?您如何期望在没有main()方法的情况下运行此代码?

您还应该尝试使用正确的格式(缩进)和命名约定(类名以大写字母,方法和带小写字母的变量开头)。这将有助于我们阅读您的代码并帮助您发现错误。

我还要说,简单地将eclipse项目从一台计算机复制到另一台计算机通常比它的价值更麻烦。除非您的所有依赖项都在完全相同的位置,否则您将遇到问题。

答案 1 :(得分:1)

我遇到了同样的问题,在Intellij中,有一个简单的解决方案适合您。您在代码中使用app.UseAuthentication();。您应该将package tests添加到tests.test1中。

从右上角转到Program arguments

答案 2 :(得分:0)

我逐字复制了你的代码,为了让它运行,我必须添加的唯一是添加以下内容:

public static void main(String[] args) {
        PApplet.main("test1");
    }

您必须从 main()1 调用对象。请注意,我使用的是 Eclipse。

我还必须将调用 size() 移到 settings(),因为我没有使用 PDE。

    public void settings() {
        size(1000, 800);
    }

否则,在 Eclipse 中,您会收到此错误:

When not using the PDE, size() can only be used inside settings().
Remove the size() method from setup(), and add the following:
public void settings() {
  size(1000, 800);
}
java.lang.IllegalStateException: size() cannot be used here, see https://processing.org/reference/size_.html
    at processing.core.PApplet.insideSettings(PApplet.java:949)
    at processing.core.PApplet.size(PApplet.java:2018)
    at moletest.setup(moletest.java:55)
    at processing.core.PApplet.handleDraw(PApplet.java:2432)
    at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
    at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)

一旦我这样做了,那么 PApplet 就会运行并完美地显示第一个窗口。

但是,由于缺少某些资源,我(可以理解)出现以下错误:

The file "background.png" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
The file "mole.png" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
"comic.tff" is not available, so another font will be used. Use PFont.list() to show available fonts.

总而言之,您的代码似乎没问题。但是,如果您更改编译环境,则可能需要进行一些小的更改。


脚注

1How to write and run processing code in Eclipse? How to make Eclipse a second home for processing?

<块引用>
    在 Java 中需要
  1. main(String args[]); 来表示 从这里开始。 Processing 有一个预处理器可以为您执行此操作,但在 Eclipse 中您必须自己添加它。
相关问题