- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 javascript 和 nodejs 的新手,我正在使用这个项目来发展我的技能和学习新技术。目前我的项目使用多个相互依赖的类。类文件位于不同的目录中,我目前正在尝试使用 export 和 require 语句来允许在其他文件中引用类。我还使用 browserify 和 watchify 将所有文件捆绑在一起,以简化 html 脚本标签。
这是项目布局(JavaScript 文件夹)
-Base (Folder)
--Game.js (Class)
--Player.js (Class)
--Ball.js (Class)
-Helpers (Folder)
--Draw.js (Class)
--Helper.js (Class)
-GameType (Folder)
--Game1.js (Class)
相互依赖的类如下。
Game is Dependent on
--Draw.js
--Helper.js
Player and Ball are Dependent on
--Helper.js
Game1
--Game.js
--Ball.js
--Player.js
该项目在 app.js 文件中加载,并且需要 game1 文件。目前我正在尝试测试并使所有 require 和 export 语句正常工作。目前在 app.js 文件中,我可以创建一个 game1 对象,但我无法访问其中的变量和方法。我可以控制台记录该对象,它具有所有正确的变量和方法,因此它正确地创建了 game1 对象,但我无法访问它的任何部分。我不确定我目前是否错误地使用了 ES6 类表示法的 require 和 export 语句,或者我的代码是否存在其他问题。我是否正确使用了 require 和 export 语句?
以下是类和 app.js 文件的片段。一些方法在几个类中需要完成,但我正在尝试添加要求和导出功能以使我的代码导航更好。我希望你能帮助我找到问题的解决方案。
应用程序.js
const Game1 = require('./GameType/game1.js');
window.onload = function(){
console.log("Test Started");
console.log();
var canvasLocator = document.querySelector("#gameCanvas");
var canvasContext = canvasLocator.getContext("2d");
var game1 = new Game1();
//Prints out the correct object in the console
console.log(game1);
game1.draw();
//Returns an empty array of objects
//In my test is should return a draw obj
}
游戏1.js
const Game = require('../Base/Game.js');
const Ball = require('../Base/Ball.js');
const Player = require('../Base/Player.js');
class Game1 extends Game{
constructor(){
super();
this.ball = new Ball(400, 300);
this.player1 = new Player("User", 15, 275, "blue");
this.player2 = new Player("AI", 775, 275, "blue");
this.mouseYPos;
}
refresh(){
//Needs to be implemented
}
draw(){
console.log("Super Draw: " + this.ball);
}
moveEverything(){
//Needs to be implemented
}
}
module.exports = Pong;
Game.js 作为一个接口(interface),也有所有游戏所必需的变量
'use strict';
const Helper = require('../Helpers/Helper.js');
const Draw = require('../Helpers/Draw.js');
class Game{
constructor(){
this.interval;
this.started = false;
this.framesPerSecond = 30;
this.drawObj = new Draw();
this.helper = new Helper();
}
//Each game needs to implement
draw(){
console.log("draw() not implemented in child class!");
}
moveEverything(){
console.log("moveEverything() not implemented in child class!");
}
refresh(){
console.log("refresh() not implemented in child class!");
}
};
module.exports = Game
球
const Helper = require('../Helpers/Helper.js')
class Ball{
constructor(x, y){
this.ballX = x;
this.ballY = y;
this.ballSpeedX;
this.ballSpeedY;
this.ballSpeedXChange;
this.ballSpeedYChange;
this.helper = new Helper();
}
move(x,y){
this.ballX = this.ballX + x;
this.ballY = this.ballY + y;
}
increaseSpeed(speedX, speedY){
this.ballSpeedX = this.ballSpeedX + speedX;
this.ballSpeedY = this.ballSpeedY + speedY;
}
reflectBall(player, drawObj){
}
reflect(ptOfContact, paddleSpeed){
}
setBallDifficulty(difficulty){
switch(difficulty){
case "Easy":
this.ballSpeedXChange = -1;
this.ballSpeedYChange = 1;
break;
case "Medium":
this.ballSpeedXChange = -1.5;
this.ballSpeedYChange = 1.5;
break;
case "Hard":
this.ballSpeedXChange = -2;
this.ballSpeedYChange = 2;
break;
default:
console.log("No difficulty Found");
}
}
}
module.exports = Ball
播放器
const Helper = require('../Helpers/Helper.js');
class Player{
constructor(input, x, y, color){
//Boolean value for AI or Actual Player
this.inputType = this.inputType(input);
this.playerX = x;
this.playerY = y;
this.playerSpeed;
this.playerScore = 0;
this.paddleWidth = 10;
this.paddleHeight = 50;
this.color = color;
this.helper = new Helper();
}
move(drawObj){
//True: User Input
//False: AI Input
if(this.inputType){
this.movePlayerInYDir(drawObj);
}
else{
this.movePlayerAIInYDir(drawObj);
}
}
movePlayerInYDir(drawObj){
let before = this.playerY;
this.playerY = this.helper.playerInput(drawObj);
this.playerSpeed = this.playerY - before;
if((this.playerY + this.paddleHeight) >= (drawObj.getBaseHeight())){
this.playerY = (drawObj.getBaseHeight() - this.paddleHeight);
}
}
movePlayerAIInYDir(drawObj){
this.playerSpeed = this.setBotDifficulty("Easy");
this.playerY = this.playerY + this.playerSpeed;
if(this.playe2Y <= 0){
//Hits Top
this.playerSpeed = (this.playerSpeed) * -1;
}
else if((this.playerY + this.paddleHeight) >= drawObj.getBaseHeight()){
//Hits Bottom
this.playerSpeed = (this.playerSpeed) * -1;
}
}
setAIDifficulty(difficulty){
switch(difficulty){
case "Easy":
//TODO
break;
case "Medium":
//TODO
break;
case "Hard":
//TODO
break;
case "Practice":
//Unbeatable Mode
break;
default:
console.log("No difficulty Found");
}
}
//Helper
inputType(type){
//True: Real Input
//False: AI
switch(type){
case "User":
return true;
case "AI":
return false;
default:
console.log("Type not recognized");
}
}
}
module.exports = Player
助手
class Helper{
constructor(){
this.gameType;
this.canvasLocator = document.querySelector("#gameCanvas");
this.canvasContext = this.canvasLocator.getContext("2d");
this.mouseXPos;
this.mouseYPos;
}
getMouseYPos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return (evt.clientY - rect.top);
}
playerInput(drawObj){
let c = this.canvasLocator;
let helper = this;
//let game = this;
//let mouseYPos;
//console.log(game);
c.addEventListener("mousemove", function(evt){
helper.mouseYPos = helper.getMouseYPos(c, evt);
}
, false);
//console.log(game.mouseYPos);
return helper.mouseYPos;
}
change(speed){
//Ball Function for reflection
if(speed > 8 || speed < -8){
return 2;
}
return (Math.abs(1/8 * speed) + 1);
}
randomIntNumber(min, max){
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
randomSpeed(){
if(this.randomIntNumber(0, 100) % 2 == 0){
return this.randomIntNumber(-7, -9);
}
else{
return this.randomIntNumber(7, 9);
}
}
randomNumber(min, max){
return (Math.random() * (max - min)) + min;
}
}
module.exports = Helper
谢谢你的帮助。
最佳答案
对于初学者,您询问了如何在 ES6 中导入和导出。在我解决这个问题之前,重要的是要注意 ES6 模块语法与 Node.JS 导入模块的方式不同。您的示例使用 Node.JS Common.js 样式的模块加载。
在 ES6 中,你可以像这样导入模块......
import jquery from 'jquery';
或者如果你正在加载 node_modules 文件夹之外的东西......
import myClass from './path/to/where/the/file/is';
我刚刚提供的两个示例是如何将整个文件加载为依赖项。
现在,如果您只想导入一个函数供使用,您也可以使用 ES6 来实现。
test.js
const myFunction = () => { alert("hello world") };
export { myFunction }
现在您可以像这样只导入 myFunction...
import { myFunction } from './path/to/test.js';
综上所述,您需要记住的一件事是 native 浏览器尚不具备导入 JavaScript 模块的能力。所以为了解决这个问题,我们需要使用像 Webpack 这样的东西来提供使用 ES6 导入和导出模块的能力。
关于javascript - 如何导入和导出 javascript ES6 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45764743/
在 settings.py LANGUAGE_CODE = 'es-mx'或 LANGUAGE_CODE = 'es-ar'不起作用,但是 LANGUAGE_CODE = 'es'或 LANGUAGE
我想知道OpenGL ES 2.0和OpenGL ES 3.0之间有什么区别。 OpenGL ES 3.0的主要优点是什么? 最佳答案 总体而言,这些变化通过更大的缓冲区、更多的格式、更多的统一等提高
这是我为此端点使用 Postman localhost:9201/response_v2_862875ee3a88a6d09c95bdbda029ce2b/_search 的请求正文 { "_sour
OpenGL ES 2.0 没有 ES 1.0 那样的 GL_POINT_SMOOTH 定义。这意味着我用来绘制圆圈的代码不再有效: glEnable(GL_POINT_SMOOTH); glPoin
我尝试编译这个着色器: varying vec2 TexCoords; varying vec4 color; uniform sampler2D text; uniform vec3 textCol
我是 OpenGL ES 的新手,我使用的是 OpenGL ES 2.0 版本。我可以在片段着色器中使用按位操作(右移、左移)吗? 最佳答案 OpenGL ES 2.0 没有按位运算符。 ES 3.0
有没有办法只用线画一个三角形? 我认为GL_TRIANGLES选项可使三角形充满颜色。 最佳答案 使用glPolygonMode(face, model)设置填充模式: glPolygonMode(G
我想用一个包含 yuv 数据的采样器在 opengl es 着色器中将 yuv 转换为 rgb。我的代码如下: 1)我将 yuv 数据发送到纹理: GLES20.glTexImage2D(GLES20
我正在使用这样的域: http://www.domain.com/es/blabla.html 我想更改 .es 的/es 部分并将 URLS 转换为类似以下内容: http://www.domain
有谁知道OpenGL ES是否支持GL_TEXTURE_RECTANGLE?我计划将它用于 2D 图形以支持非二次幂图像。我当前的实现使用 alpha=0 填充的 POT 纹理,对于拉伸(stretc
我需要在具有 PowerVR SGX 硬件的 ARM 设备上实现离屏纹理渲染。 一切都完成了(使用了像素缓冲区和 OpenGL ES 2.0 API)。唯一 Unresolved 问题是速度很慢glR
这是一个奇怪的事情。我有一个片段着色器,据我所知只能返回黑色或红色,但它将像素渲染为白色。如果我删除一根特定的线,它会返回我期望的颜色。它适用于 WebGL,但不适用于 Raspberry Pi 上的
我正在考虑将一些 OpenGL 代码移植到 OpenGL ES 并且想知道这段代码到底做了什么: glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) 因为 g
我正在考虑将一些 OpenGL 代码移植到 OpenGL ES 并且想知道这段代码到底做了什么: glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) 因为 g
GLSL ES最多可以编译多少个程序?所以假设我创建了 100 个片段着色器,每个都有不同的效果。所以在运行时我编译所有这些并动态地我用 glUseProgram 交换它们。我假设每次我编译一个新的
我正在尝试使用顶点缓冲区对象来绘制圆,并在 iPhone 上的 OpenGL ES 2.0 中启用 GL_POINT_SMOOTH 来绘制点。 我使用以下 ES 1.0 渲染代码在 iPhone 4
为什么在 OpenGL ES 1.x 中缩放(均匀)对象会导致对象变轻? 更有意义的是它会更暗,因为法线被缩小是否也会使对象更暗?但由于某种原因,物体变轻了。当我放大时,对象变得更暗。在我看来,这应该
我正在尝试通过移植 some code 在 iOS 上的 OpenGL ES 2.0 中获得一些阴影效果。来自标准 GL。部分示例涉及将深度缓冲区复制到纹理: glBindTexture(GL_TEX
所以我正在使用 2D 骨骼动画系统。 有 X 个骨骼,每个骨骼至少有 1 个部分(一个四边形,两个三角形)。平均而言,我可能有 20 块骨头和 30 个部分。大多数骨骼都依赖于父骨骼,骨骼会每帧移动。
我在使用 ES 着色器时遇到了一些晦涩难懂的问题,而且我现在几乎没有想法了。 这是一些代码: .. precision mediump float; .. #define STEP (1f/6f) 5
我是一名优秀的程序员,十分优秀!