d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Processing Js Help > Making A Game
12Next
Add Reply New Topic New Poll
Member
Posts: 16,404
Joined: Mar 28 2009
Gold: 7.69
Dec 2 2016 02:28pm
When the user clicks ready, a random rect will spawn (random x, y, w, h). After the player clicks within the walls of that rect they reach level 2 and 2 rects spawn they must click inside of. Level 3, 3 rects spawn they must click inside and so on. IDK where this code is wrong because no rects are spawning after the player clicks ready.

Code
var colors = (random(0, 255), random(0, 255), random(0, 255));
var levelNumber = 0;
var randomRects = [];
var scene;
var randX = random(20, 370);
var randY = random(20, 370);
var randW = random(5, 45);
var randH = random(5, 45);

//button object
var Button = function(config) {
this.x = config.x;
this.y = config.y;
this.width = width;
this.height = height;
this.label = config.label;
};
Button.prototype.draw = function() {
fill(0, 0, 0);
rect(this.x-5, this.y+64, this.width-127, this.height-340, 5);
fill(252, 18, 29);
textSize(40);
textAlign(CENTER);
text(this.label, this.x+134, this.y+106);
};


//Asks player if they're ready
var btn1 = new Button ({
x: 70,
y: 100,
label: "Click to Play"
}
);

//Asks player if they want to play again
var btn2 = new Button ({
x: 111,
y: 100,
label: "Play Again?"
}
);


//generate increasing number of rectangles
var randRect = function () {
return {
x: randX,
y: randY,
width: randW,
height: randH,
color: colors,
clicked: false,
};
};
//+1 rectangle spawns after each level
var levelUp = function () {
levelNumber++;
for (var i = 0; i < levelNumber; i++) {
randomRects.push(randRect);
}
};
//draw the rectangles
var drawRects = function () {
for (var i = 0; i < randomRects.length; i++) {
var r = randomRects[i];
stroke (0, 0, 0);
fill (r.color);
rect (r.x, r.y, r.width, r.height);
}
};

//scene functions
var scene1 = function() { // 'Click to Play' button
scene = 1;
background(255, 255, 255);
btn1.draw();
};
var scene2 = function() { //bulk of game takes place here
scene = 2;
background(255, 255, 255);
drawRects();


};
var scene3 = function() { // Play Again button
scene = 3;
background(255, 255, 255);
btn2.draw();
};
scene1();


//Reserving // not sure what to put inside, if anything at all
draw = function() {

};


mousePressed = function() {
//scene change
if (scene === 1 && mouseX >= 135 && mouseX <= 248 && mouseY <= 263 && mouseY >= 150) {

scene2();
}
else if (scene === 3 && mouseX >= 135 && mouseX <= 248 && mouseY <= 263 && mouseY >= 150) {
scene2();
}

//Clicking inside the random rects
else if (scene === 2 && mouseX >= randX && mouseX <= randX+randW && mouseY >= randY && mouseY <= randY+randH ) {

text("HI", 50, 50); //test


}

};
Member
Posts: 7,190
Joined: Jul 10 2008
Gold: 14,001.56
Dec 3 2016 06:57am
msged

This post was edited by netwarrior on Dec 3 2016 06:57am
Member
Posts: 16,404
Joined: Mar 28 2009
Gold: 7.69
Dec 4 2016 12:16pm
Still looking for help
Member
Posts: 16,404
Joined: Mar 28 2009
Gold: 7.69
Dec 4 2016 09:25pm
Willing to throw a generous fg donation your way for a detailed explanation
Member
Posts: 735
Joined: Jan 23 2007
Gold: 725.94
Dec 5 2016 08:53pm
I made a Plunker to try and help you: https://plnkr.co/edit/hnM6y81UsPPraZ35epe0

Is there more code in your HTML file or in another script? Right off the bat I get an error:

Code
Uncaught ReferenceError: random is not defined
at https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:1:15


So, I think that should be Math.random() and not random(). After that change:

Code
Uncaught ReferenceError: width is not defined
at new Button (https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:14:18)
at https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:29:12


So, it looks like that should probably be config.width. After that change:

Code
Uncaught ReferenceError: background is not defined
at scene1 (https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:76:5)
at https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:91:1


And that's where I stop following your code. Where is the function "background()" defined? Are you using an HTML5 canvas? It looks like you may be, but the function calls aren't right. Or, maybe you're using some library that defines these functions, but I'd need that in order to tell you what's wrong.
Member
Posts: 16,404
Joined: Mar 28 2009
Gold: 7.69
Dec 5 2016 09:11pm
Quote (garrettthegreat @ Dec 5 2016 09:53pm)
I made a Plunker to try and help you: https://plnkr.co/edit/hnM6y81UsPPraZ35epe0

Is there more code in your HTML file or in another script? Right off the bat I get an error:

Code
Uncaught ReferenceError: random is not defined
at https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:1:15


So, I think that should be Math.random() and not random(). After that change:

Code
Uncaught ReferenceError: width is not defined
at new Button (https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:14:18)
at https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:29:12


So, it looks like that should probably be config.width. After that change:

Code
Uncaught ReferenceError: background is not defined
at scene1 (https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:76:5)
at https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:91:1


And that's where I stop following your code. Where is the function "background()" defined? Are you using an HTML5 canvas? It looks like you may be, but the function calls aren't right. Or, maybe you're using some library that defines these functions, but I'd need that in order to tell you what's wrong.



It's being done on khanacademy and I shouldnt have to pull it off of KA because it's for school.
Member
Posts: 735
Joined: Jan 23 2007
Gold: 725.94
Dec 5 2016 09:39pm
Quote (Shakti @ Dec 5 2016 09:11pm)
It's being done on khanacademy and I shouldnt have to pull it off of KA because it's for school.


What can I do to help? If it's a KA only exercise that only works if you're a KA student taking this particular class, I'm not going to be able to do much other than to ask you to look a the developer console and forward any error messages on to me.

What you gave me so far doesn't work because none of these variables and functions are defined. Unless you can give me a definition, we'll have to go through console messages only.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 5 2016 09:42pm
Quote (garrettthegreat @ Dec 5 2016 09:53pm)
I made a Plunker to try and help you: https://plnkr.co/edit/hnM6y81UsPPraZ35epe0

Is there more code in your HTML file or in another script? Right off the bat I get an error:

Code
Uncaught ReferenceError: random is not defined
at https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:1:15


So, I think that should be Math.random() and not random(). After that change:

Code
Uncaught ReferenceError: width is not defined
at new Button (https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:14:18)
at https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:29:12


So, it looks like that should probably be config.width. After that change:

Code
Uncaught ReferenceError: background is not defined
at scene1 (https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:76:5)
at https://run.plnkr.co/blr0T7r9TqTpaaLD/script.js:91:1


And that's where I stop following your code. Where is the function "background()" defined? Are you using an HTML5 canvas? It looks like you may be, but the function calls aren't right. Or, maybe you're using some library that defines these functions, but I'd need that in order to tell you what's wrong.


i think he's getting those from processingjs

http://processingjs.org/reference/background_/
http://processingjs.org/reference/random_/
Member
Posts: 735
Joined: Jan 23 2007
Gold: 725.94
Dec 5 2016 10:02pm
Quote (carteblanche @ Dec 5 2016 09:42pm)


Hm. I could see why someone who actually read the topic description would know that and why I wouldn't :wallbash:

Looks like a lot of research on my end in order to learn the ins and outs of processingjs. Maybe there's a forum that's better for this stuff since it's pretty specialized.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 5 2016 10:24pm
Quote (garrettthegreat @ Dec 5 2016 11:02pm)
Hm. I could see why someone who actually read the topic description would know that and why I wouldn't :wallbash:

Looks like a lot of research on my end in order to learn the ins and outs of processingjs. Maybe there's a forum that's better for this stuff since it's pretty specialized.


from looking at his numerous topics, very few of his problems has to do with processingjs-specific knowledge. just general coding knowledge is usually sufficient to help the poor guy out. i've just lost interest in his lack of desire to trouble shoot, so i gave up holding his hand.

Quote
IDK where this code is wrong because no rects are spawning after the player clicks ready.

offhand, i have a few guesses.

where's your code for btn1 that executes when you click it?

Code
var drawRects = function () {
for (var i = 0; i < randomRects.length; i++) {

soo....randomRects.length is always 0, so drawRects doesn't really do much. the only time you increase the length is when you call levelUp. which, by the way, you never call.

i recommend you either add logging or debug to see what code is getting executing "after the player clicks ready". it will answer many of your questions.

This post was edited by carteblanche on Dec 5 2016 10:27pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll