当前位置: 首页 > 知识库问答 >
问题:

处理“用法:PApplet[options][sketch args]”

淳于慎之
2023-03-14

每次编译代码时,我都会收到这样一条消息:“用法:PApplet[options][sketch args]请参阅Javadoc for PApplet以获取解释。”我使用的代码是通过闪存驱动器从我的旧电脑中导入的,在那台电脑上运行时效果良好。当我试图把文件从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个答案

强化
2023-03-14

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

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

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

由于我没有使用PDE,我还不得不将调用移动到size()settings()

    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.

总而言之,你的代码看起来不错。但是,如果更改编译环境,则可能需要进行一些小的更改。

1请参见如何在Eclipse中编写和运行处理代码?如何使Eclipse成为处理的第二个家?

万高畅
2023-03-14

我遇到了同样的问题,在Intellij中,有一个简单的解决方案。您正在代码中使用包测试。您应该添加测试。test1插入程序参数。

从右上角,选择编辑条件

华旭
2023-03-14

我建议先弄点小点儿的。

从一个新项目开始,让这个工作:

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。

如果你再次陷入困境,试着更具体地描述你的错误:你确定它发生在编译过程中,还是在你试图运行的时候?它在什么线上?您使用的是哪个版本的处理?你是怎么运行代码的?(我没有看到一个main()方法?)

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

我还要说,从一台计算机到另一台计算机复制一个eclipse项目通常比它的价值更麻烦。除非你所有的依赖都在完全相同的地方,否则你会有问题。相反,我建议创建一个新项目,并且只复制代码。

 类似资料:
  • 我有一个作业,我必须通过PApplet处理器显示“健身房机器”和它们的数据。我需要使用多态性和继承(跑步机是体操机,等等)。每个健身房机器必须有自己的绘制方法。我只是想不出如何避免NullPointerException。 这是我的“processingexample.java”文件中的代码,它将通过处理器实际绘制图像: 在过去的5个小时里,我一直无法弄清楚为什么会抱怨NullPointerExc

  • 我在其他的帖子里读到过,但我想不通。 我读过select也试过,但我想不通。

  • 问题内容: 我的Sencha Touch应用正在将表单发布到我的 WebService上,但不是发送而是发送。 我在这里读取了类似的线程,但是我只是不知道如何在代码中处理该方法。 我确实尝试将属性添加到Action中,但是MVC3中似乎不存在该属性。 选项/ GetInTouch / CommunicateCard HTTP / 1.1 主机:webservice.example.com 引用 网

  • 问题内容: 看来我应该这样做: 通过将其设置为来配置dispatcherServlet 但是,考虑到我没有XML配置,或者代码中没有任何各种初始化程序类(此答案提到),该怎么做? 在一个类中,我有一个像这样的方法,当前不会被调用。 Spring Boot 1.2.7.RELEASE; 一个简单的设置与Spring REST指南并没有太大区别 。 问题答案: 选项1:Spring Boot属性(仅适

  • 问题内容: 是否可以为某种HTTP方法禁用Spring Security? 我们有一个Spring REST应用程序,其服务需要在HTTP请求的标头中附加授权令牌。我正在为此编写一个JS客户端,并使用JQuery发送GET / POST请求。该应用程序使用此过滤器代码启用了CORS。 但是,当JQuery发送针对CORS的OPTIONS请求时,服务器将使用授权失败令牌进行响应。显然,OPTIONS

  • 我遵循这个链接的第二个答案(两个步骤)来实现视频到Youtube的上传:使用curl和api v3在Youtube上上传视频 代码工作正常,但当我执行时,我得到的响应是:状态:嵌入:true license:“YouTube”privacystatus:“unlisted”publicstatsviewable:true uploadstatus:“uploaded”