Guide on creating incremental movement for an object using three.js and tween.js

I am trying to create a scenario where my cylindrical objects (referred to as pions in the code) move step by step towards a specified object on a board of my design. Here is the code snippet I have:

a: starting point
index: target object

attachEvent(_this, index,a) {
if(index>=a){

_this.domEvents.addEventListener(_this.cases[index], 'click',        
function(event) {
for ( var i = a; i <= index; i++) {
  _this.stepByStep(_this,i)
}
}, false)
}else{

_this.domEvents.addEventListener(_this.cases[index], 'click',        
function(event) {
//no action for now
}, false)
}

}

stepByStep(_this,i)
{
_this.tween = new TWEEN.Tween(this.pions.position)
        .to({
        x:  _this.cases[i].position.x,
        y: _this.cases[i].position.y,
        z: 5},1000)
        .start();
}

Despite implementing the code, when I test it out, the pions seem to move directly to the target object without moving step by step as intended.

Answer №1

Although I'm not familiar with Tween.js (I prefer using TweenJS), I believe the usage is similar.

It seems like in your for loop, you are creating new tweens each iteration instead of adding new positions. This results in a bunch of separate tweens that affect the same object and run concurrently, with the last tween being applied.

UPDATE It appears that this is how Tween.js operates? Only one to() call can be made for a tween. My original solution is suitable for TweenJS but not for Tween.js, which your example utilizes:

ORIGINAL SOLUTION APplies TO CREATEJS/TWEENJS ONLY:

Create the Tween once and add the to() calls within the loop:

var tween = new createjs.Tween(this.pions.position);
for ( var i = a; i <= index; i++) {
  _this.stepByStep(tween, _this.cases[i].position); // Adjusted arguments!
}

function stepByStep(tween, position)
{
    tween.to({
        x: position.x,
        y: position.y,
        z: 5
    }, 1000);
}

This approach creates a single tween and includes to() calls for each position. I also simplified the arguments for cleaner code. Feel free to refactor as needed.


UPDATED APPROACH

In Tween.js, it seems necessary to create a new tween for each animation. However, rather than generating them all at once, you should chain them and commence with the first tween only.

Here's some pseudo-code:

var firstTween = new TWEEN.Tween(obj)
var earlierTween = firstTween;
(loop) {
  var tween = new TWEEN.Tween(obj);
  tween.to({params});
  earlierTween.chain(tween);
  earlierTween = tween;
}
firstTween.start();

Check out this quick fiddle I put together. The drawing layer utilizes EaselJS while the tweening utilizes Tween.js. https://jsfiddle.net/0mh6kj5y/8/

I also noticed that you used this.pions in your function, whereas everywhere else uses _this for scope.

I hope this information proves helpful!

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

Ways to deactivate dates that come after today's date

Having a bit of trouble with my datepickers in two different views. I have written code to disable dates after the current date, and it works for the first view but not the second. This code snippet works: var yesterday = new Date(); yesterday.setTime(ye ...

Steps to sending a request with a custom user agent

In my Angular app, I have successfully implemented server-side pre-rendering with the condition that it will only pre-render if a search bot is sending the request. Now, I need to verify if everything is pre-rendered correctly. However, when I visit my w ...

Prevent event listeners from being triggered during the '$destroy' event. Error: $on function is undefined

As I attempt to halt all event listeners when the scope is destroyed, a certain error arises. The following error is displayed: TypeError: vm.$on is not a function; Even using vm.on(..) does not resolve the issue angular.module('app.layout&apo ...

What are the steps to launch a Next.js application on cPanel hosting?

Currently, I am faced with the task of deploying a Next.js application on cPanel. Node and npm have been successfully installed. Could you kindly guide me on how to deploy the Next.js app using this configuration? In my attempt to build the app in the cPa ...

Using ng-repeat in conjunction with the OpenWeatherMap API to search for and display cities

Hey everyone, I've been attempting to organize data retrieved from openweathermap.org using the provided API and AngularJS's ng-repeat feature. Unfortunately, I am facing some challenges in making it work properly. My goal is to initially display ...

Retrieve the downloaded status of a blob within a specific function

I need a way to verify if the blob downloading process is finished when generating an excel file on the NodeJS server side in response to a request from the React frontend side. Is there a method or promise that can help me achieve this? I want to update t ...

The value is undefined until a new Resource object is pushed with the item

I've encountered an issue while testing a factory and I can't seem to find anyone else with the same problem. Can someone help me figure out what's causing this strange error? TypeError: 'undefined' is not a function (evaluating & ...

"Getting an 'Undefined index' error while accessing a JavaScript variable in PHP

Every row in my table contains an Edit button. I managed to fetch the row number by clicking on the Edit button using JavaScript, but I am unsure how to do it in PHP. My attempt to pass the variable from JS to PHP resulted in an error: Undefined index ...

nodemon is launching an endless number of .node-xmlhttprequest-sync files

I am facing a strange issue with my app that imports a module containing a promise. Everything runs smoothly when I start the app using "node app.js" However, if I use "nodemon" to launch it, it constantly creates files with names like .node-xmlhttpreque ...

Preventing users from selecting past dates in HTML input date field

I need help with disabling past dates in an HTML date input field. Even though I am able to restrict selecting past dates on the date picker, I can still save data if I manually type in a previous date (e.g., 12/08/2020). $(document).ready(function() { ...

Encountering difficulties while attempting to deploy NodeJS application on Heroku

When attempting to deploy my nodejs app, I encountered the following error: 2019-11-25T18:16:16.927748+00:00 app[web.1]: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9a8a7a6a7b0a4a6bcbae4afa6bbbca4e4ada0baaabcbabaa0a6a ...

Include a link to a JavaScript file within a dynamically created HTML page in a Delphi VCL application

I am currently developing a Delphi XE5 VCL Forms Application which includes a TIdHTTPServer on the main form. Within this server, there is a CommandGet procedure called IdHTTPServer: procedure TForm1.IdHTTPServerCommandGet(AContext: TIdContext; ARequest ...

Experiencing difficulties with a JavaScript loading issue where images are not loading properly

I recently downloaded a calendar JavaScript file and placed it in the directory /user/js/calendar. In my HTML file located at /user/, I added the following code: <script language="JavaScript" src="js/calendar/calendar_us.js"></script> <link ...

At times, the content in a JQuery dialog box may display outdated information

On my webpage, I have multiple buttons where each button contains specific data. When a button is clicked, it opens a dialog box with two input fields that are date types. If any changes are made, they should be saved for that particular button. The issue ...

The function that was passed to readFileSync anonymously is failing to return any data

Recently, I created a basic JavaScript object with the csvFileToArray function designed to return a parsed CSV array. However, I've encountered an issue with receiving output from the anonymous function passed to readFileSync. While test1 is success ...

Extracting data on an AngularJS platform by using web scraping techniques

I have been working on an AngularJS application that currently retrieves JSON data from an API using http.get, and it's been working really well. Recently, I've been exploring the idea of passing a URL to a static webpage and scraping the result ...

Displaying current location on Google maps with generated code and adding a new marker by clicking in PHP: Firefox

I am currently working on a code that will display a marker at the current location and also add a new marker whenever the screen is touched or clicked. I have been trying to make it work with the code I found online, but it has been a bit of a challenge ...

VueJS Element Library's date picker feature returns dateTime values with their corresponding time zones attached

I'm currently utilizing a date picker from The expected format for the date should be: yyyy-MM-dd However, the actual returned date format is: Thu Apr 20 2017 00:00:00 GMT+0530 (India Standard Time) This is the code snippet I've used: & ...

ng-model based on various scenarios

I have two distinct arrays defined as : var oldarr = ['one','two','three']; var newarr = ['four','five','six']; var condi = 1 / 0; I am using the array values as ng-model options in a select d ...

Surprising Outcomes of Negative Margin in jQuery Animation

Unique Project Summary I am currently working on a website that incorporates a sliding menu feature. I have successfully implemented this functionality, and the display remains consistent during the animation transitions for sliding the menu in and out. T ...