p5.js - Using functions from other files in JavaScript -
i'm making game in js using p5, , came upon problem.
in html file have references .js files:
<!doctype html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.3/p5.min.js"></script> <script src="main.js"></script> <script src="iskeypressed.js"></script> <script src="blocks.js"></script> <script src="player.js"></script> </head> <body> </body> </html>
i have 1 .js file defining function iskeypressed():
function iskeypressed(keyquery) { var did = false; for(var = 0; < keyspressed; i++) { if(keyspressed[i] === keyquery) { did = true; } } return did; }
i reference in object inside player.js:
player.motion = function() { if(iskeypressed('w')) { this.velocity.add(0,-5); } if(iskeypressed('s')) { this.velocity.add(0,5); } if(iskeypressed('a')) { this.velocity.add(-5,0); } if(iskeypressed('d')) { this.velocity.add(5,0); } }
but when try call player.motion, error:
uncaught typeerror: iskeypressed not function
does know why occurring?
you try this
<!doctype html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.3/p5.min.js"></script> <script src="main.js"></script> <script src="iskeypressed.js"></script> <script src="blocks.js"></script> <script src="player.js"></script> <script> player.motion = function() { if(iskeypressed('w')) { this.velocity.add(0,-5); } if(iskeypressed('s')) { this.velocity.add(0,5); } if(iskeypressed('a')) { this.velocity.add(-5,0); } if(iskeypressed('d')) { this.velocity.add(5,0); } } </script> </head> <body> </body> </html>
this importing functions iskeypressed.js
file , therefore able reference in <script>
tag. not able use iskeypressed.js
's functions in player.js
because cannot reference it.
Comments
Post a Comment