JavaScript Memory Game: Enhance User Experience with Hover Effects on Tiles

Looking for some assistance with JavaScript code I've written for a memory game on Khan Academy. I'm struggling to figure out how to change the color of a tile when the mouse hovers over it. I attempted to draw a star on a tile within the "if (tiles[i].isUnderMouse(mouseX, mouseY))" section in the mouseClicked function as a test. However, this approach only worked when the mouse was clicked and didn't persist once new tiles were drawn. I'm unsure where to start to address this issue. Any guidance would be appreciated.

// Define variables
var fdImage = image(getImage("avatars/questionmark"), this.x, this.y, this.width, this.width);

var Tile = function(x, y, face) {
    this.x = x;
    this.y = y;
    this.face = face;
    this.width = 70;
};

Tile.prototype.drawFaceDown = function() {
    fill(214, 247, 202);
    strokeWeight(2);
    rect(this.x, this.y, this.width, this.width, 10);
    image(getImage("avatars/questionmark"), this.x, this.y, this.width, this.width);
    this.isFaceUp = false;
};

// More code here...

Answer №1

To apply a hover effect to the tiles within this Khan Academy task, you need to start by introducing a new function to your Tile object

Tile.prototype.applyHoverEffect = function() {
    fill(150, 247, 202);
    strokeWeight(2);
    rect(this.x, this.y, this.width, this.width, 10);
    image(getImage("avatars/leaf-green"), this.x, this.y, this.width, this.width);
    this.isFaceUp = false;
};

Next, utilize the mouseMoved event in the following manner:

mouseMoved = function() {
    for (var i = 0; i < tiles.length; i++) {
        if (tiles[i].isUnderMouse(mouseX, mouseY) && !tiles[i].isFaceUp) {
            tiles[i].applyHoverEffect();
        } else if (tiles[i].isFaceUp) {
            tiles[i].drawFaceUp();
        } else {
            tiles[i].drawFaceDown();
        }
    }
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Executing a controller action in Yii by utilizing JavaScript's $.get method

Recently, I've been attempting to invoke a controller action using javascript $.get. A helpful member on Stack Overflow recommended the following code: $.get("custom/balance", function(){ }); In this code snippet, 'custom' is the control ...

The Protractor tool (node:9208) threw an UnhandledPromiseRejectionWarning due to an ElementNotVisibleError, indicating that the element is not interactable. Despite this

I am attempting to interact with an element using protractor but encountering the following error (node:9208) UnhandledPromiseRejectionWarning: ElementNotVisibleError: element not interactable (Session information: chrome=69.0.3497.92) (Driver informa ...

Utilizing Vue refs to set focus on a specific element target

Clicking on "span class="before-click" should hide it, and display input class="after-click" instead. The displayed input must be in focus! However, when trying to use $refs.afterClick to access the DOM and apply .focus(), an unexpected error stati ...

Unable to update due to outdated response call causing issues

I am currently in the process of updating outdated response calls and have encountered a peculiar issue where the response is not being properly ended. Typically, I would use : res.send(200, {message: 'Location Updated'}); However, this method ...

Tips for sharing content within an iframe

Despite my efforts to find a solution, I have been unable to come across one that aligns with my specific situation. I currently have a form for inputting person data. Within this form, there is an iframe containing another form for adding relatives' ...

The radio button in the HTML form is disabled when the user selects

I have two radio buttons labeled Billable "Y" and Billable "N". These radio buttons are linked to a database with corresponding values of "Y" or "N". If the user selects "Y" using the radio button, then the NonBillableReason text box should be disabled. ...

Ways to transmit data to the backend using React.js

I am completely new to backend programming and feeling lost when it comes to sending onClick data to the backend for storage. Currently, the backend is a basic flask server, but there is a possibility that my partner might switch it to mySQL. My project is ...

"Clicking the button triggers the addition of a PHP include

Whenever I attempt to incorporate a new form by clicking a button, the code functions properly with regular text like 'abc' but encounters issues when using a php include. Any suggestions for what might be causing this problem? JAVASCRIPT $(do ...

The issue with Three.Js Raycasting arises when attempting to change camera transformations within an Object3D parent element

In my latest project, I decided to create a "camera controller" that handles the movement and rotation of the camera by utilizing an Object3D as its parent. The Y-axis rotations are applied to the Object3D, while the X-axis rotation is directly applied to ...

Sorting through JSON information using JQUERY

Struggling with filtering data using jQuery and JSON. I need to exclude objects with the ID "234" or the sender named "Alan Ford" in order to create separate messaging systems for Inbox and Outbox. Directly manipulating the JSON is not an option. Here&apo ...

What sets apart elem['textContent'] from elem.textContent?

When dealing with a HTMLElement in JavaScript, is there any distinction between the following lines of code: elem['textContent'] = "Test"; versus elem.textContent = "Test"; This question arises when setting the text content of an HTMLElement. ...

Delete the first and last two entries in the json dataset

data:[ { user: "2016-04-14", avg_: "13", new: "32511.20" }, { user: "2016-04-21", avg_: "32", new: "32779.17" }, { user: "2016-04-22", avg_: "32", new: "32898.40" }, { user: "2016-04-23", avg_: "32", new: "32903.11" }, { user: "2016-04-24", avg_: ...

What is the best way to swap out the if else statement with a Ternary operator within a JavaScript function?

Is there a way to replace the if else statement in the function using a Ternary operator in JavaScript? private getProductName(productType: string): string { let productName = 'Product not found'; this.deal.packages.find(p => p.isSele ...

Issue when activating Materialize Bootstrap

I'm facing an issue with my code. I have implemented a feature where a modal should be triggered after a user successfully adds a new user. However, I am using Materialize and the modal is not being triggered. Below is a snippet of my code: <div i ...

Unable to incorporate an external JavaScript file via a static URL

I'm attempting to link an external javascript file using a static URL, like this: <script type="text/javascript" src="{{ url_for('static/js', filename='test.js') }}"></script> However, I am encountering the following ...

Angularjs directive retrieves infowindow DOM element from Google Maps

In order to apply some style fixes to the Infowindow, I am trying to select the element with the class 'gm-style-iw'. This selection process is taking place within an angularjs directive. <div ui-view="full-map" id="full-map" class="mainMap c ...

The webpage is failing to render the properties of the JSON object

I'm encountering an issue where my code is successfully displaying the object, but I am unable to access its properties in the console. What might be preventing me from retrieving the information stored in this object? Below is the snippet of code ca ...

What is the process for resetting a search filter list box?

I developed a search feature for filtering a list based on the instructions provided in this W3Schools tutorial: function myFunction() { var input, filter, ul, li, a, i, txtValue; input = document.getElementById("myInput"); filter = input.va ...

Even after setting the handler to return false, Angular continues to submit the form

In the following scenario, I have encountered an issue: Here is the HTML template snippet: <form [action]='endpoint' method="post" target="my_iframe" #confirmForm (ngSubmit)="submitConfirmation()"> <button type="submit" (click)="conf ...

Changing the class of a div in JavaScript from one class to another

On my HTML page, I have a div with a CSS class assigned to it. The goal is to switch the current class for another when a button is clicked. It's crucial that not a single CSS property within the class is altered - the entire class must be replaced en ...