将我的Jquery项目更改为纯javascript
Changing my Jquery project to plain javascript
我使用jquery和一个有用的在线教程为一个学校项目创建了一个breakout游戏。
工作小提琴在这里:http://jsfiddle.net/Kinetic915/kURvf/
编辑修改小提琴:http://jsfiddle.net/Kinetic915/nVctR/
我已经改变了大部分的javascript,但我有问题改变的jquery代码,使球javascript。
我在有问题的地方做了标记并留了空格。
非常感谢您给予的任何帮助!
//***********************************************************************************
// START CODE
//***********************************************************************************
// VARIABLES and other initializing functions are here
function start() {
//******************************************************************************
//JQUERY
// HERE IS THE MAIN PROBLEM!!!!!!
// HERE IS THE MAIN PROBLEM!!!!!!
Cir = $('#canvas')[0].getContext("2d");
//JQUERY
//changing to Cir = canvas.getContext("2d"); causes the code to FAIL.
return setInterval(drawCIRCLE, 10);
}
function windowsize() {
//success with javascript
WIDTH.width = window.innerWidth;
HEIGHT.height = window.innerHeight;
WIDTH = window.innerWidth;
HEIGHT = window.innerHeight;
//Previous JQUERY:
// WIDTH = $("#canvas")[0].width = $(window).width();
// HEIGHT = $("#canvas")[0].height = $(window).height();
}
windowsize();
var x = WIDTH / 2 - 30; //divide by 2 start in middle of window
var y = HEIGHT / 2;
//THIS DRAWS THE CIRCLE
function circle() {
//Cir.clearRect(0, 0, WIDTH, HEIGHT);
Cir.beginPath();
Cir.arc(x, y, 10, 0, Math.PI * 2, true);
Cir.closePath();
Cir.fill();
}
// Initialization of the Block array, rendering of the gutter area and coordinate box
were here
//*********************************************************
// HERE IS THE CODE THAT RENDERS THE BALL MOVEMENT ETC.
//draw a circle
function drawCIRCLE() {
clear();
circle();
drawPADDLE(); //calls draw paddle function
drawGUTTER(); // calls draw gutter function
drawCOORBOX(); // calls draw coordinate box function
drawBRICKS(); //calls the function to draw the boxes
//have we hit a brick?
rowheight = brickheight + padding;
colwidth = brickwidth + padding;
row = Math.floor(y / rowheight);
col = Math.floor(x / colwidth);
//if so, reverse the ball and mark the brick as broken
if (y < numrows * rowheight && row >= 0 && col >= 0 && bricks[row][col] == 1) {
dy = -dy;
bricks[row][col] = 0;
}
if (x + dx > WIDTH || x + dx < 0) dx = -dx;
if (y + dy < 0) dy = -dy;
else if (y + dy > ((HEIGHT - paddleh) - ppoffset) || y + dy > HEIGHT) {
if (x > paddlex && x < paddlex + paddlew)
//switch! once first is true, then second goes
dy = -dy;
else if (y + dy > ((HEIGHT - paddleh) - ppoffset) && y + dy > HEIGHT) {
clearInterval(intervalId);
}
}
x += dx;
y += dy;
if (rightpress) paddlex += 5;
else if (leftpress) paddlex -= 5;
}
function clear() {
Cir.clearRect(0, 0, WIDTH, HEIGHT);
//Is this jquery? I suspect this part of the code making the circle rendering fail.
}
start();
init_paddle();
initbricks();
几年前我在这里用纯JavaScript写了一个类似的代码这段代码使用纯javascript,没有库。代码注释得很好(我认为:))
我通常附加这样的事件
document.onkeydown = function(e)
{
e = e || window.event;
switch (e.keyCode) { // which key was pressed?
case 32: // release ball.
if(!game.ball.isFree)
{
game.ball.isFree = true;
game.ball.directionX = game.ball.directionY = 1;
game.ball.x = game.ball.offsetLeft;
game.ball.y = game.screen.offsetHeight - game.ball.offsetTop;
}
break;
case 37: // left, rotate player left
game.bar.direction = -1;
break;
case 39: // right, rotate player right
game.bar.direction = 1;
break;
}
}
document.onkeyup = function(e)
{
e = e || window.event;
switch (e.keyCode)
{
case 37:
case 39:
game.bar.direction = 0;
break;
}
}
},
当然,你还有很多其他地方可能需要移植,所以一个有用的问题分解将更容易回答:)
希望对您有所帮助
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- 我可以'我似乎不知道如何修复javascript中的两个lint.有人能帮我理解吗
- 我的javascript for循环不起作用
- 我的django模板布尔变量是't在javascript中按预期工作
- 如何将我的javascript库公开给其他客户端使用
- JavaScript:我的声音只播放一次
- Javascript:我的setTimeout函数由于第三方而不起作用
- Javascript:我的数组的大小随着每次for循环迭代而减小.为什么
- JavaScript 我的函数在第二次被点击时将无法工作
- Javascript:我的画布什么也没画
- 可以't将字符串转换为数字javascript我的代码出了什么问题
- Javascript:我的fbAsyncInit()方法从未被调用
- jQuery/JavaScript我的脚本中的活动链接-下划线不正确
- Javascript:我的Javascript代码不工作?
- Hangman Javascript -我的代码似乎没有更新的单词占位符,当一个字母猜对了
- 我正在学习JavaScript.我的测试代码有什么问题?
- jQuery/JavaScript:我的递归setTimeout函数在选项卡失效时加速
- Javascript-我的setTimeOut在while循环中没有'不起作用
- Javascript我的函数有什么问题