Stop the sprite on last frame - game-maker-studio-2

So I have this serious problem and I don't know how to let it work. I have this in my step event:
if (image_index >= 6 && image_index <= 8) && (sprite_index == spr_sonia_down_right) {
image_speed = 0;
image_index = 7;
}
The thing that I want is that on the last frame the sprite stops. I have an object and in that object I put the sprites or I change them using sprite_index and that works, but my sprite "down" has a secuence that she is going down and down and down. When she is complete down, that is in the 8 sub image, I want to freeze the sprite on that frame, but I can't reach that.

I think there are two things that you can do to improve this:
Instead of guessing through (image_index >= 6 && image_index <= 8), you can also use the Animation End Event. This Event happens when the object's current sprite has finished it's animation.
I would've made a seperate single sprite that shows the last sprite of the animation, (for example spr_sonia_down_end_right) and show that sprite once the animation is done.
So, for example in Animation End:
if (sprite_index == spr_sonia_down_right) {
sprite_index = spr_sonia_down_end_right;
}

Related

Multiple overlapping animations on the same element in svg.js

I want to start a animation on an element while a previous animation is still active. However, calling animate() on the element queues the new animation at the end of the current animation.
For example, consider an animation where an element is being moved to a new position. Now, I also want to make it fade out when it reaches a certain position. The following queues the “once” animation at the end of the move, rather than at 80%.
rect.animate(1000).move(100, 100)
.once(0.8, function(pos, eased) {
rect.animate(200).opacity(0);
});
How do I make the element start fading out when it reaches 80% of the move? The API seems to be designed for chaining animations rather simultaneous overlapping animations.
What you are trying to do is a bit more complicated. Unforrtunately its not possible to "hack" into the current animation and add a new animation on the fly.
However what you can do is adding a new property which should be animated:
var fx = rect.animate(1000).move(100, 100)
.once(0.8, function(pos, eased) {
fx.opacity(0);
});
As you will notice that has its own problems because the oopacity immediately jumps to 80%. So this is not an approach which works for you.
Next try: Use the during method:
var morh = SVG.morph(
var fx = rect.animate(1000).move(100, 100)
.once(0.8, function(pos, eased) {
fx.during(function(pos, morphFn, easedPos) {
pos = (pos - 0.8) / 0.2
this.target().opacity(pos)
}
});
We just calculate the opacity ourselves

How to Animate Sprites in Gamemaker

I would like to make my sprite stand still when not moving and animate when keys are pressed. I have a sprite with 8 images and an object that I have created where I assigned the sprite. I have added a key press and key release command and I have added a section where you can put your own custom code. However when I play the game the sprite is animating right away until I press a key and then he stops animating and moves in a still position. How can I fix this?
if image_index = 0
{
image_speed = 0;
}
else
{
image_index = 1;
image_speed = 1;
}
Your answer is in what you posted.
So in other words. If your going to do it this way. Why not just use 3 sprites one for stand one for left and one for right?
of if you insist on not doing that then just set image_index to 0 to start so

How to make an animation infinite loop using CoffeeScript in Framer.js?

I am trying to make a simple pulsing animation of a on-screen element in Framer.js and now it looks like this:
element.animate
properties: scale:1.3
element.on Events.AnimationEnd,->
this.scale = 1
locationUn.animate
properties: scale:1.3
basically when the screen is loaded, the element will be enlarged and upon end of the animation, it's forced back to scale 1 and run the animation again; but this solution is not elegant and animation seems very abrupt.
I am new to CoffeeScript...is there anyway to write a infinite loop to check on some condition like this?
checker = true
while(checker == true){
run animation
if(some events occurs){
checker = false
}
}
....
How to realize this in CoffeeScript?
You can create a looping pulse like this:
circle = new Layer()
circle.style.borderRadius = "100%"
circle.backgroundColor = "white"
circle.center()
outwards = new Animation({
layer: circle,
properties: {scale: 1.3},
curve: "ease-in-out"
})
inwards = outwards.reverse()
outwards.on(Events.AnimationEnd, inwards.start)
inwards.on(Events.AnimationEnd, outwards.start)
outwards.start()

Starting a CAKeyframeAnimation from a non-zero time

I have a sub-layer keyframe animation defined to animate an image along a BSpline, grouped together with a rotate animation. With the sub-layer.speed set to 0, I can drag the image back and forth along the curve by adjusting the animationsGroup.timeOffset value based on the distance dragged.
What I want to do is, after a certain threshold (say %15) is to set the animation speed to 1 so the animation completes by itself, but it's not that simple. The animation either completes immediately and resets everything back to the start position, or the animiation reaches the end of the path, loops round to zero, and continues animating until it's back to the point where the animation kicks in.
What I want is:
Tstart -> drag -> T0.15 -> animation -> Tend
But what I'm getting is
Tstart -> drag -> T0.15 -> animation -> Tend -> Tstart -> T0.15
I've already looked into use of timeOffset and time-warps, and fiddled with the parameters but to no avail.
You can that outcome by wrapping your animation group in a new animation group whose duration is 0.85 times the animation groups duration. Here is a bit of code that does just that:
- (void) setTimeOffset:(double)value
{
if (value < 0.15) {
// This is what you are already doing.
_animGroup.timeOffset = value;
[_someSublayer addAnimation:_animGroup forKey:#"animGroup"];
} else if (_animGroup.speed == 0.0) {
// You must set the final position before animating otherwise the
// layer will come back to it's original position.
_someSublayer.position = _theEndPointOfYourPath;
// Now set the speed of the animation group to 1 so that it animates.
// Be sure to leave the timeOffset alone so that is starts where you want it.
_animGroup.speed = 1.0;
// Create a new animation group around it whose duration is the remaining time
// of the animation group and add that to the layer instead. It will prevent
// animGroup from wrapping.
CAAnimationGroup *outerGroup = [[CAAnimationGroup alloc] init];
outerGroup.animations = #[_animGroup];
outerGroup.duration = _animGroup.duration - _animGroup.timeOffset;
[_someSublayer addAnimation:outerGroup forKey:#"animGroup"];
}
}

Unity 3D Spinning a gameobject

I am trying to spin a 3D Gameobject in unity. It is actually a cylinder with just a texture of a poker chip on it. I want to spin it 360 degrees once it has collided with a raycast. It is working just fine in the Unity emulator, however, on the device itself the chip comes to a stop after its spin and then continues to spin in an endless loop. Here is a snippet of the code in question. Thank you for any help in advance.
// Spin the chip
if (Animate) {
if (Speed > 0 && Chip.tag.Contains("Chip")) {
Chip.transform.Rotate(0, Speed*Time.deltaTime, 0);
Speed -= 3;
Debug.Log(Speed);
}
else {
// Reset
Animate = false;
Speed = 360;
Chip.transform.localRotation = Quaternion.Euler(0.0,0.0,0.0);
}
}
To summorize this the best I can the gameobject Chip is assigned when it collides on raycast as such
// Set the chip
Chip = hit.transform;
Everything is done in the update function. Once the raycast hits it calls a betting function then after the betting is calculated it changes the Boolean Animate to true causing the spinning of the chip.
Something is setting Animate = true in some other code, hard to tell whats going on without seeing the rest of it.
Put some debug next to every spot where Animate is set to true, you should see something else setting it, only possible explanation as to why it continues to spin.
Another option is to use the Animation tool and instead of rotating, you just play the animation which performs the rotation for you.
Edit: Chances are its around the touch code, cause when you debug in the editor your using key strokes. A gotcha I've experienced a few times.
James Gramosli is correct in that some other code is triggering the animation again and it is most likely your touch code. It is a common problem when moving between editor and a touch-enabled device. You can determine if this is the case by using the UnityRemote to verify the control flow of your code.
That said, I would change your code to the following which removes the spin code from the Update loop that runs every frame. It is a small optimization, but primarily it cleans up the architecture and makes it more modular and a little neater.
It is not clear from your code snippet, but I will assume you are using UnityScript.
In your script that handles the touch code when you click on the chip, insert this line:
hit.transform.SendMessage("Spin", hit.transform, SendMessageOptions.DontRequireReceiver);
Put this code in a separate script called "SpinChip" and then add the script to your chip object.
var StartSpeed = 360.0;
var Deceleration = 3.0;
function Spin()
{
if (Animating)
{
print("Chip is already spinning, not starting another animation");
return;
}
/*
This code isn't necessary if this exists in a separate script and is only ever attached to the clickable chip
if (!gameObject.tag.Contains("Chip"))
{
print("That wasn't a chip you clicked");
return;
}
*/
print("Chip has been told to spin");
StartCoroutine(SpinningAnimation);
}
function SpinningAnimation()
{
print("Chip spin start");
transform.localRotation = Quaternion.identity;
Speed = StartSpeed;
Animating = true;
while (Speed > 0)
{
transform.Rotate(0, Speed*Time.deltaTime, 0);
Speed -= Deceleration;
yield; // wait one frame
}
print("Chip has completed the spin");
Animating = false;
}
What this code does is create a co-routine that runs once per update loop when activated that will spin the chip for you, and is independent of your actual button clicking code.
var rotSpeed: float = 60; // degrees per second
function Update(){
transform.Rotate(0, rotSpeed * Time.deltaTime, 0, Space.World);
}
Here is a code that rotates your game object, you can use it just with a vector 3 :transform.Rotate(x, y, z); or with Space transform.Rotate(x, y, z, Space.World);
rotSpeed is the rotation speed.
In your update function . The Bool variable Animate may becoming true . This may be reason your cylinder continues to rotate.
Other Solution is : You can create an animation of your cylinder and then take a stopwatch . So that after sometime you can stop you animation using the time of stopwatch

Resources