Start/Stop a sprite animation from/on a specific frame in Spritekit - swift3

​I have a sprite animation of 30 frames, 6 of these frames are keyframes and the rest are transitions from keyframe to keyframe.
I am trying to figure out how to do the following:
When button is pressed the sprite starts animating. Animation will run at least once and then it will stop the animation on a randomly chosen keyframe.
When button is pressed again, animation will start from the last chosen keyframe and will run 1 complete cycle and then stops again on a randomly chosen keyframe.
I've found a similar question here but that was asked in 2014. Did Swift and Spritekit change enough in 3 years to make it possible?
Edit:
#Alessandro Ornano
I've tried this:
var roll1Textures = ["Roll1Motion0", "Roll1Motion1", "Roll1Motion2", "Roll1Motion3", "Roll1Motion4", "Roll1Motion5", "Roll1Motion6", "Roll1Motion7", "Roll1Motion8", "Roll1Motion9", "Roll1Motion10", "Roll1Motion11", "Roll1Motion12", "Roll1Motion13", "Roll1Motion14", "Roll1Motion15", "Roll1Motion16", "Roll1Motion17", "Roll1Motion18", "Roll1Motion19", "Roll1Motion20", "Roll1Motion21", "Roll1Motion22", "Roll1Motion23", "Roll1Motion24", "Roll1Motion25", "Roll1Motion26", "Roll1Motion27", "Roll1Motion28", "Roll1Motion29"]
let roll1Animation = SKAction.animate(with: Array(roll1Textures[0...5]), timePerFrame: 0.06, resize:false, restore:false)
But I get an error message: Cannot use instance member 'roll1Textures' within property initialiser; property initialisers run before 'self' is available.
How did I implement your code in a wrong way?

I don't know if it could be useful to your game but now in Swift you can also do in one line:
let animation = SKAction.animate(with: Array(textures[0...5]), timePerFrame: 0.06, resize:false, restore:false)
In other words you can create an array slice composed by your specific range then re-create the array of textures with Array

Related

Marquee (scrolling) Text in SwiftUI

I am having difficulties implementing the marquee (scrolling) text view in SwiftUI. The problem is that the text needs to stay in the original (0) position for 2 seconds and the starts moving to the left, show up on the right and continue scrolling until the 0 position and wait for 2 seconds again.
You can work with what's in this video to get something working, but honestly it's difficult to make it work with varying screen sizes if your view is in any way responsive. But if you are using the foundation in that video for the animations and combining that with a geometry reader to get your zero position and rightmost entry point position, you could get the scroll positioning correct.
As for starting and stopping, I would suggest you use a Timer to toggle a boolean #State variable called scrollText to start/stop the animation 2 seconds after its duration. You would start by first toggling the animation in .onAppear, like this:
Text("Hello World")
.offset(x: scrollText ? zeroPoint : screenEntryPoint)
.animation(Animation.linear(duration: 8).repeatForever(autoreverses: false))
.onAppear {
self.scrollText.toggle()
}
If you then set a timer for 10 seconds to toggle scrollText, you would be able to get the animation to run for 8s, pause 2 seconds, and then run again.
Please note that to make marquee text work, you also need to REPEAT the text twice, so that the text will animate in from the right while it's also leaving from the left. Good luck!

RPG Maker MV - Checking which frame of a character sheet is currently active

I have a character sheet for the player that triggers when they are idle for a while. The sheet has them going from standing to laying down in 6 frames, sleeping for 3 frames and then standing back up in 7 frames.
I'm looking to pause the animation when it reaches the 9th frame without using timers (as the character sheet may change in the future). Is there a function that will let me query which animation frame is currently being displayed?
Alternatively, is there a way to know if a character sheet has reached the last frame and is about to return to the first frame. I need to know when this animation has finished a cycle so I can smoothly transition back into the walking animation.
I'm extending Modern Algebra's ExtraMovementFrames plugin, so the solution ended up being to capture the output of this:
var patternIndex = (this._pattern % this.emfCharacterState().pattern.length);
which uses the ._pattern property of the player, modulo the length of the animation.
I then used this.setStepAnime(false); to pause the walking animation of the character once it reached the desired frame.
Also asked this on the rpg maker mv forums: https://forums.rpgmakerweb.com/index.php?threads/checking-which-frame-of-a-character-sheet-is-currently-active.115662/

Cocos2D-X CCTransition shows background between the 2 scenes

I've been trying to successfully create a slide transition between 2 scenes for the past 2 hours and all I can find are just some answers in Objective-C and I have no idea how to translate them.
I'm using XCode 5 with Cocos2D-X 2.2.3 to develop for iOS.
CCTransitionSlideInT *trans = CCTransitionSlideInT::create(1.0, Scene2::scene());
CCDirector::sharedDirector()->replaceScene(trans);
Doing that makes my current scene slide down and gets replaced by black (background) THEN after the transition is done, the second scene appears without any transition effects; it just appears.
How can I slide the first scene with the second one attached to it?
I've seen that it's done in TestCPP. I tried tracing it but it got too complicated for me. All I really need is some clarification!
Thanks in advance! And I hope I provided all the necessary info.

Grid does not animate from collapsed to visible

I'm currently trying to make a grid that animates from collapsed -> visible to notify the user that the save has been completed. In Blend 4 I opened the project and created a usercontrol for the SaveNotifier so I can use it in other areas of this project and others. I created the default to be collapsed and also created another state called "Complete" which has visibility set to Visible and has a timetrigger of 3 seconds which sends it back to the default state. The transitions are set to transition over 1 second and use fluidlayout to show the animation between the states, but it does not show the animation between states. Instead it just shows it as if there was no fluidlayout or transition time.
If someone would be so kind as to let me know if there is a problem with trying to do this or even show me how to do this it would be gre
Not sure why, but when I put the grid inside of a new grid it started working. Seems as if you need to have a grid to be the full size for the animation to display.

Cocos2d scroll layer on Sprite drag

I have designed a small tutorial named "Stacker", As the name suggests, The game involves stacking blocks on each other. I have a large number of blocks to be stacked and hence all cant be accomodated in the screen itself... I m new to cocos2d and box2d but have managed to create a body with its adjoining sprite wen a user clicks on the screen. I have used MouseJoint to give movement to the body till the user performs the drag action that is till the user takes his finger off the screen.
The problem is that i need to follow the sprite (actually need the camera to follow the sprite) when the user drags it above the screen space, i referred the following links with no success... i guess wat i need is to move the body to a virtual coordinates which m not getting coz even if the screen does shift using the camera methods, but the sprite doesnt move with respect to the screen...
cocos2d forum link
flash concept but box2d
Can some1 guide me in case i need to have some pre-requisites before following camera in the manner i specified.. Thanx!
Ok Guys!
Got it guys! Had to take a global variable which records the increments per frame, The increments were equal to the layer movement which i did by setting the position of the layer to a unit less in every frame! Then set the Mouse join to the target which is (ScreenCoordinates + increment) dis too has to be done in every frame!
Done!
Cool method but needed a bit of brainstorming!!