I'm fairly new at programming so please forgive what I am sure is a simple answer.
I am trying to get this pacman animation to cycle through an array of images and reverse direction when it hits the edge of the page.
I tried debugging in Chrome and it gave me the following error
"cannot read properties of null (reading width)"
var pos = 0;
//pageWidth is the width of the webpage. This is later used to calculate when Pac-Man needs to turn around.
const pageWidth = window.innerWidth;
//This array contains all the PacMan movement images
const pacArray = [
['./images/PacMan1.png', './images/PacMan2.png'],
['./images/PacMan3.png', './images/PacMan4.png'],
];
// this variable defines what direction should PacMan go into:
// 0 = left to right
// 1 = right to left (reverse)
var direction = 0;
// This variable helps determine which PacMan image should be displayed. It flips between values 0 and 1
var focus = 0;
// This function is called on mouse click. Every time it is called, it updates the PacMan image, position and direction on the screen.
function Run() {
let img = document.getElementById('PacMan');
let imgWidth = img.width;
let pageWidth = window.innerWidth;
focus = (focus 1) % 2;
direction = checkPageBounds(direction, imgWidth, pos, pageWidth);
img.src = pacArray[direction][focus];
if (direction) {
pos -= 20;
img.style.left = pos 'px';
} else {
pos = 20;
img.style.left = pos 'px';
}
}
setInterval(Run,200)
// This function determines the direction of PacMan based on screen edge detection.
function checkPageBounds(direction, imgWidth, pos, pageWidth) {
if (direction == 1 & pos imgWidth > pageWidth) direction== 0;
if (direction == 0 & pos < 0) direction == 1;
//
return direction;
}
//Please do not change
module.exports = checkPageBounds;
Again any advice is greatly appreciated
Thank you
CodePudding user response:
The error says you're trying to get a property called width
from something that is null
.
Looking at your code, it's probably related to this line:
let imgWidth = img.width
Note: the error actually tells you the exact line number and character index at which the error happened.
This means that, at the time your Run
function is invoked, document.getElementById('PacMan')
returns null
.
It can mean one of the following:
- you misspelled
PacMan
- you've placed the
<script>
tag above the#PacMan
element, so the element with that id has not yet been added to DOM.
If we're dealing with the latter, note that, by default, <script>
contents are executed as soon as the tag is met by the DOM parser. Here's how you can delay its execution until all DOM content has loaded:
<script>
window.addEventListener('DOMContentLoaded', () => {
console.log('DOM fully loaded and parsed');
// place your script in here. It will find `#PacMan` even if
// the element with that id is placed later in DOM than this script
});
</script>
CodePudding user response:
Your example don't shows the template/html. Is the element id (PacMan) written correctly?
This example should print a 200 in console:
<img id="PacMan" src="https://picsum.photos/200/300" />
const img = document.getElementById('PacMan');
let imgWidth = img.width;
Just a little off-topic suggestion: take a look on difference between "var", "const" and "let".