We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Good day,
I'm not experienced with p5, processing or programming in general. So I was really excited when I saw, how easy it is to work with p5.
I made a silly little pong game. In p5 editor it works the way I want, just gets more and more slow after running for some time.
Then there is a possibility to run a sketch in browser (from p5 editor). The browser in my case is chrome. Sketch runs fine, even without slowing down. But as code got longer I noticed that it doesnt always run inside browser even if it works in editor. For example I had to put if statement inside another if statement instead of checking all conditions inside one if statement.
Another possibility is to run a sketch by launching index.html file. Here again I get different results. For example sounds do not want to get pre-loaded. I dont know, maybe it would get solved by properly hosting it somewhere instead running it like that. But this is not biggest issue (I just commented out all the sounds in the code). Even without sounds it gives different results in different browsers:
In windows I tried it on explorer, firefox, chrome and opera. It worked ok on all except firefox.
I also tried it on linux ubuntu, on chromium and firefox. Again, it didnt work on firefox. And on chromium it worked, but very slow.
So this is my story. And the question is: are there any guidelines, what should be avoided and what must be done, to make a code good for all browsers? Or maybe there is a possibility to export a sketch in some kind of exe file that could be sent to other people and I would be sure that it will run on their machines exactly the same as on mine?
Answers
There are several different issues at play here:
In terms of possible solutions/guidelines:
As for packaging your code up as an app. It's an interesting idea and in principle possible thanks to things like NodeJS and the Atom Electron platform; but at present p5js doesn't provide any tools to do this...
Good luck!
Hosting files did solve issue with sound. Here is my silly little game:
http://andrius.5gbfree.com/index2.html
There are bugs in it and it doesnt work on firefox (up and down doesnt work). I dont feel like fixing those bugs- I acheaved my goal with this game (had fun and learned something new).
Thank you blindfish for your comments- I will use those resources when I work on my next p5 project.
true
, all the 4 arrow keys are0
!!! @-)https://GitHub.com/processing/processing/pull/4006
But not all is lost! Rather than having this verbosity
if ()
condition:if (keyIsPressed && keyCode == UP_ARROW) {}
Why not rely on something more simple & succinct like keyIsDown()?:
if (keyIsDown(UP_ARROW)) {}
http://p5js.org/reference/#/p5/keyIsDown
Even keyPressed() is better & safer than keyIsPressed + keyCode:
http://p5js.org/reference/#/p5/keyPressed
GoToLoop, thank you! I updated code according to your suggestion and now it works in firefox too.
Just played it in some Firefox browser and it's working alright! :-bd
Although you've left some
println('pass1');
at some place there and it's littering the console.But I've got an important recommendation about how to implement methods in classes. L-)
Methods declared inside a class' constructor wastes lotsa RAM for each object created from it.
That's why we should move them outside the constructor's function and declare it as its prototype:
As a bonus, a more compact moveNot()'s implementation: O:-)
That's very interesting concept. I've glanced at p5 reference page, but I did'nt see anything about prototypes.
So inside constructor I declare only those functions that will be used with all instances, and if it may be used or not, then I put it outside (as prototype). Otherwise interpreter will read those functions even if they do not need to be executed. Did I understood it correctly?
prototypes are part of the JavaScript language, for which there is plenty of documentation. I'm reading the you don't know JS series at the moment. It is suitable for beginners but goes into a bit more technical detail about what's happening under the hood so is also useful to more experienced coders who don't have a technical background..
Inside constructor we don't declare any methods! All of them are declared externally.
All the rest stays in constructor though. That is, those properties known in OOP as instance variables.
Indeed, they don't utter the word prototype not even once!
Neither in reference, tutorials and examples sections!
What they teach is exactly what you had just done above, throw everything inside constructor:
http://p5js.org/examples/examples/Objects_Objects.php
BtW, just found out this very nice article about most common JS prototype design patterns:
http://www.HTMLGoodies.com/beyond/javascript/some-javascript-object-prototyping-patterns.html
And take a look at what was said there about defining methods inside constructor:
But don't fret! Your game doesn't instantiate classes more than 2 times.
Again that's what is quoted from the article:
So there's no much need to refactor your "SuperHeroPong.js" game classes to define methods externally in prototype in your particular case here.
However, if your game had some classes which many
new
objects would be created from, then they would warrant a prototype refactoring for sure. ;;)