sei-ec-remote / project-1-issues

Open new issues here
1 stars 2 forks source link

Game project issue #152

Closed RitaHC closed 1 year ago

RitaHC commented 1 year ago

What's the problem you're trying to solve?

--> I've created a home image on my canvas, BUT UNABLE TO ADD AN EVENT LISTENER TO THE IMAGE

Post any code you think might be relevant (one fenced block per file)

const home = () => {
    console.log('HomeScreen')

    centerX = (canvas.width/2) -50 
    centerY = (canvas.height/2) +30

    const img = document.getElementById('homeimg')
    const homeBtn = ctx.drawImage(img,centerX,centerY, 100 , 100)
    console.log(homeBtn)

    homeBtn.addEventListener('click', () => {
        console.log('Score clicked')
        location.reload()

      })

}

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

---> Event Listener is not working ---> script.js:328 Uncaught TypeError: Cannot read properties of undefined (reading 'addEventListener') at home (script.js:328:13) at animate (script.js:302:10)

What is your best guess as to the source of the problem?

---> Consoled the value of the homeBtn (value-unidentified) - unable to resolve the issue

What things have you already tried to solve the problem?

Online resources/MDN/videos

Paste a link to your repository here

--- >https://github.com/RitaHC/Raven-Hunt

asands94 commented 1 year ago

Based on what I was searching and trying out, it looks like you cannot use addEventListener with drawImage as nothing is returned for that value.

https://stackoverflow.com/questions/2664485/html5-adding-an-eventlistener-to-a-drawn-image-on-canvas