I use the BGA framework stock items in my code.
So a common action to do is 'transfer' a card from one stock item to another, something easy like this:
But now to amimate this behaviour I need to use 'slideToObject', something like:
So the problem exists if you combine the stock transfer with the slideToObject. When should the silde occur..?
stFrom.removeFromStockById(idCard); At some point the card doesn't exist and then animation won't work.
If you add in to a stock and then slide it, again the card will exist first then slide to the destination, even worse.
So all-in-all, what seemed like a simple combination doesn't work. I've looked in a few code bases and I don't see complete solution.
So....
1. What about using, setTimeout(function()
{
this.playedStack.addToStockWithId(uniqueId, card.id);
}, 500 + (150 * ii));
1A. Well it may work on the surface, but the callback relies on the parameters not changing. Call this code snippet 2 times or more are you'll find uniqueId, card.id do not change. For a one-time call, this will work OK.
But also, hoping future timed events will occur together smoothly doesn't sound very robust.
2. Do the animation really quick.
2A. Yes this sometimes worked. I didn't look into this too deeply, but sometimes I moved code around and nothing changed. Mostly, not surprisingly, the animation doesn't even occur.
3. Hook into the animate beforeBegin and onEnd events.
3A. This is the best I could come up with, and it works quite well:
There are a few things of note:
If the 'card' does not yet exist, then 'tempcard' a css defined background image of a card is used.
The 2 attributes idUnique, idCard are required to be embedded in the card <div>, and then pulled out in the callback.
If there is extra stuff to do than just these before and after events then you're going to have to deal with this too.
For example my hack: if (stTo == this.playerHand) this.adjustPlayHand();
--------
So all-in-all this is quite a long-winded solution to what appears to be quite a simple problem, I have no idea if I've totally missed the point.
The problem after all of this though remains that the animation still doesn't work that well.
The slide goes to the middle of the target stock container <div>.
The stock items still get 'animated' re-arranged at the same time.
Lastly, and I have no reason why, the slide doesn't work to one of my stock items. The card slides underneath the destination parent <div>. Everything is css position: relative.
So a common action to do is 'transfer' a card from one stock item to another, something easy like this:
Code: Select all
stFrom.removeFromStockById(idCard);
stTo.addToStockWithId(idUnique, idCard);
Code: Select all
this.slideToObject(cardTemp, target, 500, 0).play();
stFrom.removeFromStockById(idCard); At some point the card doesn't exist and then animation won't work.
If you add in to a stock and then slide it, again the card will exist first then slide to the destination, even worse.
So all-in-all, what seemed like a simple combination doesn't work. I've looked in a few code bases and I don't see complete solution.
So....
1. What about using, setTimeout(function()
{
this.playedStack.addToStockWithId(uniqueId, card.id);
}, 500 + (150 * ii));
1A. Well it may work on the surface, but the callback relies on the parameters not changing. Call this code snippet 2 times or more are you'll find uniqueId, card.id do not change. For a one-time call, this will work OK.
But also, hoping future timed events will occur together smoothly doesn't sound very robust.
2. Do the animation really quick.
2A. Yes this sometimes worked. I didn't look into this too deeply, but sometimes I moved code around and nothing changed. Mostly, not surprisingly, the animation doesn't even occur.
3. Hook into the animate beforeBegin and onEnd events.
3A. This is the best I could come up with, and it works quite well:
Code: Select all
animateStockTransfer: function (ii, uniqueId, cardid, mobile, target, duration, delay, stFrom, stTo)
{
var cardTemp = dojo.clone($(mobile + '_item_' + cardid));
if (!cardTemp)
{
// use a card background.
cardTemp = dojo.clone($('tempcard'));
dojo.place(cardTemp, mobile);
dojo.attr(cardTemp, "class", "tempcard");
}
dojo.attr(cardTemp, "id", "card-temp-" + ii);
dojo.attr(cardTemp, "idUnique", uniqueId);
dojo.attr(cardTemp, "idCard", cardid);
dojo.attr(cardTemp, "z-index", "99");
dojo.place(cardTemp, mobile);
var anim = this.slideToObject(cardTemp, target, duration, ii * delay);
if (stFrom != null)
{
dojo.connect( anim, 'beforeBegin', this, function( node ) {
var idCard = dojo.attr(node, "idCard");
stFrom.removeFromStockById(idCard);
} );
}
dojo.connect( anim, 'onEnd', this, function( node ) {
var idUnique = dojo.attr(node, "idUnique");
var idCard = dojo.attr(node, "idCard");
if (stTo != null)
stTo.addToStockWithId(idUnique, idCard);
if (stTo == this.playerHand)
this.adjustPlayHand();
dojo.destroy(node);
} );
anim.play();
},
There are a few things of note:
If the 'card' does not yet exist, then 'tempcard' a css defined background image of a card is used.
The 2 attributes idUnique, idCard are required to be embedded in the card <div>, and then pulled out in the callback.
If there is extra stuff to do than just these before and after events then you're going to have to deal with this too.
For example my hack: if (stTo == this.playerHand) this.adjustPlayHand();
--------
So all-in-all this is quite a long-winded solution to what appears to be quite a simple problem, I have no idea if I've totally missed the point.
The problem after all of this though remains that the animation still doesn't work that well.
The slide goes to the middle of the target stock container <div>.
The stock items still get 'animated' re-arranged at the same time.
Lastly, and I have no reason why, the slide doesn't work to one of my stock items. The card slides underneath the destination parent <div>. Everything is css position: relative.