The image is failing to animate according to the PNG sequence function

Enhanced Functionality:

Upon clicking the "Tap Here" image button, a function called "GameStart()" is triggered. This function ensures that the main image "Star" descends down the page from the top through an animated sequence using png logic. The proposed animation includes a rotation between left and right to give the impression that the "Star" is descending down a rope.

Action Taken:

I have successfully implemented the function GameStart(), which executes the desired rotational movement of the star when the user taps on the "TAP HERE" image button.

Challenge Faced:

The png sequence does not display as intended while the star moves downward on the page. There are a total of 83 png files located within the folder named "TheStar".

What could possibly be causing this issue? Your assistance is greatly appreciated. I have included the code snippet for your review below.

function GameStart() {
  console.log("GameStart");
  $("#Tap").click(function() {
    x = document.getElementById('GameStar').offsetTop;
    if (x < bottomStarLimit) {
      for (var i = 0; i < 100; i++) {
        if (i >= 0 && i < 10) {
          $("#GameStar").attr("src", "lib/Elements/TheStar/Star_0000" + i + ".png");
          x = x + step;
          document.getElementById('GameStar').style.top = x + "px";
        }
        if (i >= 10 && i < 100) {
          $("#GameStar").attr("src", "lib/Elements/TheStar/Star_000" + i + ".png");
          x = x + step;
          document.getElementById('GameStar').style.top = x + "px";
        }
      }
    }

  })
}
#Tap {
  position: absolute;
  width: 600px;
  height: 650px;
  margin-top: 2100px;
  margin-left: 670px;
  outline: 0px;
  z-index: 2;
}
<div id="GamePage" style="width:100%; height:100%;z-index=1;">
  <img id="GameStar" style="position: absolute; top:-6.5em; left:500px; width: auto; height: 150px, z-index:1;" type="image" src="lib/Elements/TheStar/Star_00000.png">
  <input id="Tap" type="image" src="lib/Elements/Tap%20here%20button.png" onclick="GameStart()" />

</div>

Answer №1

It seems like there might be a small error in your code.

Within the click function $("#Tap").click(function()
You are currently using for loops to iterate through your code. This means that every time you click the "Tap Here" image button, all of your image files will be iterated through, and you may only end up seeing the last image in your folder.

Let me demonstrate how your code is currently functioning:

  • Click
  • Retrieve element
  • Check if at the bottom
  • Iterate through all elements
  • Add image to element
  • Increase counter
  • Increase distance on element

From what I understand, it seems like you are aiming to achieve the following:

  • Retrieve element
  • Upon clicking
  • Iterate through
  • Add image to element
  • Increase distance on element

Here is some sample code that could guide you in the right direction:

var player1 = $('#character'); // our player sprite
var iteratecount = 0; // needs to be placed outside
$('#move').click(function() { // when the button is clicked
  iteratecount++; // equivalent to iteratecount = iteratecount + 1;
  player1.css('top', (iteratecount * 10) + 'px'); // multiplied by 10 for better visual appearance
});
#character {
  border-radius: 20px;
  background-color: firebrick;
  position: absolute;
  top: 0;
  width: 100px;
  height: 105px;
}
.game {
  position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="move">Move</button>
<div class="game">
  <img id="character" href="" />
</div>

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

Linking input to radio buttons in Vue.js

When creating an edit form page, I encountered an issue where the page was not loading properly. Before the page loaded, I ran the following code: beforeMount(){ if(this.$route.meta.mode === 'edit'){ this.initialize = '/api/arti ...

What is the method to determine the encoding that the user is using to input into the browser?

After reading Joel's insightful article about character sets, I have decided to follow his advice by using UTF-8 on both my web page and in my database. However, one thing that confuses me is how to handle user input. As Joel aptly points out, "It doe ...

Mishandling of string interpretation

I'm having trouble converting ANSI color codes from console output into HTML. While I discovered a script that can handle this task, I am struggling to make it parse the strings within node js properly. Even when I attempted to JSON.stringify it to in ...

Can the data retrieved from a jsonp call that may be considered "bad" be utilized effectively?

When making a JSONP call to another domain, I am receiving data that is in JSON format but not wrapped in a function. This causes a parse error to occur. However, the data is still retrievable and visible. Despite the error, is it possible to utilize the ...

Using jQuery to send legitimate JSON data in the request payload

After checking the jQuery Ajax documentation, it seems that by default, data gets serialized as a query string when sending requests. However, I learned that setting processData:false should allow me to send actual JSON in the request body. The problem is, ...

Angularjs still facing the routing issue with the hashtag symbol '#' in the URL

I have recently made changes to my index.html file and updated $locationProvider in my app.js. After clicking on the button, I noticed that it correctly routes me to localhost:20498/register. However, when manually entering this URL, I still encounter a 4 ...

What is the best way to align elements in relation to a central div container?

Is there a way to align two buttons in the center on the same line, and then position a selector to the right of those centered buttons? How can I achieve this arrangement? Essentially, how do you position an element in relation to a centered div? UPDATE: ...

Retrieve the JSON array embedded within the JSON string

Can someone help me extract the JSON array from a JSON string? I've been struggling to make it work. Here is the code snippet for reference: // I need assistance with this var all_barcodes = '{"VAM12345":{"colour":"red","size":"32"},"VAM456789" ...

The Ajax request functions flawlessly on Mozilla but encounters issues on Chrome, causing confusion as it occasionally works

I am using a PHP file with a class and function to store data in the database, accessed via AJAX. While everything works smoothly in Mozilla, Chrome seems to be causing some issues. Strangely, sometimes it works fine, but other times it fails for no appare ...

Are arrays being used in function parameters?

Can the following be achieved (or something similar): function a(foo, bar[x]){ //perform operations here } Appreciate it in advance. ...

What is the best way to embed two controllers within an AngularJS webpage?

Currently, I have a Web Forms ASP.NET website that I am trying to enhance by adding an AngularJS page. This page is meant to interact with my RESTful Web API to display quotes for selected securities upon button click. While the Web API calls work when dir ...

Regex produces odd results in string processing

This particular JavaScript regular expression: homework.description = (homework.input.match(/((\(((\S\s?)\)?)*)|(about( \w*)*))/i)); When applied to the text: potato (potato) Produces this unexpected output: (potato),(potato), ...

Is the username you want available?

I am facing a challenge in my registration form validation process where I need to verify the username input using javascript and flask in python, but the implementation is unclear to me. The requirement is to utilize $.get in the javascript segment along ...

Is there a difference between innerHTML strings and their corresponding strings in JavaScript?

I am facing an issue with a code snippet that seems to have an unusual error. The following code is a simplified version to demonstrate the strange behavior. <html> <head> <script type = "text/javascript"> window.onload = fun ...

Master br tag in HTML: a complete guide

I am currently struggling with understanding how to correctly use the line break tag in HTML. During a recent class test, we were tasked with identifying issues within an HTML script, and I mistakenly believed that < /br> was incorrect. I am unsure ...

Warning: Typescript is unable to locate the specified module, which may result

When it comes to importing an Icon, the following code is what I am currently using: import Icon from "!svg-react-loader?name=Icon!../images/svg/item-thumbnail.svg" When working in Visual Studio Code 1.25.1, a warning from tslint appears: [ts] Cannot ...

What is the best way to export my mongo schema to a file and then utilize it for inserting data?

I've been encountering difficulty when attempting to insert data into my collection. I'm not entirely sure if I'm doing it correctly, so I apologize for the vague request. Hopefully, by providing you with my code, you can help me understand ...

Having trouble with the functionality of the Simpletip plugin?

I seem to be at a loss here. I've been experimenting with Simpletip in an attempt to create a tooltip that shows up on click, specifically aiming at making it functional on mobile devices. Despite my efforts, I can't seem to make anything work. ...

When accessing req.param on the server side in a Node.js application, it returns undefined

I am currently utilizing nodejs and angularjs for my project. In the client-side javascript file, I made a GET request to the nodejs server with necessary data in parameters. itinerary.js $http({ method : "GET", url : '/createItinerary&apo ...