We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, does anyone know how I can get the computer to understand my bunker x, y variables? I would rather use other.x, other.y. other.r than bunker.x, bunker.y, and bunker.r but either way I'm lost.
var bunkers = [];
var b1;
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
b1 = new Bubble(250, 200);
for (var i = 0; i < 4; i++){
bunkers[i] = new Bunker(i*294 + 247, height-140);
}
}
function draw() {
background(0);
for (var i = 0; i < 4; i++){
bunkers[i].show();
}
b1.update();
b1.display();
if (b1.intersects(bunkers[i])){
bunkers[i].changeColor();
}
}
function Bubble(x, y) {
this.x = x;
this.y = y;
this.r = 48;
this.col = color(255);
this.changeColor = function() {
this.col = color(random(255), random(255), random(255))
}
this.display = function() {
stroke(255);
fill(this.col);
ellipse(this.x, this.y, this.r * 2, this.r * 2);
}
this.intersects = function(bunker) {
var d = dist(this.x, this.y, bunker.x, bunker.y);
if (d < this.r + bunker.r) {
return true;
} else {
return false;
}
}
this.update = function() {
this.x = this.x + random(-5, 5);
this.y = this.y + random(-5, 5);
}
}
function Bunker(x, y) {
this.x = x;
this.y = y;
this.r = 60;
this.show = function(){
push();
translate(this.x-257, this.y-68);
noStroke();
fill(0, 100, 0);
beginShape();
vertex(200, 50);
vertex(208, 50);
vertex(208, 42);
vertex(216, 42);
vertex(216, 34);
vertex(224, 34);
vertex(224, 26);
vertex(290, 26);
vertex(290, 34);
vertex(298, 34);
vertex(298, 42);
vertex(306, 42);
vertex(306, 50);
vertex(314, 50);
vertex(314, 110);
vertex(290, 110);
vertex(290, 102);
vertex(282, 102);
vertex(282, 94);
vertex(274, 94);
vertex(274, 86);
vertex(240, 86);
vertex(240, 94);
vertex(232, 94);
vertex(232, 102);
vertex(224, 102);
vertex(224, 110);
vertex(200, 110);
endShape(CLOSE);
pop();
}
}
Answers
b1 = new Bubble(250, 200);
.
dot operator.if (d < this.r + bunker.r) {
toif (d < this.r + other.r) {
inside Bubble::intersects() method, right?function (bunker) {
tofunction (other) {
.Hi GoTo, this isn't my real code. What I'm having problems with is I can't seem to get any objects to recognize the bunker object(s). I'm trying to get hit detection... but the other objects just roll over the bunkers without hit detection. Could it be something to do with the translate method? Does the computer know where the bunkers are? The error says: undefined is not an object (evaluating 'bunker.x') but bunker is an object isn't it?
if (b1.intersects(bunkers[i])) {
.for ( ; ; ) {}
loop in which i is its variable iterator! :-O"If you rely on any p5's method which changes its canvas' matrix, you're making things much harder to check collisions."
I think I should learn how not to use the translate method. I've noticed that with canvas no one seems to make horses et cetera, not in tutorial videos anyways... Is there some other way for p5 to make designs like the space invader aliens et cetera without using the translate method? I think the pVector maybe?
Thank you,
When not using translate(), coords. (x=0, y=0) is the top-left pixel of the canvas.
Yes, the thing is I'm lost because I don't know how to move the objects around without the translate. I'm totally knew. I'm going to have to try to understand it. Whatever that means...
You move your "sprites" around by changing their x & y properties.
Let's have some base
class
called Actor w/ the most common properties for location, dimension, velocity & color:Now let's have a
class
Alien whichextends
from Actor, adding display(), update() & victory() methods to it:Of course you can have other classes inheriting from the base
class
Actor if that makes sense too.Then implement their own particular methods or other properties in addition to parent Actor's.
BtW, learn more about JS classes at the links below: :bz
You know what I'd love to know? Can I pick this bunker object up and move it around on my canvas? It may be simple for some people but I have no idea on how to do it? Is it simple? I was hoping to make my own designs rather than use sprites.
A more practical approach would be to render all of those vertex() inside a p5.Graphics created via createGraphics() w/ just enough width & height to fit in the whole "sprite": *-:)
Once created, just display it into canvas via image(): http://p5js.org/reference/#/p5/image
Here's a silly space invader made outta rect(), but written in Java Mode:
http://studio.ProcessingTogether.com/sp/pad/export/ro.9Iaf6privOouM/latest
After turning it into a p5.Image or p5.Graphics, you can move it around via image(). :-h