Use random in command prompt, create the class for it

edited January 2017 in Questions about Code

Hello. I have a code which works on processing but in command prompt has an error, asks me to create a class for the random, how can i do that please?

import processing.core.PApplet;
import processing.core.PVector;
import processing.core.*;
import java.util.*;



public class Bike{
    int colour;
    float xPos;
    float yPos;
    float xSpeed;
    float ySpeed = -2;
    int loctn;
    boolean moveLeft, moveRight, moveUp, moveDown;
    String name;
    PApplet parent;
    int x1, y1;
    int x2, y2;
    int x3, y3;
    int xdirection = 1;  // Left or Right
    int ydirection = 1;  // Top to Bottom
    int rad = 10;
    int currentDir=3; // east  
    boolean stateAfterWallCollision=false;
    int oldFrameCount = 0; 
    int lastRandomTurnFrameCount = 0;
    final int north=0; 
    final int west=1;
    final int south=2; 
    final int east=3;




    public Bike( PApplet p, String name,  int colour, float xPos, float yPos, float xSpeed){
        parent = p;
        this.colour = colour;
        this.xPos = xPos;
        this.yPos = yPos;
        this.xSpeed = xSpeed;
        this.name = name;
        this.x1 = 5;
        this.y1 = 20;
        this.x2 = 5;
        this.y2 = 30;
        this.x3 = 15;
        this.y3 = 25;
    }

    public void display(){      
        parent.rectMode(parent.CENTER);
        parent.fill(colour);
        //parent.ellipse(xPos, yPos, 20, 20);
        //parent.triangle(xPos+x1, yPos+y1, xPos+x2, yPos+y2, xPos+x3, yPos+y3);
    }
    public void display(){      
        parent.rectMode(parent.CENTER);

        if (stateAfterWallCollision)
            parent.fill(0, 255, 0); // red 
        else
            parent.fill(colour);

        switch (currentDir) {

        case 0:
          // north
            parent.triangle(xPos+18, yPos+10, xPos+23, yPos+20, xPos+12, yPos+20);
          break;

        case 1:
          // West
            parent.triangle(xPos+22, yPos+25, xPos+32, yPos+30, xPos+32, yPos+20);
          break;

        case 2:
          // south
            parent.triangle(xPos+y1, yPos+x1, xPos+y2, yPos+x2, xPos+y3, yPos+x3);
          break;

        case 3:
          // east
            parent.triangle(xPos+x1, yPos+y1, xPos+x2, yPos+y2, xPos+x3, yPos+y3);
          break;

        default:
          // error 
            parent.println("error 58");
          break;
        }

    }

    public void walk() {

            // move 
            xPos = xPos + xSpeed;
            yPos = yPos + ySpeed;

            // collision 
            if (xPos > parent.width-20) {
              xPos = parent.width-20;
              PVector newMovment = new PVector(-5.0, 0);
              xSpeed = newMovment.x;
              ySpeed = newMovment.y;
              currentDir=west; 
              stateAfterWallCollision=true; 
              oldFrameCount=parent.frameCount;
            }
            if (yPos > parent.height-20) {
              yPos = parent.height-20;
              // PVector newMovment = doTheThingBottom();
              ySpeed = -parent.abs(ySpeed);
              currentDir=north; 
              stateAfterWallCollision=true;
              oldFrameCount=parent.frameCount;
            }
            if (xPos < 0) {
              xPos = 0;
              xSpeed = parent.abs(xSpeed);
              currentDir=east; 
              stateAfterWallCollision=true;
              oldFrameCount=parent.frameCount;
            }
            if (yPos < 0) {
              yPos = 0;
              ySpeed = parent.abs(ySpeed);
              currentDir=south; 
              stateAfterWallCollision=true;
              oldFrameCount=parent.frameCount;
            }

            // random turn
            if (parent.random(100)>97 && 
              !stateAfterWallCollision &&
              parent.frameCount-lastRandomTurnFrameCount>39) {
                PVector newMovment = randomTurn();
              xSpeed = newMovment.x;
              ySpeed = newMovment.y;
            }

            // it flees mouse
            if (parent.dist(xPos, yPos,parent. mouseX, parent.mouseY)<44 &&
                    parent.frameCount-lastRandomTurnFrameCount>29) {
                PVector newMovment = randomTurn();
              xSpeed = newMovment.x;
              ySpeed = newMovment.y;
            }

            // ending state After Wall Collision
            if (stateAfterWallCollision && parent.frameCount-oldFrameCount>19) {
                PVector newMovment = randomTurn();
              xSpeed = newMovment.x;
              ySpeed = newMovment.y;
              stateAfterWallCollision=false;
            }
          }

          PVector randomTurn() {

            // random turn

            PVector result=new PVector(0, 0);

            int oldDir = currentDir; 

            int randomDirection = int (random(4));                         //line of error in command prompt 

            // if the result is unsatisfying
            if (oldDir == randomDirection) {
              // leave
              return (new PVector(xSpeed, ySpeed));
            }

            currentDir = randomDirection;  

            switch(randomDirection) {

            case 0:
              // north
              result = new PVector(0, -5f);
              break;

            case 1:
              // West
              result = new PVector(-5f, 0);
              break;

            case 2:
              // south
              result = new PVector(0, 5f);
              break;

            case 3:
              // east
              result = new PVector( 5f, 0);
              break;

            default:
              // error 
                parent.println("error 134");
              result = new PVector( 5f, 0);
              break;
            }//switch  

            lastRandomTurnFrameCount = parent.frameCount; 
            return result;
          }
        }

//_____________________________________________

import processing.core.PApplet;
public class UseBike extends PApplet{
    Bike[] bikes;
    public static void main(String[] args){
        PApplet.main("UseBike");
    }

    public void settings(){
        size(1000, 500);
        smooth();
    }

    public void setup(){
        bikes = new Bike[4];
        bikes[1] = new Bike(this, "greenBike", color(0, 255, 0), (float)(width/1000),(float)(height/2), 5f);

    }

    public void draw(){
        background(240);        

        bikes[1].display(); 

        bikes[1].walk();

    }


} 
Tagged:

Answers

  • edited January 2017

    but in command prompt has an error

    What does this mean? "In command prompt"

    asks me to create a class for the random

    Verbatim errors, with a link to the line in question, if applicable, are so much more useful than "has an error"

  • Ok, random is a method in Papplet and Bike would normally be an inner class of the papplet and therefore have access to it. Your second but of code uses Bike as a standalone class which won't have access to random ().

    So, either pass the papplet into bike and use that or, if random is static (I can't remember) then you can use PApplet.random() everywhere. But don't be surprised if you find other errors after fixing random.

  • For a hint as to how processing does this, generate an executable and look in there for a Java file.

    There's more info about passing papplet around in the eclipse tutorial.

  • Hello. "but in command prompt has an error", I tried to compile and run in Command prompt, which is where i have to make the code work at the end. Can you help with the changes? I'm new at it, can't really see how to do it. I would really appreciate that please. Thanks very much.

  • https://processing.org/tutorials/eclipse/

    specifically the "Processing in Eclipse with Multiple Classes" section

  • edited January 2017

    I have transferred the library of processing into Eclipse before, couldn't do it yet. This is the error

    Error 1 - in Command prompt

    C:\Work>javac -cp ;core.jar Robot.java
    Robot.java:304: error: '.class' expected
                        int randomDirection = int (random(4));               
                                                  ^
    Robot.java:304: error: ';' expected
                        int randomDirection = int (random(4));
    

    Error 2 - in Eclipse

    - The method random(int) is undefined for the type 
     Robot
    - Syntax error on token "int", delete this token
    - The method random(int) is undefined for the type 
     PVector
    
  • that's the same problem. the solution is the one i've posted twice and gotoloop has posted once.

  • I am importing the "import processing.core.PApplet" I have the variable, I am using it in my constructor as well, like in the examples.

  • read the tutorial again. pay attention to the parent stuff in the class.

  • Answer ✓

    this picture here:

    that red underlining is exactly what you're seeing - random() is not defined for the class in question.

Sign In or Register to comment.