473,382 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Multiple instances of the same object interfere with each other

Hi all,
I'm new to the group, I looked for some threads about this problem, but
never managed to find a solution, thus here is my question:

I'm coding an Animation class to give web pages the ability to move,
tween, resize and change alpha to objects (say div, img, and other DOM
nodes), the problem arises when I try to instantiate a new animation on
a different object, if I instatiate a new animation on a different HTML
object, the object animated before stops to move (or resize or
whatever), I thought it could have to do with closures, but I can't
figure out how to handle this problem, here is the code (excerpts):

----------------------------------------------
Here is the code that instatiates objects.
The animationMoveTo method requires these parameters:
(endingXCoordinate, endingYCoordinate, millisecondsForTheAnimation,
indexForTheSetInterval, intervalForTheSetInterval).
As you can see, I call the Animation constructor two subsequent times,
in this case, I can't see the aquila div object to slide into the page,
but if I delay (say with a setTimout with interval 200), the
instatiation of the second Animation (the one with sinistro as
argument), I can see the first move for just 200ms then stop and start
the animation for "sinistro", another (frustrating) note: 'sinistro'
starts the animation from the would-be arriving coordinates of the
'aquila' object:

animazioneAquila = new Animation('aquila');
larghezza = animazioneAquila.cssValue.getWidth();
sinistra = animazioneAquila.cssValue.getLeft();
dove = sinistra - larghezza;
animazioneAquila.animationMoveTo(dove, "40%", 600, 0,
100);

animazioneSinistro = new Animation('sinistro');
animazioneSinistro.animationMoveTo(0, "40%", 200, 1, 100);
----------------------------------------------------------------------
Here are excerpts from the Animation and Css classes that are
referenced by the code:

function Animation(identificativo) {

var now = (new Date()).getTime();
eval('tmp' + now + ' = this');
eval('tmp' + now + '.cssValue = new Css(identificativo)');

// MOVE OBJECTS
this.animationMoveTo = animationMoveTo;

[...]

function moveBy (dx, dy, tx, ty, index) {
var newX = eval('tmp' + now + '.cssValue').getLeft() + dx;
var newY = eval('tmp' + now + '.cssValue').getTop() + dy;
eval('tmp' + now + '.cssValue').setStyle("left", newX);
eval('tmp' + now + '.cssValue').setStyle("top", newY);
if (dx * (tx - newX) >= 0 && dy * (ty - newY) >= 0) {
return 1;
}
eval('tmp' + now + '.cssValue').setStyle("left", tx);
eval('tmp' + now + '.cssValue').setStyle("top", ty);
clearInterval(moving[index]);
return 0;
}

move = moveBy;

function animationMoveTo(tx, ty, interval, index, fluidity) {
if (typeof tx == "string") tx = checkPercentage(tx, "w");
if (typeof ty == "string") ty = checkPercentage(ty, "h");
if (typeof moving != 'undefined') {
if (typeof moving[index] != 'undefined')
clearInterval(moving[index]);
} else moving = new Array();
var iterazioni = interval/fluidity;
var dx = (tx - eval('tmp' + now + '.cssValue').getLeft())/iterazioni;
var dy = (ty - eval('tmp' + now + '.cssValue').getTop())/iterazioni;
if (dx || dy) {
moving[index] = setInterval("move(" + dx + "," + dy + "," + tx + ","
+ ty + "," + index + ")", fluidity);
}
}

[...]

function Css(identificativo) {
this.id=identificativo;

var now = (new Date()).getTime();
eval('cssVar' + now + ' = this');

// Object POsition
this.getLeft = getLeft;
this.getTop = getTop;

[...]

function getLeft() {
obj = document.getElementById(eval('cssVar' + now + '.id'));
var curleft = 0;
if (obj.offsetParent) {
while (obj.offsetParent) {
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
}
else if (obj.x) curleft += obj.x;
return curleft;
}

function getTop() {
obj = document.getElementById(eval('cssVar' + now + '.id'));
var curtop = 0;
if (obj.offsetParent) {
while (obj.offsetParent) {
curtop+= obj.offsetTop
obj = obj.offsetParent;
}
}
else if (obj.y) curtop += obj.y;
return curtop;
}
[...]

---------------------------------------------------------------------------

Thank you for the help, and sorry for this long post, but I can't
imagine why instatiating 2 new objects (new means new, i thought) has
this interfered bahaviour.

Hedgehog

Jul 23 '05 #1
2 3099
Hedgehog wrote:

[snip]
[...] new means new, i thought [...]


It does, but you're not using the objects to contain data. Instead
you're creating global variables in a way which might cause conflicts
if two objects are constructed (almost) simultaneously.

Get rid of the eval rubbish and create members properly:

function Animation(id) {
this.cssValue = new CSS(id);
}

function CSS(id) {
this.getLeft = function() {
var o = document.getElementById(id),
l = 0;

if(o.offsetParent) {
while(o.offsetParent) {
l += o.offsetLeft;
o = o.offsetParent;
}
} else if(o.x) {l += o.x;}
return l;
};
this.getTop = function() {
var o = document.getElementById(id),
t = 0;

if(o.offsetParent) {
while(o.offsetParent) {
t += o.offsetTop;
o = o.offsetParent;
}
} else if(o.y) {t += o.y;}
return t;
};
}

It would seem that the methods of the Animation object should be added
via the prototype object as they don't necessarily seem to need to
form a closure within the constructor function. However, that
assessment might change based on the timer mechanism you use. That too
is based around global identifiers and it probably shouldn't be.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
Hi, it's me Hedgehog! ^_^

Thank you very much, with your help and the code you provided, I
understood how it has to work! And now everithing is ok! Thank you very
much!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
1
by: Vlad | last post by:
Is there any way to install multiple instances of the same windows service designed with VS.NET 2003? I tried copying the binaries into a separate folder and then copying registry entries for the...
11
by: Mike | last post by:
Looking to find any information on how to properly configure multiple instances of DB2. This is on Win2k db2 ver 7.2. I am basically looking for information on how the multiple instance settings...
9
by: Abhishek Srivastava | last post by:
Hello All, In IIS 6.0 We have a concept of worker processes and application pools. As I understand it, we can have multiple worker process per appliction pool. Each worker process is dedicated...
2
by: Tumurbaatar S. | last post by:
ASP.NET QuickStart Tutorial says that: .... ASP.NET maintains a pool of HttpApplication instances over the course of a Web application's lifetime. ASP.NET automatically assigns one of these...
2
by: Helen Trim | last post by:
I have an application with three forms that are msde visible and activated when needed. It uses Word to open documents and one of the forms is opened as the Word document is closed in the...
3
by: tyler.schlosser | last post by:
Hi there, I am trying to launch a program called AmiBroker using the command: AB = win32com.client.Dispatch("Broker.Application") However, I have a dual-core CPU and would like to launch two...
6
by: John Salerno | last post by:
My code is below. For now I'm focusing on the lines where health (and armor) are increased in each character class. Let's say I decided to change the amount of increase in the future. As it is now,...
5
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.