Montana-Media-Arts / 120_CreativeCoding

Main Code Repo for MART 120. This contains lecture code examples, the HW Wiki, and HW Code Examples.
https://montana-media-arts.github.io/creative-coding-1/
MIT License
131 stars 13 forks source link

object not showing up #202

Closed erinflint closed 5 years ago

erinflint commented 5 years ago

Ok so after restarting this project twice I have a handle on it finally. BUT, in true final project fashion, there is an error that I cannot seem to find for the life of me. I am doing a remake of the classic Snake game and for some reason my snake object and my food object will not show up. I have posted both codes below so if anyone could take a look through them and let me know if they find anything funky that would be super helpful! https://github.com/erinflint/120-Work/blob/master/FINAL%20PROJECT%20CC/empty-example/snake.js https://github.com/erinflint/120-Work/blob/master/FINAL%20PROJECT%20CC/empty-example/snakeobject.js

wattse13 commented 5 years ago

Hey @erinflint, neither of those links worked. They both brought me to an empty page

erinflint commented 5 years ago

Welp then I guess that’s my first problem! I’ll get some working ones here soon. Le ven. 7 déc. 2018 à 11:52 AM, wattse13 notifications@github.com a écrit :

Hey @erinflint https://github.com/erinflint, neither of those links worked. They both brought me to an empty page

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Montana-Media-Arts/120_CreativeCoding/issues/202#issuecomment-445329380, or mute the thread https://github.com/notifications/unsubscribe-auth/AoxytVu_bJVIBVhmDSZLMMYwN818Prrbks5u2rj2gaJpZM4ZHyqt .

erinflint commented 5 years ago

@wattse13 Ok so my link isn't working at all now, so here's my code. (two separate codes fyi)

CODE 1:

function snake() { this.x = 0; this.y = 0; this.xspeed = 1; this.yspeed = 0; this.total = 0; this.tail = [];

this.eat = function(pos) { var d = dist(this.x, this.y, pos.x, pos.y); if (d < 1) { this.total++; return true; } else { return false; } }

this.dir = function(x, y) { this.xspeed = x; this.yspeed = y; }

this.die = function() { for (var i = 0; i < this.tail.length; i++) { var pos = this.tail[i]; var d = dist(this.x, this.y, pos.x, pos.y); if (d < 1) { console.log('starting over'); this.total = 0; this.tail = []; } } }

this.update = function() { for (var i = 0; i < this.tail.length - 1; i++) { this.tail[i] = this.tail[i + 1]; } if (this.total >= 1) { this.tail[this.total - 1] = createVector(this.x, this.y); }

this.x = this.x + this.xspeed * scl;
this.y = this.y + this.yspeed * scl;

this.x = constrain(this.x, 0, width - scl);
this.y = constrain(this.y, 0, height - scl);

}

this.show = function() { fill('green'); for (var i = 0; i < this.tail.length; i++) { rect(this.tail[i].x, this.tail[i].y, scl, scl); } rect(this.x, this.y, scl, scl);

} }

CODE 2:

function snake() { this.x = 0; this.y = 0; this.xspeed = 1; this.yspeed = 0; this.total = 0; this.tail = [];

this.eat = function(pos) { var d = dist(this.x, this.y, pos.x, pos.y); if (d < 1) { this.total++; return true; } else { return false; } }

this.dir = function(x, y) { this.xspeed = x; this.yspeed = y; }

this.die = function() { for (var i = 0; i < this.tail.length; i++) { var pos = this.tail[i]; var d = dist(this.x, this.y, pos.x, pos.y); if (d < 1) { console.log('starting over'); this.total = 0; this.tail = []; } } }

this.update = function() { for (var i = 0; i < this.tail.length - 1; i++) { this.tail[i] = this.tail[i + 1]; } if (this.total >= 1) { this.tail[this.total - 1] = createVector(this.x, this.y); }

this.x = this.x + this.xspeed * scl;
this.y = this.y + this.yspeed * scl;

this.x = constrain(this.x, 0, width - scl);
this.y = constrain(this.y, 0, height - scl);

}

this.show = function() { fill('green'); for (var i = 0; i < this.tail.length; i++) { rect(this.tail[i].x, this.tail[i].y, scl, scl); } rect(this.x, this.y, scl, scl);

} }

wattse13 commented 5 years ago

Hmmm, the first thing that I notice is your use of this. notation inside of functions that you are defining. There is a good chance that this is a legitimate way to code, but I have only ever used this. notation when creating classes.

When I looked through your repo, on line 8 of your snake.js file I saw the line:

snake = new Snake();

If I understand the lessons correctly then this line is trying to create a new object based on a Snake class. However, neither of the above codes have any classes in them. Instead, they have functions. So, instead of writing:

function snake() {}

it might be better to write:

` class Snake {

 constructor( x, y ) {

      this.x  = x;

      this.y = y;

      etc.

 }

} `

Line 8 would then also need two arguments: snake = new Snake( example argument, example argument)

Does that help at all? Again, your use of this. notation inside of custom functions may just be a style of coding that I am unfamiliar with.

Edit: I'm not sure why 'class Snake{` and the last '}' are not being included in the code block format...