Reputation: 190
I have made the complete copy of 2048 game but i have moved by tiles by teleporting (no smoothness moving of tiles like it was in original game)
I have used the following code for smothness of moving tiles.
//GameManager script
void MoveRight () {
//some code ..
AnimateTileMovement (newPosition); // newposition is the position to whihc the tiles is going to move
//some code which i need to execute (ONLY AFTER MY COMPLETE MOVEMENT OF TILE)
// BUT AS SOON AS TileMovement return its first null this code execute which is creating a lot of problem , what should i do ?
//to stop execution these code until the tiles have moved towards its desired newPosition
}
//TilesMovement Script
public void AnimationTileMovement(Vector3 newPosition) {
StartCoroutine ("TileMovement", newPosition);
}
IEnumerator TileMovement(Vector3 newPosition) {
while (transform.position != newPosition) {
transform.position = Vector3.MoveTowards (transform.position, newPosition, speedTile * Time.deltaTime);
yield return null;
}
}
i have been spending days to achieve this but i am not able to do how to stop executing the code below StartCoroutine ("TileMovement", newPosition)
as the code gets executed at the very first movement when the IEnumerator TileMovement(Vector3 newPosition)
yeild it first null ?
I have read this Article and tried also but unable to do so please suggest me what to do Coroutines unity ask
Upvotes: 2
Views: 932
Reputation: 12598
Your problem is this simple,
ONLY AFTER MY COMPLETE MOVEMENT OF TILE...
and
i have been spending days to achieve this but i am not able to do how to stop executing the code ...
if you want to start a coroutine, but keep going....
Debug.Log("we're at A");
StartCoroutine("ShowExplosions");
Debug.Log("we're at B");
that will print "A", and start the explosions animation, but them immediately keep going and print "B". (It will print "A" then immediately print "B", and the explosions will start running at the same time as "B" is printed and then keep going.)
however ....
if you want to start a coroutine, and wait...
Debug.Log("we're at A");
yield return StartCoroutine("ShowExplosions");
Debug.Log("we're at B");
That will print "A". It will then start the explosions and it will wait: it will do nothing until the explosions are finished. Only then it will print "B".
This is a basic mistake often seen in Unity.
Don't forget that "yield return"!
of course, this code
Debug.Log("we're at A");
yield return StartCoroutine("ShowExplosions");
Debug.Log("we're at B");
must itself be in a coroutine. So you'd have something like...
public IEnumerator AnimationTileMovement(...)
{
Debug.Log("we're at A");
.. your pre code
yield return StartCoroutine("ShowExplosions");
Debug.Log("we're at B");
.. your post code
}
and in MoveRight you'd have
public void MoveRight(...)
{
StartCoroutine( AnimateTileMovement(newPosition) );
makes sense?
Upvotes: 4
Reputation: 788
The source of 2048 shows that the author of the game uses CSS transitions to animate the movement of the tile. The .tile
class has the following attributes, among others:
.tile {
transition: 200ms ease-in-out;
transition-property: transform;
}
This means that, if the transform attribute of an element with a .tile
class changes, it is smoothly transitioned, causing the element to slide on your screen. You can learn more about CSS transitions here.
Upvotes: -1