- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用 CocosSharp 创建了一个本质上是 candy crush 的 rippof,这实际上是我第一次使用,C# 和 Xamarin 也是如此。为了制作动画,我使用提供的 MoveTo 方法,但我不确定在继续我的代码之前我应该如何等待动画完成。目前我正在使用 await Task.Delay() 结合 while 循环来完成这个,但是这个“感觉”并且看起来“脏”。我想知道等待动画完成的正确方法是什么?
这是我写的一些代码:
// Checks to see if a swap is possible, if it is then it will do so
// otherwise it will call for a failed swap animation
public async void trySwap(int horzDelta, int vertDelta, int fromRow, int fromCol)
{
//debugLabel.Text = "checking to see if a swap is possible.";
int toRow = fromRow + vertDelta;
int toCol = fromCol + horzDelta;
// Make sure that the user didn't swipe out of the grid as there isn't any candies to swap with out there
if (toRow < 0 || toRow >= gridRows)
{
return;
}
if (toCol < 0 || toCol >= gridColumns)
{
return;
}
candy toCandy = candyAt(toRow, toCol);
candy fromCandy = candyAt(fromRow, fromCol);
//debugLabel.Text = "Switching candy at [" + fromRow + ", " + fromCol + "] with candy at [" + toRow + ", " + toCol + "].";
Swap swap = new Swap();
swap.candyA = fromCandy;
swap.candyB = toCandy;
if (isSwapPossible(swap))
{
// Swap them
animateSwap(swap);
await Task.Delay(300); // Wait for the swap animation to finish before continuing
dropped = false; // Sets dropped to false, it will be used to check if the game finished dropping all of the candies
filledAgain = false;
finishedRemoving = false;
do
{
// My reason to add the while loops with the awaits is that the App seems to come back to this do while even before the
// the method completely finish running. I'm guessing that awaits in the methods is forcing the App to continue running while it's awaiting
// to continue within the method. It's possible that the called methods are running on a separate threads from the thread that is running this
// method, so while those threads are put on hold, the App jumps back to this thread. After putting in while loops the app does seems to work like
// I want it to so I'm probably on the right track, thought there must be a better way to accomplish as the current way looks ugly.
removeMatches(); // Remove the matches
while (!finishedRemoving)
{
await Task.Delay(50);
}
dropCandies(); // Move the candies down
while (!dropped) // As long as the dropCandies method isn't finished it will keep adding an await
{
await Task.Delay(50);
}
fillUpColumns(); // Fill the grid back up
while (!filledAgain)
{
await Task.Delay(50);
}
detectPossibleSwap(); // Need to update the list of possible swaps
await Task.Delay(300);
}
while (deleteChains.Count != 0);
decrementMoves();
// In the case that grid ends up with no possible swaps, we need to refill the grid new candies
if (possibleSwaps.Count == 0 && movesLeft != 0)
{
reshuffle();
while (!doneShuffling)
{
await Task.Delay(50);
}
}
}
else
{
// failedSwapAnimation only needs to run if there's valid candies
if (swap.candyA != null && swap.candyB != null)
{
// Swap is not possible so run the failed swap animation
failedSwapAnimation(swap);
// Waiting to make sure the animation has been completed
await Task.Delay(300);
}
else
{
// The method enables the user interaction again and returns the call point without any type of animation
// as the user tried to do a swap with an empty location
enableListeners();
return;
}
}
// Turn user interaction back on as all of the matches were removed and the grid filled back up
if (movesLeft != 0)
{
enableListeners();
}
}
下面是实际调用 MoveTo 方法的方法:
// Visually animates the swap using the CCMoveTo function provided by CocosSharp,
// also updates the grid location of the candies
private void animateSwap(Swap swap)
{
const float timeToTake = 0.3f; // in seconds
CCFiniteTimeAction coreAction = null;
// Store the positions of the candies to be used to swap them
CCPoint positionA = new CCPoint(swap.candyA.Position);
CCPoint positionB = new CCPoint(swap.candyB.Position);
// Animate the swapping of the candies
coreAction = new CCMoveTo(timeToTake, positionB);
swap.candyA.AddAction(coreAction);
coreAction = new CCMoveTo(timeToTake, positionA);
swap.candyB.AddAction(coreAction);
// Update the row and column positions for each candy
swap.candyA.setPosition(convertYToRow(positionB.Y), convertXToColumn(positionB.X));
swap.candyB.setPosition(convertYToRow(positionA.Y), convertXToColumn(positionA.X));
// Update the position of the candies within the grid
grid[swap.candyA.getRow(), swap.candyA.getColumn()] = swap.candyA;
grid[swap.candyB.getRow(), swap.candyB.getColumn()] = swap.candyB;
}
// Animation for a failed swap
private async void failedSwapAnimation(Swap swap)
{
const float timeToTake = 0.1f; // in seconds
CCFiniteTimeAction coreAction = null;
CCFiniteTimeAction secondAction = null;
// Store the positions of the candies to be used to swap them
CCPoint positionA = new CCPoint(swap.candyA.Position);
CCPoint positionB = new CCPoint(swap.candyB.Position);
// Animate moving the candies back and forth
coreAction = new CCMoveTo(timeToTake, positionB);
secondAction = new CCMoveTo(timeToTake, positionA);
swap.candyA.RunActions(coreAction, secondAction);
coreAction = new CCMoveTo(timeToTake, positionA);
secondAction = new CCMoveTo(timeToTake, positionB);
swap.candyB.RunActions(coreAction, secondAction);
// Wait for the animation to complete before moving on
await Task.Delay(300);
}
// Method to find all chains in the grid
private void removeMatches()
{
List<Chain> horizontalChains = detectHorizontalMatches();
List<Chain> verticalChains = detectVerticalMatches();
// Logic to remove the candies from the grid goes here, possibly call a method that takes the list of chains to work with
// Don't forget that candies have to be removed from the grid and then afterwards the sprites need to be removed from the screen separately
// which can be handle by another method
foreach (Chain item in verticalChains)
{
horizontalChains.Add(item);
}
deleteChains = horizontalChains;
removeCandies(horizontalChains);
}
// Remove the candy objects from the screen and the grid
private async void removeCandies(List<Chain> chains)
{
if (finishedRemoving != false)
{
finishedRemoving = false;
}
foreach (Chain chain in chains)
{
foreach (candy candy in chain.candies)
{
// Remove the candy from the grid
grid[candy.getRow(), candy.getColumn()] = null;
CCSprite removeCandy = candy.getSprite();
if (removeCandy != null)
{
const float timeToTake = 0.3f; // in seconds
CCFiniteTimeAction coreAction = null;
CCAction easing = null;
coreAction = new CCScaleTo(timeToTake, 0.3f);
easing = new CCEaseOut(coreAction, 0.1f);
removeCandy.RunAction(coreAction);
await Task.Delay(50); // Wait for the scaling animation to show a bit before continuing on to remove the candy
//pointGone = false;
//pointLabel(candy.getRow(), candy.getColumn());
//while (!pointGone)
//{
// await Task.Delay(1);
//}
removeCandy.RemoveFromParent(); // This should remove the candy from the screen
handlePoints();
}
}
// Wait for all of the candies to be removed before moving on to the next chain in the list of chains
await Task.Delay(300);
}
// Since the method is finished removing all of chains, needed to set the finishedRemoving bool variable to true
// so the calling method can get out of it's await loop
finishedRemoving = true;
}
// Drops the candies down
private async void dropCandies()
{
// Makes sure that dropped bool variable is set false before continuing
if (dropped != false)
{
dropped = false;
}
for (int col = 0; col < gridColumns; col++)
{
for (int row = 8; row > 0; row--)
{
if (level.tiles[row, col] == 1)
{
candy Candy = candyAt(row, col);
if (Candy == null)
{
// Find which row number to drop the candy from
int tempRow = row - 1;
while (tempRow >= 0 && grid[tempRow, col] == null)
{
tempRow--;
}
// Only runs if there's a row that has a candy in it
if (tempRow >= 0)
{
CCPoint position = new CCPoint(70 + (62 * col), 810 - (70 * row));
Candy = candyAt(tempRow, col);
Candy.AddAction(new CCEaseOut(new CCMoveTo(0.3f, position), 0.3f));
Candy.setPosition(row, col); // Update the row and column of the candy
grid[row, col] = Candy; // Update the position of the candy within the grid
grid[tempRow, col] = null;
// Wait for the candy to drop before moving to on the next candy
await Task.Delay(50);
}
}
}
}
}
// Since the method should have gone through the entire grid and finished dropping the candies
// need to set dropped to true so the calling method can get out of the await loop
dropped = true;
}
// Fill the holes at the top of the of each column
private void fillUpColumns()
{
int candyType = 0;
if (filledAgain != false)
{
filledAgain = false;
}
for (int col = 0; col < gridColumns; col++)
{
// Starting at the top and working downwards, add a new candy where it's needed
for (int row = 0; row < gridRows && grid[row, col] == null; row++)
{
if (level.tiles[row, col] == 1)
{
int newCandyType = 0;
// Have to first create a new candy outside of the while loop or otherwise the IDE won't let me use the variable newCandy
// as it will be seen as using an unassigned variable, even though it will be assigned a new candy in the while loop
candy newCandy = new candy(rand, row, col);
newCandyType = newCandy.getType();
// Make sure that each candy that is being added isn't the same as the one that was added previously
while (newCandyType == candyType)
{
newCandy = new candy(rand, row, col);
newCandyType = newCandy.getType();
}
candyType = newCandyType;
grid[row, col] = newCandy;
// Once all of the candy is created to fill the grid back up
// Use an animation to add it to the screen
animateAddingNewCandies(row, col);
}
}
}
// Since the entire grid was filled back up with candies, need to set the filledAgain bool variable to true
// so the calling method can get out the await loop
filledAgain = true;
}
对于那些想要查看完整代码以更好地理解问题的人,我可以在此处发布 github 链接。我现在不会包括它,因为我不确定这是否真的被允许。对一些评论感到抱歉,因为其中一些只是我写下我当时的想法。
最佳答案
我会使用带有 CCMoveTo 和 CCCallFunc 的序列
var moveAction = new CCMoveTo(1.0f, someplace);
var moveCompletedAction = new CCCallFunc(this.FunctionToCallWhenMoveIsComplete);
CCSequence mySequence = new CCSequence(moveAction, moveCompletedAction);
mysprite.RunAction(mySequence);
....
void FunctionToCallWhenMoveIsComplete ()
{
// run your post move code here
}
关于c# - 等待 MoveTo 操作完成的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38406000/
从 Redis 获取消息时,onDone:(){print('done')} 从未起作用。 import 'package:dartis/dartis.dart' as redis show PubS
昨天我玩了一些vim脚本,并设法通过循环来对当前输入的内容进行状态栏预测(请参见屏幕截图(灰色+黄色栏))。 问题是,我不记得我是怎么得到的,也找不到我用于该vim魔术的代码片段(我记得它很简单):它
我尝试加载 bash_completion在我的 bash (3.2.25) 中,它不起作用。没有消息等。我在我的 .bashrc 中使用了以下内容 if [ -f ~/.bash_completio
我正在尝试构建一个 bash 完成例程,它将建议命令行标志和合适的标志值。例如在下面 fstcompose 命令我想比赛套路先建议 compose_filter= 标志,然后建议来自 [alt_seq
当我尝试在重定向符号后完成路径时,bash 完成的行为就好像它仍在尝试在重定向之前完成命令的参数一样。 例如: dpkg -l > /med标签 通过在 /med 之后点击 Tab我希望它完成通往 /
我的类中有几个 CAKeyframeAnimation 对象。 他们都以 self 为代表。 在我的animationDidStop函数中,我如何知道调用来自哪里? 是否有任何变量可以传递给 CAKe
我有一个带有 NSDateFormatter 的 NSTextField。格式化程序接受“mm/dd/yy”。 可以自动补全日期吗?因此,用户可以输入“mm”,格式化程序将完成当前月份和年份。 最佳答
有一个解决方案可以使用以下方法完成 NSTextField : - (NSArray *)control:(NSControl *)control textView:(NSTextView *)tex
我正在阅读 Passport 的文档,我注意到 serialize()和 deserialize() done()被调用而不被返回。 但是,当使用 passport.use() 设置新策略时在回调函数
在 ubuntu 11.10 上的 Firefox 8.0 中,尽管 img.complete 为 false,但仍会调用 onload 函数 draw。我设法用 setTimeout hack 解决
假设我有两个与两个并行执行的计算相对应的 future 。我如何等到第一个 future 准备好?理想情况下,我正在寻找类似于Python asyncio's wait且参数为return_when=
我正在寻找一种 Java 7 数据结构,其行为类似于 java.util.Queue,并且还具有“最终项目已被删除”的概念。 例如,应可以表达如下概念: while(!endingQueue.isFi
这是一个简单的问题。 if ($('.dataTablePageList')) { 我想做的是执行一个 if 语句,该语句表示如果具有 dataTablesPageList 类的对象也具有 menu
我用replaceWith批量替换了许多div中的html。替换后,我使用 jTruncate 来截断文本。然而它不起作用,因为在执行时,replaceWith 还没有完成。 我尝试了回调技巧 ( H
有没有办法调用 javascript 表单 submit() 函数或 JQuery $.submit() 函数并确保它完成提交过程?具体来说,在一个表单中,我试图在一个 IFrame 中提交一个表单。
我有以下方法: function animatePortfolio(fadeElement) { fadeElement.children('article').each(function(i
我刚刚开始使用 AndEngine, 我正在像这样移动 Sprite : if(pValueY < 0 && !jumping) { jumping =
我正在使用 asynctask 来执行冗长的操作,例如数据库读取。我想开始一个新 Activity 并在所有异步任务完成后呈现其内容。实现这一目标的最佳方法是什么? 我知道 onPostExecute
我有一个脚本需要命令名称和该命令的参数作为参数。 所以我想编写一个完成函数来完成命令的名称并完成该命令的参数。 所以我可以这样完成命令的名称 if [[ "$COMP_CWORD" == 1 ]];
我的应用程序有一个相当奇怪的行为。我在 BOOT_COMPLETE 之后启动我的应用程序,因此在我启动设备后它是可见的。 GUI 响应迅速,一切正常,直到我调用 finish(),按下按钮时,什么都没
我是一名优秀的程序员,十分优秀!