Best way to Pause/Resume dozens of animations? - c#

Let's say my app is running dozens of animations simultaneously and I want to pause them all as if this were a game and the user hit the pause button. What's the best way to do that? I'm using a combination of CAKeyFrameAnimations, UIView.Animate(), CABasicAniamtions, etc. Obviously I could manually stop them all when the user hits pause, and resume them from where they stopped when the user un-pauses.. But is there a better way to do this?
Edit: I want to be able to Pause & Resume via user pressing a button.
2nd Edit: I got like 90% of the way there, but I'm still having a problem...
Here's my Xamarin iOS project:
https://www.dropbox.com/s/5zbhhao6cijtb1i/Test_Pause.zip
Useful Apple reference:
https://developer.apple.com/library/ios/qa/qa1673/_index.html
I have an app that starts an position animation. When I pause it the first time, it pauses correctly. When I hit resume, it resumes correctly from right where it was paused. But when you hit pause again a 2nd time (during the same animation) it resumes at a position as if it had never paused to begin with. It pauses way ahead of where it should be. The desired result of course is to have it halt exactly where it is when you hit the pause button.
Sorry if this sounds confusing, the example project i've attached is really simple, to reproduce the problem, download my app from the link above. Run it. Make sure to Pause, Resume, then Pause again all within the same animation. Any idea what I'm doing wrong?

you can use a Boolean at the beginning of the drawing method, if the boolean is true (means no animation ) then it goes directly to the end

You can use a CAAnimationGroup, which is a subclass of CAAnimation, and manipulates its component Animations as a group (I'm not sure if they're strictly synchronized, but I think they're close enough for visual purposes).

I can now successfully pause and resume animations with CAMediaTiming, all without having to Stop the aniamtion and change the duration and start it again. See this useful resource
Class Level Variables:
UIView MyImage;
double timeSincePause = 0;
double pauseTime = 0;
Pause Function:
MyImage.Layer.Speed = 0.0f;
MyImage.Layer.TimeOffset = MyImage.Layer.ConvertTimeFromLayer(CAAnimation.CurrentMediaTime(), MyImage.Layer) - timeSincePause;
Resume Function:
pauseTime = MyImage.Layer.TimeOffset;
MyImage.Layer.Speed = 1.0f;
MyImage.Layer.TimeOffset = 0.0f;
MyImage.Layer.BeginTime = 0.0f;
timeSincePause = (MyImage.Layer.ConvertTimeFromLayer(CAAnimation.CurrentMediaTime(), MyImage.Layer) - pauseTime);
MyImage.Layer.BeginTime = timeSincePause;

Related

Prohibit WindowsMediaPlayer wmmpsMediaEnded event

I have C# program in which I use WMPLib WindowsMediaPlayer. When playing goes to an end, it goes to these states:
wmppsMediaEnded --> wmppsTransitioning --> wmppsStopped.
By going to this chain of states, WindowsMediaPlayer.controls.currentPosition sets to 0;
So when player goes to an end, it stops playing and when I click play button, it starts playing from the start again.
I want player not to go into wmppsMediaEnded state and therefore not to start from beginning afterwards.
Is it possible?
I expected that WindowsMediaPlayer should have property, that prohibits to go to the wmppsMediaEnded state when playing ends, but it does not.
I was able to solve the problem. When player goes into wmppsStopped state, I set WindowsMediaPlayer.controls.currentPosition = WmpInstance.currentMedia.duration;
So when playing is finished, player position stays at the end, even though it goes into stopped state.

How to play animation just once whenever i shoot a bullet ( flash animation)

(check image) Hi! I actually have two questions,
THE FIRST ONE: I have two flash animations that I want to play in front of the tank's turret whenever I shoot a bullet (I have different weapons with different fire delays, machine gun, bombs..) so how can I play that flash animation to work in with the fire delay of a gun ? (note that if I uncheck loop time, the animation stops in the last sprite + I don't know how to replay it).
SECOND QUESTION: I want simply to animate my turret when I shoot I just want it to go back a little and return, you know what I mean, only in unity, how, please?
give me just the idea of working and I'll do my research :) thank you!
I have tried to enable my animation when I shoot and uncheck the loop time, but the animation stops at the last animation sprite and I don't know how to replay it.
Using Unity's built in animator, I'm assuming you are using this, you need to have at least have 2 states, in your case Idle and Fire. You would then set the transitions of the animations to go from one another, Fire -> Idle and Idle -> Fire.
Within the Idle-> Fire you will need add a trigger due to it only happening once.
You may also need 2 SpriteRenderer Components to allow for the display of the Shot effect, just make a child GameObject with it and edit it in the Animation Window.
Triggers reset as soon as the transition has been made.
If you do not have a trigger go to the parameter tab and click the '+' button which will allow you to create a new trigger. Now that that is created, in your code whenever you want your tank / boat to fire just add. referenceToAnimator.SetTrigger ("triggerName");
Bringing up your delay you could always add an additional wait state that mirrors the idle state but only runs for the selected amount of time. Your new Animator would look like.
Idle -> Trigger -> Wait -> Fire -> Idle

Unity Animation: Can I Skip/Forward the played animation by touch?

Halo guys, I'm literally new to C# on Unity. Can I ask some scripts if I may to you all. The case is somethings like this: I have an animation that played some sprites on a scene. I want to know if I can skip / forward the animation by touch using C# scripts placed on Empty Game Object. So the animation can move forward for 1 second / 2 seconds if the user touch the screen, so the animation more faster done playing or the user doesn't need to wait the animation that played to be finish.
I just think I may build that function with for statement and placed on void update. Please correct me if I'm wrong, and if I may put some code on you'r thought. Many thanks and cheers :).

Activate and Deactivate Invoke function

I attempted to give a delay to my function in Unity using C# language. By using Invoke, I achieved to add delay when the cursor is moved to a certain area. However, now I need to cancel the Invoke when the cursor is moved from that certain area. The idea is, when I accidentally move the cursor to the area, I can cancel whatever function being invoked there.
Here's my current code.
void SceneCall(){
Application.LoadLevel (MainMenu);
}
void SceneCallIn(){
Invoke ("SceneCall", 3f);
}
if (_newGameButton.Contains (Event.current.mousePosition)) {
SceneCallIn();
}
I tried to use CancelInvoke() as else, and it even doesn't want to invoke anymore when I move my cursor to _newGameButton afterwards. How do you cancel an Invoke then activate it once again? Thank you for your answer~
EDIT: Like #Programmer says in comments, what I exactly want to do is when my mouse is position at a certain point, timer will start counting. After 3 seconds
MainMenu scene will be loaded. While the timer is counting and the pointer is moved away from that certain position, I want the timer to stop and I don't want the MainMenu scene to be loaded anymore.
If you truly want to have "rollovers" and so on, here is a full tutorial:
https://stackoverflow.com/a/36046495/294884
Really you will have to become totally expert in all that, before you can do it.
I must tell you again though:
For 15 years now, you should never use rollovers for any reason in interface.
I faced the same problem yesterday, but I solved simply calling again the invoke method.
Here is my code:
public void OnGazeEnter()
{
InvokeRepeating("OnGazeStay", 0.0f, 1.0f / 30.0f);
}
public void OnGazeExit()
{
CancelInvoke("OnGazeStay");
}
In your case, if you want to register more than one method, you will have to save some sort of List<String> myMethods, so you will be able to delete every method that you've registered.

Wpf:MediaElement pause & play again disables speed ratio

I have a mediaElement. I change its speedratio according to my will 1x,2x,3x... It works the very first time but when I pause and play again it simply does not work. It starts playing in 1x speed. Is there anything I am missing?
I have encountered the same problem.
After some experimentation I found setting the SpeedRatio back to 1.0 before calling Play() again then waiting for the Position to change (typically using a timer) and setting SpeedRatio back to the old value seems to work.

Categories