jQuery is used to make an object fade out once another object has been moved into place

As I delve into the world of jQuery, I've decided to create a simple Super Mario imitation. The concept is to control Mario using arrow keys and fade out mushrooms once Mario reaches them. However, my attempts at achieving this result have not been successful. If anyone could provide guidance in the right direction, I would greatly appreciate it!

Below is the HTML code:

<!DOCTYPE html>
<html>
    <head>
        <title>Super Mario!</title>
        <link rel='stylesheet' type='text/css' href='stylesheet.css'/>
        <script type='text/javascript' src='script.js'></script>
    </head>
    <body>
        <img class="mushroom" src="http://ih2.redbubble.net/image.6378228.5104/sticker,375x360.u2.png"/>
        <img class="mario" src="http://i1061.photobucket.com/albums/t480/ericqweinstein/mario.jpg"/>
    </body>
</html>

Here is the CSS code:

img {
    position: relative;
    left: 0;
    top: 0;
}

.mushroom {
    position: relative;
    left: 300;
    top: 200;
    width: 48px;
    height: 48px;
}

And here's the jQuery implementation:

$(document).ready(function() {
    // Keydown function to move mario
    $(document).keydown(function(key) {
        switch(parseInt(key.which,10)) {
            case 37:
                $('.mario').animate({left: "-=10px"}, 'fast');
                break;
            case 38:
                $('.mario').animate({top: "-=10px"}, 'fast');
                break;
            case 39:
                $('.mario').animate({left: "+=10px"}, 'fast');
                break;
            case 40:
                $('.mario').animate({top: "+=10px"}, 'fast');
                break;
        }
        $(document).on('keydown', '.mario', function(e){
            var x = e.clientX - this.offsetLeft,
                y = e.clientY - this.offsetTop;

            if (x > 300 && x < 400 && y > 200 && y < 300) {
                $('.mushroom').fadeOut('slow');
            }
        });
    });
});

I initially attempted to use an IF statement to check Mario's position using CSS, but that approach didn't work as expected. It seems that the CSS is not updated on key press events. Therefore, I'm currently stuck on this issue.

The original IF statement idea looked like this:

// On Mario's arrival remove Mashroom
$(document).on('keydown', '.mushroom', function() {
    if($('.mario').css("left")=="300" && ("top")=="200") {
        $('.mashroom').animate({left: "+=10px"}, 'fast'); 
    }
});

Thank you in advance for any assistance provided!

Answer №1

Not Found

$('.luigi').css("top")

if($('.luigi').css("left")=="350" && $('.luigi').css("top")=="250") {
//                                   ^              ^

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

MySQL stored procedure called through AJAX/PHP is not returning expected values

After dedicating several days to this task, I am now seeking assistance. My goal is to utilize Ajax/PHP/MySQL to display only a specific subset of a table based on the user's selections from a dropdown menu. The PHP code invokes a MySQL stored procedu ...

HighCharts: visualize vertical stacked bars displaying the distribution of percentages within each stack

I currently have a stacked bar chart similar to the one shown in this JSFiddle demo. My goal is to transform the stacks so that each bar represents a percentage of the total stack height. For instance, in the Apples stack we currently have values {3, 2, 5 ...

Get selectize.js to display only options that begin with the user's input

Using selectize.js, my current setup looks like this: Instead of only showing words that start with 'arm', it displays words or options containing 'arm' as a substring elsewhere. I want to modify the function to display only words or ...

Finding differences between two 24-hour format times using moment.js

Is there a way to compare two times in 24-hour format using the code below? $("#dd_start_timing, #dd_end_timing").on('keyup change keydown', function() { var DutyDayStartTime = $("#dd_start_timing").val().trim();// 13:05 var ...

How does a database contribute to the functionality of a redux application?

Recently, I started exploring redux and finally understood the architecture behind it. In a single page redux application where all data is stored in a central store and updated through frontend actions, what purpose does the back-end and database serve? ...

React Modals: Only the modal component triggered by the first click will open, with no other modals opening

As a newcomer to StackOverflow, I apologize if my problem description is not clear. I am currently learning React and working on a course-search app as a project. The app filters courses based on user input from a JSON file and displays them in cards with ...

Creating a sorting function in VueJS requires a few key steps

I'm working on implementing a sorting function in my VueJS code that includes the following options: Price: Low to High, Price: High to Low. Here is my template: <div name="divSortBy"> <span> Sort by: & ...

Using jQuery to dynamically format a date with variables

Looking to properly format a date for use in a compare function to sort data $(xml).find("item").each(function () { var dateText = $(this).find("Date").text(); var year = dateText.substr(0,4); var month = dateText.subst ...

Upon the page loading, only select a handful of checkboxes (JSF)

I am currently developing a web application using JSF 2.0 and I need to have checkboxes pre-selected when the page loads. <h:selectManyCheckbox value="#{UserRegistration.rightSelected}" id="myRight"> <f:selectItem itemValue="add" itemLabel="A ...

Ways to determine the success of $wpdb->query and retrieve the outcome

Currently, I am in the process of developing a WordPress website, I have implemented a form that allows users to make modifications and update the database: Below is the HTML/PHP code snippet: echo '<form class="form-verifdoc" target=&q ...

Interactive Infographics in HTML5 format

Currently, I am unable to find any examples on how to accomplish a specific task mentioned in the following link. If you have a tutorial with step-by-step instructions or a code project available, it would greatly assist me. Link: Evolution of WEB ...

Today, I am experimenting with HTML5 File Upload using Capybara and Selenium Webdriver in Ruby

I have encountered a problem with a simple modal that allows users to upload a file using a browse button. Due to some unknown issue, possibly related to the fact that it is an HTML5 file input and the browser adds its own functions to it, testing this has ...

Having trouble with aligning items in the center using Bootstrap 5?

Can anyone help me figure out how to center a div or img element vertically within another div element? I've tried using align-items-center", but nothing seems to be working. I've also experimented with changing the display properties of both ele ...

Is there a way to eliminate the additional gap found at the bottom of a div element?

I've been encountering some difficulty when trying to insert an image into a div without any padding. The issue I'm facing is that the sizing doesn't seem to be correct; there's unnecessary extra space at the bottom. Despite my efforts ...

Changed the form to become scrollable once it reaches a designated section during scrolling

Seeking assistance! I am currently working on a website using Bootstrap 5, and I have a registration form within the banner section, specifically within a row > col-md-5. The position of the form is fixed, but I want to make it scrollable once it reache ...

Retrieving all rows from a table using Laravel API and Vue.js

<template> <div class="container"> <div class="row mt-5 mb-3"> <div class="col-md-10"> <h3>Gallery</h3> </div> <div class="col-md-2"> <button class="btn btn-success" ...

I find myself grappling with the styling issue in MUI select

<FormControl> <Select labelId="unique-label" id="unique-id" value={1} className={styles.uniqueCustomSelectRoot} IconComponent={() => <MyUniqueIcon />} sx={{ " ...

When utilizing the ::first-letter pseudo-element on <sub> and <sup> tags

Why is the <sub> and <sup> not supporting the ::first-letter CSS pseudo-element? Any solutions? p:first-letter, sub:first-letter, sup:first-letter { color: red; font-weight: bold; } <p>This text contains <sub>subscript</su ...

What is the correct way to chain custom callback functions using .queue()?

I am currently grappling with the concept of chaining custom functions: My code snippet looks like this: show_loader(0, function() { open_box($target_open, event.value, '.wide-col', function() { hide_loader(function() { ...

Rails is being overwhelmed by an excessive number of icons being added by Bootstrap

I'm having an issue with adding a user icon to my header. When using Bootstrap, it seems to be adding more icons than I have specified in my code. Can anyone help me figure out what's going on? Thanks in advance! <ul class="nav pull-right"> ...