JointJS: the best method for doing a transition in both x and y? - transition

I'm animating an element's motion to a desired new point, but I have not been able to work out the proper syntax to pass the transition function two values. I have a workaround right now:
packet.transition('position/y', linkInfo.end.y, {
delay: animationDelay,
duration: animationDuration
});
packet.transition('position/x', linkInfo.end.x, {
delay: animationDelay,
duration: animationDuration
});
That seems less than satisfactory as I know there has to be a better way. I've spent too long poring over the docs and tutorials and the correct method still isn't penetrating my thick skull. Can some kind soul help out?
Many thanks in advance!

This is the code I ended up using. My problem was not understanding the interpolation types.
var linkInfo = {
start: {
x: startX,
y: startY
},
end: {
x: endX,
y: endY
},
source: sourceCell,
target: targetCell
};
packet.position(linkInfo.start.x, linkInfo.start.y);
graph.addCell(packet);
packet.transition('position', linkInfo.end, {
delay: animationDelay,
duration: animationDuration,
valueFunction: joint.util.interpolate.object
});

Did you try something like this:
packet.transition('position', {x: linkInfo.end.y, y: linkInfo.end.y}, {
delay: animationDelay,
duration: animationDuration
});

Related

Entity disappears at the end of the animation

I'm trying to animate the movement of an entity but at the end of the animation the entity disappears. It occurs if you animate the scale or translation but not rotation. I'm not sure if it's a bug or expected behaviour but I would like to find a way to stop it.
let transform = Transform(scale: simd_float3.one,
rotation: simd_quatf(),
translation: [0.05, 0, 0])
let animationDefinition = FromToByAnimation<Transform>(by: transform,
duration: 1.0,
bindTarget: .transform)
if let animationResource = try? AnimationResource.generate(with: animationDefinition) {
entity.playAnimation(animationResource)
}
I know you can use entity.move() and that works fine but I want to explore other ways to animate entities.
This transform animation works as expected. Fix the opacity of your model, if it has translucent materials (for that use USDZ Python Tools commands fixOpacity and usdARKitChecker). Also, check if any transformation ​​are applied to the entity on which you are running the transform animation.
let boxScene = try! Experience.loadBox()
boxScene.children[0].scale *= 3
arView.scene.anchors.append(boxScene)
let entity = boxScene.children[0].children[0]
let transform = Transform(scale: simd_float3.init(2, 2, 2),
rotation: simd_quatf.init(angle: .pi, axis: [1, 1, 1]),
translation: [0.4, 0, 0])
let animationDefinition = FromToByAnimation<Transform>(by: transform,
duration: 2.0,
bindTarget: .transform)
if let anime = try? AnimationResource.generate(with: animationDefinition) {
entity.playAnimation(anime)
}
As you can see from this example, after applying the animation, the entity does not disappear.

SKAction not executing

let bag = SKSpriteNode(imageNamed: "Content/bag")
holder.addChild(bag)
bag.name = "bag"
bag.zPosition = 5
bag.position = CGPoint(x: -3, y: (holder.size.height / 2) + (bag.size.height / 2) - 6)
bag.setScale(0.5)
print(bag.parent)
bag.runAction(SKAction.playSoundFileNamed("Content/wrap.wav", waitForCompletion: true))
bag.runAction(SKAction.scale(to: 1.0, duration: 1.0, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0)) {
bag.physicsBody = SKPhysicsBody(rectangleOfSize: bag.size)
bag.physicsBody!.dynamic = false
bag.physicsBody!.categoryBitMask = CollisionTypes.Bag.rawValue
bag.physicsBody!.collisionBitMask = CollisionTypes.Ingredients.rawValue
}
print(bag.parent)
The above code gets executed whenever the user taps the "holder" which is just a sprite already on the scene. It should create a new "bag" sprite, add it to the holder, and scale it up from 0.5 to 1.0 after running a sound effect.
The problem is that neither the sound effect or the scale action are being run. I searched this problem and it seems like the case is that the node hasn't been added but that's not the case here. I print the bag's parent before and after the actions and neither is nil. Also, the bag IS being added because I see it on the screen at 0.5 scale. How are neither of these actions being ran?

SKAction with Key changes Sprite Behavior

I've been learning Swift while laid up from back surgery and any help with this is greatly appreciated.
Issue Description:
I have a function that pushes a sprite backwards when hit. When I use
self.runAction(jumpBackSequence)
I get the desired behavior. (The sprite is pushed back a good distance.)
Now when I update this to use "withKey" (because I need to be able to stop the forward action again on the next contact the forward movement is greatly dimished.
self.runAction(jumpBackSequence, withKey: "moveStraightLine")
My function creating this action is below:
func pushBack(){
self.removeActionForKey("moveStraightLine")
let height:CGFloat = 5
let jumpDistanceA:CGFloat = 20
let jumpDistanceB:CGFloat = 20
let duration:NSTimeInterval = 0.15
let jumpUp = SKAction.moveByX(0, y:height, duration:duration)
let moveBack = SKAction.moveByX(jumpDistanceA, y: 0, duration: duration)
let jStep1 = SKAction.group([jumpUp, moveBack])
let jumpDown = SKAction.moveByX(0, y:(height * -1), duration:duration)
let moveBack2 = SKAction.moveByX(jumpDistanceB, y: 0, duration: duration)
let jStep2 = SKAction.group([jumpDown, moveBack2])
let moveZombie1 = SKAction.moveToX(0, duration: spawnSpeed1() )
let removeZombie1 = SKAction.removeFromParent()
let moveAndRemoveZombie1 = SKAction.sequence([moveZombie1, removeZombie1])
let jumpBackSequence = SKAction.sequence([jStep1, jStep2, moveAndRemoveZombie1])
self.runAction(jumpBackSequence)
// self.runAction(jumpBackSequence, withKey: "moveStraightLine")
}
You are using moveTo, not moveBy, so when you add the action back in to move the zombie, you are now saying starting from the closer spot, take X seconds to move to the end, not take X seconds - the time already travelled. You do not want this due to the effect you see. Use moveBy, and the zombie speed will stay consistant
I'm not sure why this behavior happened, but I realized that the runAction withKey was actually moving the code the specified distance where as the example without the key was not. When I updated the code to use a fraction of screen width it behaves the same with and without a key - which makes me think this might actually be an minor bug in Swift.
To fix the issue I updated the distances to
let jumpDistanceA:CGFloat = (self.scene?.size.width)! * 0.15
let jumpDistanceB:CGFloat = (self.scene?.size.width)! * 0.15
I hope this helps someone else.
Best regards,
mpe

table top gaming: how make character walk from one house to another, passing through every house in between?

I just start to learn coding with Swift last month. This is my second personal project.
I'm creating a table top game like "Monopoly" or something. Here an image of my prototype:my table top board game
I'm using a arc4random for the dices and moving the character/player with this code:
if positionPlayer1 == 1 {
let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 1 * Int64(NSEC_PER_SEC))
dispatch_after(time, dispatch_get_main_queue()) {
player1.runAction(SKAction.moveByX(-66, y: 0, duration: 1))
}
} else if positionPlayer1 == 2 {
let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 1 * Int64(NSEC_PER_SEC))
dispatch_after(time, dispatch_get_main_queue()) {
player1.runAction(SKAction.moveByX(-44, y: 0, duration: 1))
}
I also tried :
player1.runAction(SKAction.moveByX(-66, y: 0, duration: 1))
sleep(1)
The problem is: the character waits the total time e runs directly to the final position. Let's say the dice give an 6, the character waits for 6 seconds and runs to the last position. This is not a problem on a straight line, but if the movement pass through a corner, the character cuts in diagonal directly to the final position instead of pass through every house as intended.
Any help will be appreciated.
Edit: I decided to try physics. I created a lot of SKSpriteNode "walls". To all nodes I'm using:
squareBlock02.physicsBody?.allowsRotation = false
squareBlock02.physicsBody?.pinned = true
squareBlock02.physicsBody?.dynamic = false
The result is that the player hits those walls and move them. I tried mass, density... nothing prevents the player from push all walls from its way.
Thank you all people that try to help.
I finally found the answer at this site: http://www.gamefromscratch.com/post/2014/07/02/Game-development-tutorial-Swift-and-SpriteKit-Part-4-Actions.aspx
import SpriteKit
// Solution found: http://www.gamefromscratch.com/post/2014/07/02/Game-development-tutorial-Swift-and-SpriteKit-Part-4-Actions.aspx
class GameScene: SKScene {
let player = SKSpriteNode(imageNamed: "Player_01-2.png")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
player.anchorPoint = CGPoint(x:0.5,y:0.5)
player.position = CGPoint(x:view.bounds.midX,y:view.bounds.midY)
self.addChild(player)
doStuff()
}
func doStuff(){
var actions = Array<SKAction>()
actions.append(SKAction.moveTo(CGPoint(x:300,y:300), duration: 1))
actions.append(SKAction.rotateByAngle(6.28, duration: 1))
actions.append(SKAction.moveBy(CGVector(dx: 150,dy: 0), duration: 1))
actions.append(SKAction.colorizeWithColor(SKColor.redColor(), colorBlendFactor: 0.5, duration: 1))
let sequence = SKAction.sequence(actions)
player.runAction(sequence)
}
}

Running Stick Figure in Sprite Kit Animation

I am playing around a little bit in SpriteKit, and am trying to make a stick figure run. Due to my lack of experience, I believe I have started this process in an unconventional way.
Instead of drawing out each individual motion frame, I loaded the Separate images of the stick figure (ie the Head, the knee, the Thigh, the body, the arm, etc.) and told them to move accordingly. So far, everything fits together except for the motion and rotation of knee during the running sequence.
My problem is this:
When the thigh pulls back in it's stride, the knee becomes detached, and follows a straight line to the end of the stride while rotating.
My Question is this:
Is there a way to connect the thigh node to the calf node, as if it they were attached at a "knee" per say, while in motion? or is there some more efficient way?
Here is a snippet of my code, It may be a little more descriptive than I have been.
func thigh_run(){
//running config
//DONT CHANGE!
var thigh_rotation = CGFloat(M_PI)*0.8
//Can Change :)
var run_speed = NSTimeInterval(0.25)
// reset the action sequence... basically
let pos_nuetralize = SKAction.rotateByAngle(thigh_rotation/2, duration: run_speed)
let neg_nuetralize = SKAction.rotateByAngle(-thigh_rotation/2, duration: run_speed)
//strides
let thigh_pull = SKAction.rotateByAngle(-thigh_rotation, duration: run_speed*2)
let thigh_stride = SKAction.rotateByAngle(thigh_rotation/2, duration: run_speed)
// for the opposite leg
let b_thigh_pull = SKAction.rotateByAngle(-thigh_rotation/2, duration: run_speed)
let b_thigh_stride = SKAction.rotateByAngle(thigh_rotation, duration: run_speed*2)
//knee movement
let stride = SKAction.sequence([thigh_stride, thigh_pull, pos_nuetralize])
let pull = SKAction.sequence([b_thigh_pull, b_thigh_stride, neg_nuetralize])
r_arm.runAction(SKAction.repeatActionForever(stride))
f_leg_thigh.runAction(SKAction.repeatActionForever(stride))
b_leg_thigh.runAction(SKAction.repeatActionForever(pull))
//knee_run(run_speed)
}
func knee_run(speed: NSTimeInterval){
let knee_stride = SKAction.moveTo(CGPointMake(f_leg_thigh.position.x+f_leg_thigh.size.height, f_leg_thigh.position.y), duration: speed)
let knee_reset = SKAction.moveTo(CGPointMake(f_leg_thigh.position.x, f_leg_thigh.position.y-f_leg_thigh.size.height), duration: speed)
let knee_pull_rotate = SKAction.rotateToAngle(CGFloat(M_PI)/2, duration: speed, shortestUnitArc: true)
let knee_pull_rotate_half = SKAction.rotateToAngle(CGFloat(M_PI)*2, duration: speed, shortestUnitArc: true)
let knee_pull_half = SKAction.moveTo(CGPointMake(f_leg_thigh.position.x-f_leg_thigh.size.height, f_leg_thigh.position.y/2), duration: speed)
let knee_pull = SKAction.moveTo(CGPointMake(f_leg_thigh.position.x-f_leg_thigh.size.height, f_leg_thigh.position.y), duration: speed)
//Grouping of the knee moving and rotating at the same time
let knee_pull_group_half = SKAction.group([knee_pull_half, knee_pull_rotate_half])
let knee_pull_group = SKAction.group([knee_pull, knee_pull_rotate])
let f_knee_stride_seq = SKAction.sequence([knee_stride,knee_reset,knee_pull,knee_reset])
let f_knee_pull_seq = SKAction.sequence([knee_pull_group_half, knee_reset, knee_stride, knee_reset])
f_leg_calf.runAction(SKAction.repeatActionForever(f_knee_stride_seq))
b_leg_calf.runAction(SKAction.repeatActionForever(f_knee_pull_seq))
}
I understand it may be hard to read, I have Documented it for myself pretty well. the f_ prefix is for "front"leg and b is for "back"_leg. think of it as right leg and left leg however.
Anyway! Thanks in advance! I appreciate any help or tips! Comment for any further explanation! :)
The more efficient way is to use SKPhysicsJoint to connect the thigh node to the calf node.