Can't input a value to a JS variable - javascript

I started learning JavaScript today, and to test it I made a page that changes the image shown when I click on a div. Everything was working fine, but I wanted to know how I could add an input element to specify how many steps I want to jump every time I click the button. I came up with an idea, however it isn't working. Seems like the variable is kind of "accumulating" values when I console.log it. I will link the codes, first one works and the second one doesn't. The first ones uses fixed values, and the square changes colors as it should be doing (the images don't really matter). The second one should jump steps: typing 1 would make it the same as the first one, typing 2 would make it jump one color.
https://jsfiddle.net/u2s3pqxL/1/
window.onload = function(){
var x = 0;
document.getElementById("teste").onclick = function(){myFunction()};
function myFunction(){
if (x == 0){
document.getElementById("teste").style.backgroundColor = "red";
document.getElementById("taiga").style.opacity = 1;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 0;
x += document.getElementById("number").value;
} else if (x == 1){
document.getElementById("teste").style.backgroundColor = "blue";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 1;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 0;
x += document.getElementById("number").value;
} else if (x == 2){
document.getElementById("teste").style.backgroundColor = "yellow";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 1;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 0;
x += document.getElementById("number").value;
} else if (x == 3){
document.getElementById("teste").style.backgroundColor = "purple";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 1;
x += document.getElementById("number").value;
} else if (x == 4){
document.getElementById("teste").style.backgroundColor = "black";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 1;
document.getElementById("moge-ko").style.opacity = 0;
x = 0;
}
console.log(x);
};
};
https://jsfiddle.net/c3s1opb7/7/
window.onload = function(){
var x = 0;
document.getElementById("teste").onclick = function(){myFunction()};
function myFunction(){
if (x == 0){
document.getElementById("teste").style.backgroundColor = "red";
document.getElementById("taiga").style.opacity = 1;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 0;
x += 1;
} else if (x == 1){
document.getElementById("teste").style.backgroundColor = "blue";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 1;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 0;
x += 1;
} else if (x == 2){
document.getElementById("teste").style.backgroundColor = "yellow";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 1;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 0;
x += 1;
} else if (x == 3){
document.getElementById("teste").style.backgroundColor = "purple";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 1;
x += 1;
} else if (x >= 4){
document.getElementById("teste").style.backgroundColor = "black";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 1;
document.getElementById("moge-ko").style.opacity = 0;
x = 0;
}
console.log(x);
};
};
What should I do to make the second one work?

In your code, you have:
x += document.getElementById("number").value;
with this, you asked in your var "x" to add the value of your input.. with type String...
For example, you have the value "1" in the input and 0 in your varaible "x"..
If you follow your code, the result after this expression:
x += document.getElementById("number").value;
Your result will be: 01
and you, you want: 1
because you add a type String in your variable type Integer..
You must parse the varaible in Integer with this parseInt():
x += parseInt(document.getElementById("number").value);

Related

How can I make this collision detection possible?

I'm using a number of boxes that are floating around the screen, I want for them to bounce away from each other when they collide and I am testing this by just changing their colours instead when they collide, for some reason their colour changes at the start(their starting colour is white and if they collide it should change to red but they start and stays red). Is there something I am doing wrong? I would also like for everything to be done within the constructor function. I am sorry for not commenting my code as yet.
function BoxSplash()
{
this.sample = [];
var test = false;
this.col = color(129)
this.changeColour = function()
{
this.col = color(random(0,255),random(0,128),random(0,255))
}
this.intersect = function(other)
{
for(var i = 0; i < numberss; i++)
{ var currentBox = this.sample[i]
var d = dist(currentBox.x,currentBox.y,other.x,other.y)
if(d < currentBox.width + other.width)
{
return true;
}
else
{
return false
}
}
noLoop()
}
this.draw = function()
{
stroke(255)
line(0,windowHeight/2,windowWidth,windowHeight/2)
stroke(0)
for(var i = 0; i < numberss; i++)
{
var box = {
x:random(0,windowWidth - 50),
y:random(windowHeight/2 ,windowHeight - 50),
width:50,
height:50,
speedX:random(1,5),
speedY:random(1,5)
}
this.sample.push(box)
}
//use numberss variable to work with gui
for(var i = 0; i < numberss; i++)
{
fill(this.col)
rect(this.sample[i].x,this.sample[i].y,50,50)
}
this.move()
}
this.move = function()
{
for(var i = 0; i < numberss; i++)
{
var shape = this.sample[i]
shape.x += shape.speedX
shape.y += shape.speedY
if(shape.x + shape.width >= windowWidth || shape.x <= 0 )
{
shape.speedX = -shape.speedX
}
if(shape.y + shape.height >= windowHeight || shape.y <= windowHeight/2)
{
shape.speedY = -shape.speedY;
}
for(var j = 0; j < numberss; j++)
{
var others = this.sample[j]
var d = dist(shape.x,shape.y,others.x,others.y)
if( i != j && d < shape.width + others.width)
{
test = true;
}
else
{
test = false
}
if(test)
{
this.col = color(255,0,0)
}
}
}
}
}
You should visit this link and transform their circles into your boxes.
1st: Calculate dist(object1.x, object.y, object2.x, object2.y) and save into a variable called d
2nd:
if (d < object1.h + object2.h) {
// Enter your code here
}

Trying to do Luhns Algorithm in Javascript but my programming keeps looping indefinitely

As the title says I am trying to write luhns algorithm in Javascript and have it print out a valid card number and what type of card it is whether that be American Express, Mastercard, or Visa.
When ran all it does is continuously loop the alert box. What I want it to print is this is a valid card type and what brand of card it is in the Alert Box.
function checkCreditCard() {
var ccnum = document.getElementById("cardnum").value;
var cardArray = [cardnum]
var temp = ccnum;
var checkerArray;
if (cardType(ccnum) === "AmericanExpress") {
checkerArray = [15];
for (x = 0; x < 15; x++) {
checkerArray[x] = ccnum % 10;
ccnum = ccnum / 10;
}
} else {
checkerArray = [16];
for (x = 0; x < 16; x++) {
checkerArray[x] = ccnum % 10;
ccnum = ccnum / 10;
if (ccnum == 0) {
checkerArray[15] = -1;
checkerArray[14] = -1;
checkerArray[13] = -1;
}
}
}
var summing;
for (x = 1; x < checkerArray.length; x = x + 2) {
if (checkerArray[x] < 0) {
return;
}
checkerArray[x] = checkerArray[x] * 2;
if (checkerArray[x] >= 10) {
summing = summing + checkerArray[x] % 10 + checkerArray[x] / 10
} else {
summing = summing + checkerArray[x];
}
}
for (x = 0; x < checkerArray.length; x = x + 2) {
if (checkerArray[x] < 0) {
return;
}
summing = summing + checkerArray[x];
if (summing == 20) {
alert("This Card is Legit")
} else {
alert("This Card is Invalid")
}
}
function cardType(ccnum) {
var x = {
Visa: /^4[0-9]{12}(?:[0-9]{3})?$/,
Mastercard: /^5[1-5][0-9]{14}$/,
AmericanExpress: /^3[47][0-9]{13}$/,
}
for (var l in x) {
if (x[l].test(ccnum)) {
return l;
}
}
return null;
}
}
<input type="text" id="cardnum" onkeyup="checkCreditCard()" />
<span id="valid">Enter a Number and Press Enter</span>
Let's use let
Also let's move the alert OUTSIDE the loop
function cardType(ccnum) {
var x = {
Visa: /^4[0-9]{12}(?:[0-9]{3})?$/,
Mastercard: /^5[1-5][0-9]{14}$/,
AmericanExpress: /^3[47][0-9]{13}$/,
}
for (var l in x) {
if (x[l].test(ccnum)) {
return l;
}
}
return null;
}
document.getElementById("cardForm").addEventListener("submit", function(e) {
e.preventDefault();
var ccnum = document.getElementById("cardnum").value;
if (ccnum.length < 16) return
var cardArray = [cardnum]
var temp = ccnum;
var checkerArray;
if (cardType(ccnum) === "AmericanExpress") {
checkerArray = [15];
for (let x = 0; x < 15; x++) {
checkerArray[x] = ccnum % 10;
ccnum /= 10;
}
} else {
checkerArray = [16];
for (let x = 0; x < 16; x++) {
checkerArray[x] = ccnum % 10;
ccnum /= 10;
if (ccnum == 0) {
checkerArray[15] = -1;
checkerArray[14] = -1;
checkerArray[13] = -1;
}
}
}
var summing;
for (let x = 1; x < checkerArray.length; x = x + 2) {
checkerArray[x] *= 2;
if (checkerArray[x] >= 10) {
summing += checkerArray[x] % 10 + checkerArray[x] / 10
} else {
summing += checkerArray[x];
}
}
for (let x = 0; x < checkerArray.length; x = x + 2) {
summing += checkerArray[x];
}
if (summing == 20) {
alert("This Card is Legit")
} else {
alert("This Card is Invalid")
}
})
<form id="cardForm">
<input type="tel" id="cardnum" maxlength="16" />
<span id="valid">Enter a Number and Press Enter</span>
</form>

How to fix 'ReferenceError: hello is not defined' when it is

I'm trying to code a game and I want to make it so that when you click a button, it increases the number. My game is like a mining game and you click to get ores and at the top right is a box which tells you what you are mining and you can see what you are mining, except when I click the mine button, it comes with the error which says ReferenceError: hello is not defined. The function hello is the function which gives you the ores.
I have tried fixing up some other functions which give you helpers in exchange for help but it didn't change anything, also I checked on stack overflow, but there wasn't anything that could help me. (Keep in mind I am 10 years old)
HTML:
<div class="mwrapper">
<button id="minebutton" onclick="hello()">Mine!</button>
</div>
JavaScript:
//defining the vars.
var stonei = 0;
var deepness = 0;
var stone = 0;
var silveri = 0;
var silver = 0;
var goldi = 0;
var gold = 0;
var platinumi = 0;
var platinum = 0;
var diamondi = 0;
var diamond = 0;
var alexandritei = 0;
var alexandrite = 0;
var amethysti = 0;
var amethyst = 0;
var unobtaniumi = 0;
var unobtanium = 0;
var emeraldi = 0;
var emerald = 0;
var tubi = 0;
var tub = 0;
var blockN;
var block = 0;
var money = 0;
var stoneSold = 0;
var silverSold = 0;
var goldSold = 0;
var clickers = 0;
var moneyEver = 0;
var BpS = 0;
//defining element to shorten code later
var blockEl = document.getElementById("block");
var btxtEL = document.getElementById("btxt");
var moneyEl = document.getElementById("money");
//changing what the 'Block you are mining' says
var findBlock = function(b) {
if (b === 0) {
blockEl.style.backgroundColor = "grey";
btxt.innerHTML = "Stone";
blockN = "stone";
}
else if (b === 1) {
blockEl.style.backgroundColor = "silver";
btxt.innerHTML = "Silver";
blockN = "silver";
}
else if (b === 2) {
blockEl.style.backgroundColor = "gold";
btxt.innerHTML = "Gold";
blockN = "gold";
}
else if (b === 3) {
blockEl.style.backgroundColor = "rgb(90, 89, 89)";
btxt.innerHTML = "Platinum"
blockN = "platinum";
}
else if (b === 4) {
blockEl.style.backgroundColor = "rgb(185, 242, 255)";
btxt.innerHTML = "Diamond"
blockN = "diamond";
}
else if (b <= 10) {
btxt.innerHTML = "Not coded yet";
}
//hehe
else {
btxt.innerHTML = "WHAAAA?????????";
}
}
//adds materials to the left sidebar
var createBlock = function(b) {
if (b === 0) {
stonei += 1;
stone += 1;
document.getElementById("stonei").innerHTML = stonei;
document.getElementById("stone").innerHTML = stone;
}
else if (b === 1) {
silveri += 1;
silver += 1;
document.getElementById("silveri").innerHTML = silveri;
document.getElementById("silver").innerHTML = silver;
}
else if (b === 2) {
goldi += 1;
gold += 1;
document.getElementById("goldi").innerHTML = goldi;
document.getElementById("gold").innerHTML = gold;
}
else if (b === 3) {
platinumi += 1;
platinum += 1;
document.getElementById("platinumi").innerHTML = platinumi;
document.getElementById("platinum").innerHTML = platinum;
}
else if (b === 4) {
diamondi += 1;
diamond += 1;
document.getElementById("diamondi").innerHTML = diamondi;
document.getElementById("diamond").innerHTML = diamond;
}
//not coded rest
}
//From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
//for finding the block when you mine
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}
//when you click the mine button it does this
var hello = function() {
if (deepness === stone + silver + gold + platinum + diamond && stone >= stonei && silver >= silveri && gold >= goldi && platinum >= platinumi && diamond >= diamondi && stoneSold == stone - stonei && moneyEver == money + clickers &&typeof hsjsahjkd === 'undefined' || hsjsahjkd === null) {
if (deepness <= 50) {
block = 0;
findBlock(block);
deepness += 1;
createBlock(block)
}
else if (deepness >= 50, deepness < 150) {
block = getRandomInt(0, 2);
findBlock(block);
deepness += 1;
createBlock(block)
}
else if (deepness >= 150, deepness < 250) {
block = getRandomInt(0, 3);
findBlock(block);
deepness += 1;
createBlock(block)
}
else if (deepness >= 250, deepness < 350) {
block = getRandomInt(0, 4);
findBlock(block);
deepness += 1;
createBlock(block)
}
else if (deepness >= 350) {
block = getRandomInt(0, 5);
findBlock(block);
deepness += 1;
createBlock(block)
}
}
else {
btxt.innerHTML = "Cheater.";
stonei = 0;
deepness = 0;
stone = 0;
silveri = 0;
silver = 0;
goldi = 0;
gold = 0;
platinumi = 0;
platinum = 0;
diamondi = 0;
diamond = 0;
alexandritei = 0;
alexandrite = 0;
amethysti = 0;
amethyst = 0;
unobtaniumi = 0;
unobtanium = 0;
emeraldi = 0;
emerald = 0;
tubi = 0;
tub = 0;
stoneSold = 0;
silverSold = 0;
goldSold = 0;
clickers = 0;
moneyEver = 0;
BpS = 0;
console.log("You cheated. The game restarted.")
if (typeof hsjsahjkd == 'undefined') {
var hsjsahjkd = 1;
}
else {
hsjsahjkd += 1;
}
document.getElementById("cheat").innerHTML = hsjsahjkd;
}
}
Sorry, but the functions needed are quite long. If you want to see the whole code, go to megagames.me/games/mining.html
I expected the out put of hello() to increment some of the ores, but it gave ReferenceError: hello is not defined.
Make sure your JavaScript is linked in the head tag or above the button. Otherwise you'll be calling a function that doesn't exist yet. An easy way to remember is to think of it as a book, you read from top to bottom the same way JavaScript executes top to bottom.
Also, try using Let and Const instead of Var and try using Switch Cases instead of if else all over. :-)

Error javascript function

Hi I'm using Dreamweaver and it detected an error on line 78 to this javascript function. I can't spot the problem. Help me guys.
function FrontPage_Form1_Validator(theForm)
{
var checkOK = "0123456789-.,";
var checkStr = theForm.theField.value;
var allValid = true;
var validGroups = true;
var decPoints = 0;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch = = checkOK.charAt(j)) //------This is line 78------//
break;
if (j = = checkOK.length)
{
allValid = false;
break;
}
if (ch = = ".")
{
....
//will cut code here since it's not letting me post with so much code.
There is a space between the equal signs in your conditional.
Change = = to ==.

Trying to create tetris, but can't

For some reason my code refuses to let me assign an x and y position to each block. Each block is 30 pixels wide and tall and will be colored according to which piece it is a part of. Gravity and a clear function haven't been implemented and the move left and move right function are so different because it wasn't working right and then recreated it as it is seen now in the left function and it works less...
Please help!
Edit: My apologies, i dont normally post anything. The part i cant get past now is function block/add block/typeSet/assigning the type. it assigns a random one pretty well but then when that type (such as square) goes to assign x and y values it gives an error.
<!DOCTYPE html>
<html>
<head>
<title>Tetris in Canvas</title>
<style type="text/css">
canvas {
border: solid 1px black;
margin: -8px;
}
</style>
<script type="text/javascript">
var canvas = null;
var ctx = null;
var blockArray = [];
var board = [];
var blockNum = 0;
for (var s = 0; s < 14; s++) {
board[s] = [27];
for (var t = 0; t < 27; t++) {
board[s][t] = -1;
}
}
document.onkeydown = function(event) {
var keyCode;
if(event == null)
keyCode = window.event.keyCode;
else
keyCode = event.keyCode;
switch(keyCode) {
case 37:
left(blockArray.length);
break;
case 38:
up(blockArray.length);
break;
case 39:
right(blockArray.length);
break;
case 40:
down(blockArray.length);
break;
default:
break;
}
}
window.onload = function init() {
canvas = document.getElementById('Canvas1');
ctx = canvas.getContext("2d");
blank();
addBlock();
animate();
}
function up(i) {
blank();
animate();
}
function down(i) {
var u = 0;
var p = 0;
while(u<4) {
if (blockArray[i].y[u] + 1 > 26) {
u = 10;
}
else if ((board[blockArray[i].x[u]][blockArray[i].y[u] + 1]) == -1) {
u++;
}
else {
p = u;
for (var g = 0; ((g < 4) && (p = u)); g++) {
if ((blockArray[i].x[u] == blockArray[i].x[g]) && (blockArray[i].y[u] + 1 == blockArray[i].y[g])) {
u++;
}
}
if (p == u)
u = 10;
}
}
if (u < 10) {
for (var j=0; j<4; j++) {
blockArray[i].y[j] +=1;
}
}
else
addBlock();
animate();
}
function right(i) {
var u = 0;
var p = 0;
while(u<4) {
if (blockArray[i].x[u] + 1 > 13) {
u = 10;
}
else if ((board[blockArray[i].x[u] + 1][blockArray[i].y[u]]) == -1)
u++;
else {
p = u;
for (var g = 0; ((g < 4) && (p = u)); g++) {
if ((blockArray[i].x[u] + 1 == blockArray[i].x[g]) && (blockArray[i].y[u] == blockArray[i].y[g]))
u++;
}
if (p == u)
u = 10;
}
}
if (u < 10) {
for (var j=0; j<4; j++) {
blockArray[i].x[j] +=1;
}
}
else
addBlock();
animate();
}
function left(i) {
var u;
var p = 14;
for (var w = 0; w<4; w++) {
if (blockArray[i].x[w] < p) {
p = blockArray[i].x[w];
u = w;
}
}
if (p > 0) {
if ((board[blockArray[i].x[u] - 1][blockArray[i].y[u]]) == -1) {
for (var j=0; j<4; j++) {
blockArray[i].x[j] -=1;
}
}
}
animate();
}
function block(x, y, type) {
blockNum += 1;
this.id = blockNum;
this.x = [];
this.y = [];
this.landed = false;
this.type = Math.floor(Math.random() * (6)) + 1;
typeSet(this.type);
}
function addBlock() {
blockArray[blockArray.length] = new block();
}
function typeSet(type) {
i = blockArray.length;
switch (type) {
case 1:
square(i);
break;
case 2:
elR(i);
break;
case 3:
elL(i);
break;
case 4:
line(i);
break;
case 5:
zeR(i);
break;
case 6:
zeL(i);
break;
default:
break;
}
}
function animate() {
fillBoard();
colorBoard();
}
function fillBoard() {
for (var i = 0; i < 14; i++) {
for (var q = 0; q < 27; q++) {
board[i][q] = -1;
}
}
for (var i=0; i<blockArray.length; i++) {
for (var q=0; q<4; q++) {
board[blockArray[i].x[q]][blockArray[i].y[q]] = blockArray[i].type;
}
}
}
function colorBoard() {
blank();
ctx.strokeStyle = "white";
for(var q=0; q<14; q++) {
for(var r=0; r<27; r++) {
switch (board[q][r]) {
case 1:
ctx.fillStyle = "green";
color(q,r);
break;
case 2:
ctx.fillStyle = "orange";
color(q,r);
break;
case 3:
ctx.fillStyle = "cyan";
color(q,r);
break;
case 4:
ctx.fillStyle = "black";
color(q,r);
break;
case 5:
ctx.fillStyle = "yellow";
color(q,r);
break;
case 6:
ctx.fillStyle = "brown";
color(q,r);
break;
default:
break;
}
}
}
}
function color(q,r) {
ctx.fillRect(q*30,(r-4)*30,30,30);
ctx.strokeRect(q*30,(r-4)*30,30,30);
}
function square(i) {
blockArray[i].x[0] = 7;
blockArray[i].y[0] = 0;
blockArray[i].x[1] = 7;
blockArray[i].y[1] = 1;
blockArray[i].x[2] = 8;
blockArray[i].y[2] = 0;
blockArray[i].x[3] = 8;
blockArray[i].y[3] = 1;
}
function elR(i) {
blockArray[i].x[0] = 7;
blockArray[i].y[0] = 0;
blockArray[i].x[1] = 7;
blockArray[i].y[1] = 1;
blockArray[i].x[2] = 7;
blockArray[i].y[2] = 2;
blockArray[i].x[3] = 8;
blockArray[i].y[3] = 2;
}
function elL(i) {
blockArray[i].x[0] = 7;
blockArray[i].y[0] = 0;
blockArray[i].x[1] = 7;
blockArray[i].y[1] = 1;
blockArray[i].x[2] = 7;
blockArray[i].y[2] = 2;
blockArray[i].x[3] = 6;
blockArray[i].y[3] = 2;
}
function line(i) {
blockArray[i].x[0] = 7;
blockArray[i].y[0] = 0;
blockArray[i].x[1] = 7;
blockArray[i].y[1] = 1;
blockArray[i].x[2] = 7;
blockArray[i].y[2] = 2;
blockArray[i].x[3] = 7;
blockArray[i].y[3] = 3;
}
function zeR(i) {
blockArray[i].x[0] = 6;
blockArray[i].y[0] = 0;
blockArray[i].x[1] = 7;
blockArray[i].y[1] = 0;
blockArray[i].x[2] = 7;
blockArray[i].y[2] = 1;
blockArray[i].x[3] = 8;
blockArray[i].y[3] = 1;
}
function zeL(i) {
blockArray[i].x[0] = 8;
blockArray[i].y[0] = 0;
blockArray[i].x[1] = 7;
blockArray[i].y[1] = 0;
blockArray[i].x[2] = 7;
blockArray[i].y[2] = 1;
blockArray[i].x[3] = 6;
blockArray[i].y[3] = 1;
}
function blank() {
ctx.restore();
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.save();
}
function blank2() {
ctx.restore();
ctx.fillStyle = "purple";
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.save();
}
function rotateImgRight() {
for(d = 0; d < 180; d++) {
setTimeout(rotateImg, 50);
}
}
function rotateImgLeft() {
for(d = 0; d < 180; d++) {
setTimeout(rotateImg2, 50);
}
}
function rotateImg2() {
degrees = degrees - 0.5;
radian = (Math.PI / 180.0) * degrees;
blank();
ctx.translate(ctx.canvas.width/2, 700);
ctx.rotate(radian);
ctx.drawImage(srcImg, -srcImg.width/2,-srcImg.height/2);
slide = (degrees / 90) % 4;
}
function rotateImg(x,y) {
degrees = degrees + 0.5;
radian = (Math.PI / 180.0) * degrees;
ctx.translate(x,y);
ctx.rotate(radian);
ctx.drawImage(srcImg, -srcImg.width/2,-srcImg.height/2);
slide = (degrees / 90) % 4;
}
</script>
</head>
<body>
<div style="width: 100%; text-align:center">
<canvas id="Canvas1" width="421" height="690">Your browser does not support canvas.</canvas>
</div>
</body>
</html>
Your function addBlock looks like this:
function addBlock() {
blockArray[blockArray.length] = new block();
}
At any arbitrary point, your blockArray length could be anything. For now let's assume it's 25.
You're inserting a block to the 25th position in the array.
Let's have a look at the block function:
function block(x, y, type) {
blockNum += 1;
this.id = blockNum;
this.x = [];
this.y = [];
this.landed = false;
this.type = Math.floor(Math.random() * (6)) + 1;
typeSet(this.type);
}
What's this typeSet command? It doesn't look like it sets anything! Let's investigate:
function typeSet(type) {
i = blockArray.length;
switch (type) {
case 1:
square(i);
break;
// blah
}
}
AHA! I've found the problem, square() goes to the blockArray at position i, which doesn't even exist yet as block() hasn't finished executing, and assigns loads of x and y variables all over the place. When block() finishes executing, it overwrites all the values that typeSet() has just written.
You need to refactor your typeSet() method so that it takes in the actual block object and sets the x and y values on that rather than trying to access the element within the array (which doesn't even exist yet):
typeSet(this, this.type);
you can start by checking if the arrays actually contain the items you think they contain
for example, in your left method, instead of this:
for (var w = 0; w<4; w++) {
if (blockArray[i].x[w] < p) {
p = blockArray[i].x[w];
u = w;
}
}
do this:
for (var w = 0; w<4; w++) {
if ((blockArray[i]) && (blockArray[i].x[w])) {
if (blockArray[i].x[w] < p) {
p = blockArray[i].x[w];
u = w;
}
}
}
it seems as though the array is not fully populated and only done so at runtime, this can lead to undefined references.
PS: i have not fully read the code; only part of it, as it is a lot to work through.

Categories