vmars316 Posted April 25, 2016 Share Posted April 25, 2016 Hello & Thanks , I am getting the following error: Uncaught ReferenceError: thruth01 is not defined on this statement: if(cowpie.y > truth01.y && cowpie.y < thruth01.y + 64 || cowpie.y > truth02.y && cowpie.y < thruth02.y + 64 ) { alert("truth cowpie collision !") } function startGame() { // truth01 = new TargetImages(); truth01.idTag = "truth01"; truth01.x = 0; truth01.y = 40; truth01.width = 64; truth01.height = 64; truth01.speedX = 1; truth01.speedY = 0; truth01.tripsCnt =0; truth01.tripsMax=2; truth01.visible = true; truth01.directionX = 1; // lies01 = new TargetImages(); lies01.idTag = "lies01"; lies01.x = 0; lies01.y = 40; lies01.width = 64; lies01.height = 64; lies01.speedX = 1; lies01.speedY = 0; lies01.tripsCnt =0; lies01.tripsMax=2; lies01.visible = false; lies01.directionX = 1; // myGameArea.start(); } Whole code is here: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <meta charset="utf-8" /> <title>Th,Co,Tr</title> <!-- http://liesandcowpies.com/javascript/ --> <style> canvas { border:1px solid #d3d3d3; background-color: #f1f1f1; } #assets { visibility: hidden; } </style> </head> <body onload="startGame()"> fromWhere= <span id="fromWhere">fromWhere</span><br> idTag= <span id="idTag">idTag</span><br> this.idTag= <span id="thisIdTag">this.idTag</span><br> this.visible= <span id="this.visible"> this.visible</span><br> this.tripsMax= <span id="this.tripsMax"> this.tripsMax</span><br> this.tripsCnt= <span id="this.tripsCnt"> this.tripsCnt</span><br> this.directionX= <span id="this.directionX"> this.directionX</span><br> this.x= <span id="this.x"> this.x</span><br> thrower x,y,w,h= <span id="thrower.xywh">thrower.x,y,w,h</span><br> <div align="center"> <table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2"> <tbody> <tr> <td style="width:70%";><p id="traceMsg">traceLog goes here:</p> </td> <td style="width:30%";> <canvas id="canvas" width="300" height="300"></canvas> </td> </tr> </tbody> </table> </div> <div id="assets"> <img id="truth01" src="sprites/truth01.png" width="64" height="64" /> <img id="truth02" src="sprites/truth02.png" width="64" height="64" /> <img id="lies01" src="sprites/lies01.png" width="64" height="64" /> <img id="lies02" src="sprites/lies02.png" width="64" height="64" /> <img id="cowpie" src="sprites/cowpie.png" width="32" height="32" /> <img id="thrower" src="sprites/thrower.png" width="64" height="64" /> <audio id="closeDoor" src="sounds/CloseDoor.ogg"></audio> </div> <script type='text/javascript'> // var oneTraceLine = ""; var traceYESorNO = false; var updateFor1stTime = 0; var fromWhere = "fromWhere"; var timesInTripsCk=0; var visible = true; var thisx=0; var tripsMax=0; priorTripsMax = 0; var thisIdTag; var thrower_xywh = ""; var resetSpeedX = 2; var z =0; var idTag = "truth01"; var idTagTh = "thrower"; var firstTimeInCowpieUpdate = true ; var idTag01 = "truth01"; // prime the pump var idTag02 = "truth02"; // prime the pump var idTag03 = "truth03"; // prime the pump var idTag04 = "truth04"; // prime the pump // var truth01; var lies01; var truth02; var lies02; var tripsCnt=0; var directionX=1 ; var addit = 0; // canvas = document.getElementById("canvas"); // get the canvas ctx = canvas.getContext('2d'); // create canvas Context; var allImgsCnt = 0; var idTag = "truth01"; var totalTrips = 0; var allImgs = [document.getElementById("truth01"), document.getElementById("lies01"), //, document.getElementById("truth02"), document.getElementById("lies02"), document.getElementById("thrower") ] ; // var cowpies = []; var cowpieImg = document.getElementById("cowpie"); // var Thrower = function () { var self = this; this.idTag = 'thrower'; this.x = (canvas.width / 2) -30; this.y = canvas.height - 64; this.width= 64; this.height = 64; this.speedX = 2 ; this.speedY = 0; this.visible = true; this.directionX = 9; this.moveMe = false ; this.update = function() { if(self.x < 2 || self.x > (canvas.width -50)) {self.x = (canvas.width / 2) -30; } self.x = self.x + (self.speedX * self.directionX); // always = +1 or -1 } self.speedX = resetSpeedX ; self.moveMe = false ; } } // thrower = new Thrower(); thrower.idTag ; // = 'thrower'; thrower.x ; // = 0; thrower.y ; // = canvas.height - 64; thrower.width ; // = 64; thrower.height ; // = 64; thrower.speedX ; // = 1; thrower.speedY ; // = 0; thrower.visible ; // = true ; thrower.directionX ; // = 1; thrower.moveMe ; // = true ; // var Cowpie = function () { var self = this; this.idTag = 'cowpie'; this.idActive = true ; this.x = thrower.x; this.y = thrower.y; this.width= 32; this.height = 32; this.speedX = 0; this.speedY = 3; this.visible = true; this.directionY = -1; this.moveMe = false ; } // Cowpie.prototype.update = function() { var watchi = 0; if (firstTimeInCowpieUpdate && cowpies.length == 0) {cowpies.push(cowpie = new Cowpie); firstTimeInCowpieUpdate = false;} for (var i = cowpies.length - 1; i >= 0; i--) { watchi = i; if (cowpies[watchi].idActive) { // alert("cowpie.update " + watchi + " 4 cowpies[watchi].idActive= " + cowpies[watchi].idActive); cowpies[watchi].y = cowpies[watchi].y+ (cowpies[watchi].speedY * cowpies[watchi].directionY); // always -1 } ctx.drawImage(cowpieImg, cowpies[watchi].x, cowpies[watchi].y, cowpies[watchi].width, cowpies[watchi].height); /*out of bounds?*/ if(cowpies[watchi].y < 1) { cowpies[watchi].idActive = false ; } // out of bounds check /*any collisions ?*/ // collisionCk(); if(cowpie.y > truth01.y && cowpie.y < thruth01.y + 64 || cowpie.y > truth02.y && cowpie.y < thruth02.y + 64 ) { alert("truth cowpie collision !") } if(cowpie.y > lies01.y && cowpie.y < lies01.y +64 || cowpie.y > lies02.y && cowpie.y < lies02.y +64 ) { alert("lies cowpie collision !") } if(!cowpies[watchi].idActive) { // alert("cowpie.update " + watchi + " 5 cowpies[watchi].idActive= " + cowpies[watchi].idActive); cowpies.splice(i,1); } } cowpieCount = cowpies.length; } } // cowpie = new Cowpie(); cowpie.idTag = 'cowpie'; // function startGame() { // truth01 = new TargetImages(); truth01.idTag = "truth01"; truth01.x = 0; truth01.y = 40; truth01.width = 64; truth01.height = 64; truth01.speedX = 1; truth01.speedY = 0; truth01.tripsCnt =0; truth01.tripsMax=2; truth01.visible = true; truth01.directionX = 1; // lies01 = new TargetImages(); lies01.idTag = "lies01"; lies01.x = 0; lies01.y = 40; lies01.width = 64; lies01.height = 64; lies01.speedX = 1; lies01.speedY = 0; lies01.tripsCnt =0; lies01.tripsMax=2; lies01.visible = false; lies01.directionX = 1; // myGameArea.start(); } var TargetImages = (function () { function TargetImages() { this.nameTag = 'truth01' this.x = 0; this.y = 0; this.width= 32; this.height = 32; this.speedX = 1; this.speedY = 0; this.tripsCnt =0; this.tripsMax=2; this.visible = true; this.directionX = 1; this.update = function() { ctx.drawImage(allImgs[allImgsCnt], this.x, this.y, this.width, this.height); this.x = this.x + (this.speedX * this.directionX); // always = +1 or -1 document.getElementById("this.x").innerHTML = this.x; idTag = this.idTag; if(this.x > canvas.width - 2) {this.directionX = -1; // change direction this.x = canvas.width - 2; this.tripsCnt++; totalTrips++; fromWhere = "(this.x > canvas.width)"; thisIdTag = this.idTag; visible = this.visible; tripsMax = this.tripsMax; tripsCnt = this.tripsCnt; directionX = this.directionX; thisx = this.x; alertStats(); } if(this.x < -64 ) {this.directionX = 1; // change direction this.x = 0; this.tripsCnt++; totalTrips++; fromWhere = "if(this.x < -63 )"; thisIdTag = this.idTag; visible = this.visible; tripsMax = this.tripsMax; tripsCnt = this.tripsCnt; directionX = this.directionX; thisx = this.x; alertStats(); } if(this.tripsCnt > this.tripsMax) { if(this.idTag == "truth01") {this.speedX = 0; truth01.tripsCnt =0; // lies01.speedX=1 ; idTag01 = "lies01"; this.visible=false ; lies01.visible=true ; lies01.speedX=1 ; idTag = "lies01"; this.visible=false ; lies01.visible=true ; } if(this.idTag == "lies01") {this.speedX = 0; lies01.tripsCnt =0; truth01.speedX=1 ; idTag = "truth01" ; lies01.visible=false ; truth01.visible=true ; } fromWhere = "if(this.tripsCnt > this.tripsMax )"; thisIdTag = this.idTag; visible = this.visible; tripsMax = this.tripsMax; tripsCnt = this.tripsCnt; directionX = this.directionX; thisx = this.x; alertStats(); // alert('LEAVING TRIPsMAX: truth01.visible= ' + this.visible + ' , lies01.visible= ' + lies01.visible + ' \ntotalTripsCnt= ' + totalTrips); } } return TargetImages; })(); function collisionCk() { } // function alertStats() { document.getElementById("fromWhere").innerHTML = fromWhere; document.getElementById("idTag").innerHTML = idTag; document.getElementById("thisIdTag").innerHTML = thisIdTag; document.getElementById("this.visible").innerHTML = visible; document.getElementById("this.tripsMax").innerHTML = tripsMax; document.getElementById("this.tripsCnt").innerHTML = tripsCnt; document.getElementById("this.directionX").innerHTML = directionX ; document.getElementById("this.x").innerHTML = thisx; document.getElementById("thrower.xywh").innerHTML = thrower.x; } // function updateGameArea() { myGameArea.clear(); allImgsCnt = 0; if(idTag == "truth01" && truth01.visible) { truth01.update(); } allImgsCnt = 1; if(idTag == "lies01" && lies01.visible) { lies01.update(); } allImgsCnt = 4; if(thrower.visible) { // alert("thrower.visible"); if(thrower.moveMe) {thrower.update();} ctx.drawImage(allImgs[allImgsCnt], thrower.x, thrower.y, thrower.width, thrower.height); thisIdTag = thrower.idTag; visible = this.visible; tripsMax = this.tripsMax; tripsCnt = this.tripsCnt; directionX = this.directionX; thisx = this.x; thrower_xywh = (thrower.x +" , "+ thrower.y +" , "+ thrower.width +" , "+ thrower.height); alertStats(); }; cowpie.update(); } // End OF function updateGameArea() // var myGameArea = { start : function() { thrower.moveMe = false ; interval = setInterval(updateGameArea, 5); // updateGameArea(); }, clear : function() { ctx.clearRect(0, 0, canvas.width, canvas.height); }, stop : function() { clearInterval(this.interval); } } // document.addEventListener("keydown", keyDownHandler, false); document.addEventListener("keyup", keyUpHandler, false); document.addEventListener("mousemove", mouseMoveHandler, false); // function keyDownHandler(e) { // alert(" 1 cowpies.length - 1 " + z); if(e.keyCode == 39 || event.keyCode == 68) { // move right D or ---> thrower.directionX = 9; thrower.moveMe = true; } if(e.keyCode == 37 || event.keyCode == 65) { // move left A or <--- thrower.directionX = -9; thrower.moveMe = true; } if(e.keyCode == 87 || event.keyCode == 38) { // shoot = W or upArrow // alert("if(e.keyCode == 87 ||"); z= 0 ; cowpies.push(cowpie = new Cowpie); // add cowpie in last position z = cowpies.length - 1; // put last pie created position into z // alert(" 2 cowpies.length - 1 " + z); ctx.drawImage(cowpieImg, cowpies[z].x, cowpies[z].y, cowpies[z].width, cowpies[z].height); } // END OF for (var i = } // function keyUpHandler(e) { if(e.keyCode == 38 || event.keyCode == 87) { rightPressed = false; } else if(e.keyCode == 40 || event.keyCode == 83) { leftPressed = false; } } function mouseMoveHandler(e) { var relativeX = e.clientX - canvas.offsetLeft; // if(relativeX > 0 && relativeX < canvas.width) { // paddleX = relativeX - paddleWidth/2; // } } // // </script> </body> </html> I think it has to do with the placement of : truth01 = new TargetImages(); But I can't figure out where it should go . Thanks Link to comment Share on other sites More sharing options...
davej Posted April 25, 2016 Share Posted April 25, 2016 Maybe thruth should be truth ? Link to comment Share on other sites More sharing options...
vmars316 Posted April 25, 2016 Author Share Posted April 25, 2016 Ugh , Thanks ! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now