We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey, I have some code from a university project, and am trying to implement key based movement. I have got the movement to work up and down along the y-axis, but cannot get it to move left and right along the x-axis, and am unsure why.
My code for the object, and movement is as follows:
`function bomberMan(){
this.x = 100; this.y = 100; this.xpseed = 0; this.yspeed = 0;
this.dir = function(x,y){ this.xspeed = x; this.yspeed = y; }
this.update = function(){ this.x = this.x + this.xpseed; this.y = this.y + this.yspeed; }
this.show = function(){ fill(0); rect(this.x, this.y, 15, 15); }
}
function keyPressed(){ if (keyCode === UP_ARROW){ bomber.dir(0, -1); }
if (keyCode === DOWN_ARROW){ bomber.dir(0,1); }
if (keyCode === LEFT_ARROW){ bomber.dir(-1,0); }
if (keyCode === RIGHT_ARROW){ bomber.dir(1,0); } }`
Any help as to why this doesn't seem to work would be greatly appreciated, thanks so much!
Answers
https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Hey sorry! Here is the code:
@ lines #5
this.xpseed = 0;
& #14this.x = this.x + this.xpseed;
, property name xpseed seems a mistype to me. :-?So it was, thank you very much! Been starting at the code for so long didn't notice
Nothing like another pair of eyes, huh? $-)
BtW, I've got some additional tips... O:-)
Classes should be named using UpperCamelCase.
So bomberMan should be better named as Bomberman. :-bd
And while we're talking about classes, JS got keyword
class
for some time already:https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor
I've just refactored your class Bomberman to use an actual class block below. Give it a try: B-)
@GoToLoop -- very interesting -- this was syntactic sugar added in ECMAScript 2015, is that right? Seems like using
class
would make JS code easier to read....At present the style that is modeled by the p5.js object examples is still to use
function
for classes:...and the p5.js documentation also recommends that
function
style -- explaining it in pre-class
terms, but still valid:Yup! Classes internally become classical prototype-based inheritance pattern:
https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
However, if you consider such as sugar, for fairness' sake, Java's "enhanced foreach" loop is 1 as well.
For example, iterating over a PVector[] vecs array, under its "enhanced" loop below:
Java compiler would internally transpile it as something like this:
http://docs.Oracle.com/javase/8/docs/technotes/guides/language/foreach.html
Similarly, a JS'
class
would get transpiled as 1function
constructor().And its non-
static
methods added to its prototype{} object. ~O)That's why I'm insisting on spreading such news in this forum for about 2 years already! #:-S
Although JS language started being developed by Brendan Eich based mainly on Scheme language, he was asked later by Netscape to turn it into a Java spinoff:
https://en.Wikipedia.org/wiki/JavaScript#Beginnings_at_Netscape
Now at version 6 (ES2015), JS advances on becoming even more similar to Java w/ the introduction of the
class
syntax. \m/It makes the transition from Java, Python, etc. to JS more pleasurable.
Processing's Java Mode sketches become much easier to be converted to p5.js, etc. B-)
It saddens me that p5.js & Khan's Pjs haven't embraced ES6 yet after more than a year available to browser's big names. :-<
They keep on teaching a much older & harder OOP pattern for beginners! X(
It's not enough to teach archaic JS' OOP, they even teach such using a low quality approach! [-(
function
for its Bomberman version.function
itself.new
.class
gets transpiled to ES5 using prototype-based inheritance pattern. $-)function Bomberman(x, y) {}
:http://www.TypeScriptLang.org/play/