Translate from Khan Academy (javascript) to Processing. Help!

Hey I was just wondering what I would need to do to translate this code into Processing. I made it with on Khan Academy using their language. It's supposed to be a minefield. It's a random selection of "mines" or rectangles randomly spawning from the top of the screen, falling down the screen then disappearing off the bottom.

I'm not sure what the correct translations are going from their language to processing. I'm fairly new to programming so it's still new to me. I know that var (declaring variables?) should be -> int on processing, but the brackets such as "var b=[];" don't translate at all if I put "int b=[];" in processing. Any help would be awesome, thanks.

Here's what I got. It works at KhanAcademy's website but not on Processing.

var b=[];
var mineX=random(50,350);
var mineY=random(-400,-800);
var mine=[];
var bsX=2;
var bsY=4;
var mFill=255;
draw= function() {
    fill(0);
    rect(-1,-1,402,402);

    mine.push([random(-2000,4000),0]);
    for(var i in mine){
        noStroke();
        noFill();
        stroke(255, 255, 255);
        fill(mFill, 0, 0);
        rect(mine[i][0],mine[i][1],20,20,5);
        mine[i][1]+=9;

        if(mine[i][1]>400){
            mine.splice(i,1);
        }
    }

    strokeWeight(2);
    stroke(255, 255, 255);
    fill(255, 0, 0);
    rect(mineX-10,mineY-10,20,20,5);
    mineY+=9;
    if(mineY>400){
        mineY=random(-400,-800);
        mineX=random(50,350);
    }
};
Tagged:

Answers

  • edited March 2014

    Processing officially runs under Java rather than JavaScript!
    JS arrays are very complex w/ lotsa methods. Maybe a LinkedList would be the Java's most equivalent:

    http://download.java.net/jdk8/docs/api/java/util/LinkedList.html

    In Java, we gotta declare the data-type of a variable. JS numerical types would translate as float in Java!

    While variables in JS can be assigned any type of data, in Java only compatible types can be assigned to a variable declared as such! :-SS

  • edited March 2014

    but the brackets such as "var b=[];" don't translate at all if I put "int b=[];" in processing.

    In JavaScript, assigning a [] is the simplest way to get an array instance. Although there are other alternatives too.
    But for Java's own native array we need much more than that! :-\"

    1st, we need to know which type of array we want. Then how many we need!
    Let's say we want an array of 10 int values, so we declare it this way:

    int[] b = new int[10];
    

    If we don't know how many we're gonna need, we gotta use a more complex data-structure, like an ArrayList or IntList for example:

    IntList b = new IntList();
    
Sign In or Register to comment.